ansible工具之playbook

playbook简介

主要功能:将分组主机按照定义好的playbook执行。
play:定义好的角色task,task一般为ansible的模块。
playbook:将多个play组合在一起,就是playbook
playbook采用yaml语言编写,遵循yaml语法格式。

YAML介绍:

    YAML是一个可读性高的用来表达资料序列的格式。
    YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。
    Clark Evans在2001年在首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者

YAML特性:

    - YAML的可读性好
    - YAML和脚本语言的交互性好
    - YAML使用实现语言的数据类型
    - YAML有一个一致的信息模型
    - YAML易于实现
    - YAML可以基于流来处理
    - YAML表达能力强,扩展性好

YAML语法:

    - 在单一档案中,可用连续三个连字号(---)区分多个档案。另外,还有选择性的连续三个点号( ... )用来表示档案结尾
    - 次行开始正常写Playbook的内容,一般建议写明该Playbook的功能
    - 使用#号注释代码
    - 缩进必须是统一的,不能空格和tab混用
    - 缩进的级别也必须是一致的,同样的缩进代表同样的级别,程序判别配置的级别是通过缩进结合换行来实现的
    - YAML文件内容和Linux系统大小写判断方式保持一致,是区别大小写的,k/v的值均需大小写敏感
    - k/v的值可同行写也可换行写。同行使用:分隔
    - v可是个字符串,也可是另一个列表
    - 一个完整的代码块功能需最少元素需包括 name: task
    - 一个name只能包括一个task
    - YAML文件扩展名通常为yml或yaml
    - Dictionary:字典,通常由多个key与value构成,也可以将key:value 放置于{}中进行表示,用,分隔多个 key:value

playbook基础组件

Hosts:用于指定要执行指定任务的主机,须事先定义在主机清单中。

示例:

- hosts: websrvs:dbsrvs

remote_user:执行身份

(1)可用于Host和task中。
(2)通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务。
(3)可以在sudo时使用sudo_user指定sudo时切换的用户

示例:

- hosts: websrvs
  remote_user: root
  tasks:
   - name: test connection
     ping:
     remote_user: fz.hou
     sudo: yes     默认sudo为root
     sudo_user:fl  sudo为fl

task:任务列表

格式:
(1) action: module arguments
(2) module: arguments 建议使用
注意:shell和command模块后面跟命令,而非key=value
示例:

tasks:
 - name: disable selinux
   command: /sbin/setenforce 0

notify与handlers:
某任务的状态在运行后为changed时,可通过“notify”通知给相应的handlers,继而执行handlers之后的命令。

tags:标签
任务可以通过”tags“打标签,而后可在ansible-playbook命令上使用-t指定进行调用
注意:如果多个任务标签相同,标签被调用时,任务都会被执行。

示例:安装httpd,修改httpd配置文件,并重启服务。

- hosts: webservers
  remote_user: root

  tasks:
    - name: install httpd
      yum: name=httpd
    - name: modify config
      copy: src=~/httpd.conf dest=/etc/httpd/conf/httpd.conf
      tags: modify
      notify: restart httpd
    - name: start httpd
      service: name=httpd state=started enabled=yes

  handlers:
    - name: restart httpd
      service: name=httpd state=restarted

示例结果:

未分类

注意:如果命令或脚本的退出码不为零,可以使用如下方式替代:

tasks:
  - name: run this command and ignore the result
    shell: /usr/bin/somecommand || /bin/true

或者使用ignore_errors来忽略错误信息:

tasks:
  - name: run this command and ignore the result
    shell: /usr/bin/somecommand
    ignore_errors: True

运行playbook

运行playbook的方式

ansible-playbook <filename.yml> ... [options]

常见选项
–check 只检测可能会发生的改变,但不真正执行操作
–list-hosts 列出运行任务的主机
–limit 主机列表 只针对主机列表中的主机执行
-v 显示过程 -vv -vvv 更详细

playbook变量

变量名:仅能由字母、数字和下划线组成,且只能以字母开头
变量来源:

1、ansible setup facts 远程主机的所有变量都可直接调用

示例:

ansible myhosts -m setup -a 'filter=ansible_nodename'

filter是用来匹配后面的字符串,可以使用正则表达式。
也可以使用grep过滤,-C选项查看上下文三行。

示例结果:

未分类

2、在/etc/ansible/hosts中定义

普通变量:主机组中主机单独定义,优先级高于公共变量
公共(组)变量:针对主机组中所有主机定义统一变量
普通变量示例:在/etc/ansible/hosts文件中定义

[myhosts]
172.18.18.22 http_port=85 hname=nginx
172.18.18.23 http_port=86 hname=httpd

编写playbook:

cat /root/ansible/vars4.yml

  ---
  - hosts: myhosts
    remote_user: root

    tasks:
     - name: set hostname
       hostname: name={{ hname }}-{{ http_port }}

示例结果:

未分类

公共(组)变量示例:在/etc/ansible/hosts文件中定义

[myhosts:vars]
myh=HFZ

编写playbook:

cat /root/ansible/vars5.yml

  ---
  - hosts: myhosts
    remote_user: root

    tasks:
     - name: set hostname
       hostname: name={{ myh }}-{{ hname }}-{{ http_port }}

示例结果:

未分类

3、通过命令行指定变量,优先级最高

ansible-playbook –e varname=value

示例:

cat /root/ansible/vars.yml

  ---
  - hosts: myhosts
    remote_user: root

    tasks:
     - name: install package
       yum: name={{ pkname }}

示例结果:

未分类

4、在playbook中定义

示例:

cat cat vars2.yml

  ---
  - hosts: myhosts
    remote_user: root
    vars:
     - username: user1
     - groupname: group1

    tasks:
     - name: create group
       group: name={{ groupname }} state=present
     - name: create user
       user: name={{ username }} group{{ groupname }} home=/home/{{ username }}dir

示例结果:

未分类

5、可以在文件中定义变量,并在playbook中调用文件。

示例:在vars.yml文件中定义变量

hi: hello
wd: world

编写playbook:

- hosts: myhosts
  remote_user: root
  vars_files:
   - vars.yml

  tasks:
   - name: create file
     file: name=/root/{{ hi }}-{{ wd }}.log state=touch

示例结果:

未分类

6、在role中定义

playbook中的templates模板

templates特点:

基于Jinja2语言的文本文件,嵌套有脚本。

templates功能:

根据模块文件动态生成对应的配置文件

templates格式:

templates文件必须存放于templates目录下,且命名为 .j2 结尾。

yaml/yml 文件需和templates目录平级,目录结构如下:

./
 ├── temnginx.yml
 └── templates
   └── nginx.conf.j2

Jinja2语言:

使用字面量:

    字符串:使用单引号或双引号
    数字:整数,浮点数
    列表:[item1, item2, ...]
    元组:(item1, item2, ...)
    字典:{key1:value1, key2:value2, ...}
    布尔型:true/false
算术运算:+, -, *, /, //, %, **
比较操作:==, !=, >, >=, <, <=
逻辑运算:and, or, not
流表达式:for、if、when

示例:在centos6与centos7主机上安装httpd服务,并修改相应配置文件。

1、创建文件夹

mkdir ~/ansible/templats -pv

2、拷贝centos6与centos7主机上的httpd配置文件到主机。并修改文件名

ansible myhosts -m fetch -a 'src=/etc/httpd/conf/httpd.conf dest=~/ansible/'

3、复制文件到templats文件夹下并修改文件名,修改文件内容

mv ~/ansible/172.18.18.22/httpd.conf ~/ansible/templats/httpd-7.conf.j2
mv ~/ansible/172.18.18.22/httpd.conf ~/ansible/templats/httpd-6.conf.j2

4、编写playbook,注意httpd.yml与templats文件夹同级

cat httpd.yml 
    - hosts: myhosts
      remote_user: root

      tasks:
        - name: install httpd
          yum: name=httpd

        - name: templates-7
          template: src=httpd-7.conf.j2 dest=/etc/httpd/conf/httpd.conf
          when: ansible_distribution_major_version == "7"
          notify: restart httpd
          tags: conf

        - name: templates-6
          template: src=httpd-6.conf.j2 dest=/etc/httpd/conf/httpd.conf
          when: ansible_distribution_major_version == "6"
          notify: restart httpd
          tags: conf

        - name: start httpd
          service: name=httpd state=started

      handlers:
         - name: restart httpd
           service: name=httpd state=restarted

示例演示:

未分类

playbook迭代

迭代:当有需要重复性执行的任务时,可以使用迭代机制
对迭代项的引用,固定变量名为”item”
要在task中使用with_items给定要迭代的元素列表
列表格式:
字符串
字典

示例:创建固定组,并把新建用户加入到固定组中。

cat items.yml:
    - hosts: myhosts
      remote_user: root

      tasks: 
        - name: create groups
          group: name={{item}}
          with_items:
            - itemgroup1
            - itemgroup2
            - itemgroup3
        - name: create users
          user: name={{item.username}} group={{item.groupname}}
          with_items:
            - {username: 'testuser1',groupname: 'itemgroup1'}
            - {username: 'testuser2',groupname: 'itemgroup2'}
            - {username: 'testuser3',groupname: 'itemgroup3'}

示例结果:

未分类

playbook中template for if

示例:利用for-if和templates编写playbook

cat for-if.yml 
    - hosts: myhosts
      remote_user: root
      vars:
        hosts:
          - {listen_prot: 8080,web: nginx1,name: web1.fz.com}
          - {listen_prot: 8081,web: nginx2,name: web2.fz.com}
          - {listen_prot: 8082,web: nginx3}

      tasks:
        - name: for-if
          template: src=for-if.j2 dest=/root/for-if

cat templates/for-if.j2
    {% for host in hosts %}
    server{
            listen: {{host.listen_prot}};
    {%if host.name is defined%}
            name: {{host.name}};
    {%endif%}
            web: {{host.web}};
    }
    {%endfor%}

示例结果:

未分类

playbook加密

    - ansible-vault:管理加密解密yml文件
    - ansible-vault encrypt hello.yml 加密
    - ansible-vault decrypt hello.yml 解密
    - ansible-vault view hello.yml 查看
    - ansible-vault edit hello.yml 编辑加密文件
    - ansible-vault rekey hello.yml 修改口令
    - ansible-vault create new.yml 创建新文件

ansible初次试探

作为Python运维开发人员,老早就注意了ansible,saltstack。
以前专门去买腾讯云的机器来研究saltstack。现在是免费领取了华为的云机器来研究。忧伤~
使用ansible 2.4版本的,其它老版本不适合这种情况哦!把主控端A的公钥发送给被控端B :

sudo ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]  

观察B我们就发现了一个新的文件authorized_keys。

[root@fdafda .ssh]# pwd  
/root/.ssh  
[root@fdafda .ssh]# ls -l  
total 4  
-rw------- 1 root root 748 Jan 22 16:55 authorized_keys  
[root@fdafda .ssh]# cat authorized_keys  
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDFB98VspJGjtAaTQ3pJCgrl9MbOSgxz2AZZPQ9eIjzAnJfNsfLT7JClgIRa/plQjOwKAP1wUZ631mD2BzGenf05dicgFLYcfhd3g/68Fugymd6Oejbb4XciTJiHh4965vc0+P8vhQZ5wMT8ZGxQwOL8Uabw9HoKdzcckZtUol6axmP25Jv28+3kRXJov8VubahVgeVkNPYETBElKDb+lilm+yxLNRez1euzzBbYyb5ak6sDeMHuo8ZakyO1+lSl1Dv33a3E/eqLWSo0YpbVVhqkapTuFAklJ33vMo+Ejw2WjmHgpnLsevFfAb9Qc9pJoGVylREeZwEjLLjhEt9eW6el2MMxxj+8siNYA2zHnYp2YXZprDSj9SxpUgOp0A8P3AeIlARrnOF87QsXBx4V7z8x8itSrMs++OIbLhzCbBl8KbZtV8HvI2ITpfVaLdLBCoox0Bp/kZrXHyM81A0axskPhBrQ178uXY1rS3qKv7v73n93OCA9X4bHNtHhRlFeVQ7SpdHnkHvW2dNy+5gBdbOKigvtFGdMyvqyL2J5grL+K7zKfNt2PYg0wvSAbvvXBRGJQ+Pzi6azhlEdwdtA4HxYilFI22luIYmLEWKIi2d4cCfU1UWN94yMSdkVv4sYsXhXZeHslsis8L/Vm6TmChCTf7R2f8VeKugOuptQRbwFQ== [email protected]  

1、ansible模块因为随处可见,暂时不写
2、ansible playbook使用
demo:这是我的playbook.yml文件。example为hosts里面的组名

未分类

然后执行:

ansible-playbook ./playbook.yml  

结果:

未分类

目标机器上:

未分类

Ansible扩展

Ansible简介

Ansible是由Python开发的一个运维工具,因为工作需要接触到Ansible,经常会集成一些东西到Ansible,所以对Ansible的了解越来越多。

那Ansible到底是什么呢?在我的理解中,原来需要登录到服务器上,然后执行一堆命令才能完成一些操作。而Ansible就是来代替我们去执行那些命令。并且可以通过Ansible控制多台机器,在机器上进行任务的编排和执行,在Ansible中称为playbook。

那Ansible是如何做到的呢?简单点说,就是Ansible将我们要执行的命令生成一个脚本,然后通过sftp将脚本上传到要执行命令的服务器上,然后在通过ssh协议,执行这个脚本并将执行结果返回。

那Ansible具体是怎么做到的呢?下面从模块和插件来看一下Ansible是如何完成一个模块的执行

PS:下面的分析都是在对Ansible有一些具体使用经验之后,通过阅读源代码进一步得出的执行结论,所以希望在看本文时,是建立在对Ansible有一定了解的基础上,最起码对于Ansible的一些概念有了解,例如inventory,module,playbooks等

Ansible模块

模块是Ansible执行的最小单位,可以是由Python编写,也可以是Shell编写,也可以是由其他语言编写。模块中定义了具体的操作步骤以及实际使用过程中所需要的参数

执行的脚本就是根据模块生成一个可执行的脚本。

那Ansible是怎么样将这个脚本上传到服务器上,然后执行获取结果的呢?

Ansible插件

connection插件

连接插件,根据指定的ssh参数连接指定的服务器,并切提供实际执行命令的接口

shell插件

命令插件,根据sh类型,来生成用于connection时要执行的命令

strategy插件

执行策略插件,默认情况下是线性插件,就是一个任务接着一个任务的向下执行,此插件将任务丢到执行器去执行。

action插件

动作插件,实质就是任务模块的所有动作,如果ansible的模块没有特别编写的action插件,默认情况下是normal或者async(这两个根据模块是否async来选择),normal和async中定义的就是模块的执行步骤。例如,本地创建临时文件,上传临时文件,执行脚本,删除脚本等等,如果想在所有的模块中增加一些特殊步骤,可以通过增加action插件的方式来扩展。

Ansible执行模块流程

  • ansible命令实质是通过ansible/cli/adhoc.py来运行,同时会收集参数信息
    • 设置Play信息,然后通过TaskQueueManager进行run,
    • TaskQueueManager需要Inventory(节点仓库),variable_manager(收集变量),options(命令行中指定的参数),stdout_callback(回调函数)
  • 在task_queue_manager.py中找到run中
    • 初始化时会设置队列
    • 会根据options,,variable_manager,passwords等信息设置成一个PlayContext信息(playbooks/playcontext.py)
    • 设置插件(plugins)信息callback_loader(回调), strategy_loader(执行策略), module_loader(任务模块)
    • 通过strategy_loader(strategy插件)的run(默认的strategy类型是linear,线性执行),去按照顺序执行所有的任务(执行一个模块,可能会执行多个任务)
    • 在strategy_loader插件run之后,会判断action类型。如果是meta类型的话会单独执行(不是具体的ansible模块时),而其他模块时,会加载到队列_queue_task
    • 在队列中会调用WorkerProcess去处理,在workerproces实际的run之后,会使用TaskExecutor进行执行
    • 在TaskExecutor中会设置connection插件,并且根据task的类型(模块。或是include等)获取action插件,就是对应的模块,如果模块有自定义的执行,则会执行自定义的action,如果没有的会使用normal或者async,这个是根据是否是任务的async属性来决定
  • 在Action插件中定义着执行的顺序,及具体操作,例如生成临时目录,生成临时脚本,所以要在统一的模式下,集成一些额外的处理时,可以重写Action的方法
  • 通过Connection插件来执行Action的各个操作步骤

扩展Ansible实例

执行节点Python环境扩展

实际需求中,我们扩展的一些Ansible模块需要使用三方库,但每个节点中安装这些库有些不易于管理。ansible执行模块的实质就是在节点的python环境下执行生成的脚本,所以我们采取的方案是,指定节点上的Python环境,将局域网内一个python环境作为nfs共享。通过扩展Action插件,增加节点上挂载nfs,待执行结束后再将节点上的nfs卸载。具体实施步骤如下:

扩展代码:

重写ActionBase的execute_module方法

# execute_module

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import json
import pipes

from ansible.compat.six import text_type, iteritems

from ansible import constants as C
from ansible.errors import AnsibleError
from ansible.release import __version__

try:
    from __main__ import display
except ImportError:
    from ansible.utils.display import Display
    display = Display()


class MagicStackBase(object):

    def _mount_nfs(self, ansible_nfs_src, ansible_nfs_dest):
        cmd = ['mount',ansible_nfs_src, ansible_nfs_dest]
        cmd = [pipes.quote(c) for c in cmd]
        cmd = ' '.join(cmd)
        result = self._low_level_execute_command(cmd=cmd, sudoable=True)
        return result

    def _umount_nfs(self, ansible_nfs_dest):
        cmd = ['umount', ansible_nfs_dest]
        cmd = [pipes.quote(c) for c in cmd]
        cmd = ' '.join(cmd)
        result = self._low_level_execute_command(cmd=cmd, sudoable=True)
        return result

    def _execute_module(self, module_name=None, module_args=None, tmp=None, task_vars=None, persist_files=False, delete_remote_tmp=True):
        '''
        Transfer and run a module along with its arguments.
        '''

        # display.v(task_vars)

        if task_vars is None:
            task_vars = dict()

        # if a module name was not specified for this execution, use
        # the action from the task
        if module_name is None:
            module_name = self._task.action
        if module_args is None:
            module_args = self._task.args

        # set check mode in the module arguments, if required
        if self._play_context.check_mode:
            if not self._supports_check_mode:
                raise AnsibleError("check mode is not supported for this operation")
            module_args['_ansible_check_mode'] = True
        else:
            module_args['_ansible_check_mode'] = False

        # Get the connection user for permission checks
        remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user

        # set no log in the module arguments, if required
        module_args['_ansible_no_log'] = self._play_context.no_log or C.DEFAULT_NO_TARGET_SYSLOG

        # set debug in the module arguments, if required
        module_args['_ansible_debug'] = C.DEFAULT_DEBUG

        # let module know we are in diff mode
        module_args['_ansible_diff'] = self._play_context.diff

        # let module know our verbosity
        module_args['_ansible_verbosity'] = display.verbosity

        # give the module information about the ansible version
        module_args['_ansible_version'] = __version__

        # set the syslog facility to be used in the module
        module_args['_ansible_syslog_facility'] = task_vars.get('ansible_syslog_facility', C.DEFAULT_SYSLOG_FACILITY)

        # let module know about filesystems that selinux treats specially
        module_args['_ansible_selinux_special_fs'] = C.DEFAULT_SELINUX_SPECIAL_FS

        (module_style, shebang, module_data) = self._configure_module(module_name=module_name, module_args=module_args, task_vars=task_vars)
        if not shebang:
            raise AnsibleError("module (%s) is missing interpreter line" % module_name)

        # get nfs info for mount python packages
        ansible_nfs_src = task_vars.get("ansible_nfs_src", None)
        ansible_nfs_dest = task_vars.get("ansible_nfs_dest", None)

        # a remote tmp path may be necessary and not already created
        remote_module_path = None
        args_file_path = None
        if not tmp and self._late_needs_tmp_path(tmp, module_style):
            tmp = self._make_tmp_path(remote_user)

        if tmp:
            remote_module_filename = self._connection._shell.get_remote_filename(module_name)
            remote_module_path = self._connection._shell.join_path(tmp, remote_module_filename)
            if module_style in ['old', 'non_native_want_json']:
                # we'll also need a temp file to hold our module arguments
                args_file_path = self._connection._shell.join_path(tmp, 'args')

        if remote_module_path or module_style != 'new':
            display.debug("transferring module to remote")
            self._transfer_data(remote_module_path, module_data)
            if module_style == 'old':
                # we need to dump the module args to a k=v string in a file on
                # the remote system, which can be read and parsed by the module
                args_data = ""
                for k,v in iteritems(module_args):
                    args_data += '%s=%s ' % (k, pipes.quote(text_type(v)))
                self._transfer_data(args_file_path, args_data)
            elif module_style == 'non_native_want_json':
                self._transfer_data(args_file_path, json.dumps(module_args))
            display.debug("done transferring module to remote")

        environment_string = self._compute_environment_string()

        remote_files = None

        if args_file_path:
            remote_files = tmp, remote_module_path, args_file_path
        elif remote_module_path:
            remote_files = tmp, remote_module_path

        # Fix permissions of the tmp path and tmp files.  This should be
        # called after all files have been transferred.
        if remote_files:
            self._fixup_perms2(remote_files, remote_user)


        # mount nfs
        if ansible_nfs_src and ansible_nfs_dest:
            result = self._mount_nfs(ansible_nfs_src, ansible_nfs_dest)
            if result['rc'] != 0:
                raise AnsibleError("mount nfs failed!!! {0}".format(result['stderr']))

        cmd = ""
        in_data = None

        if self._connection.has_pipelining and self._play_context.pipelining and not C.DEFAULT_KEEP_REMOTE_FILES and module_style == 'new':
            in_data = module_data
        else:
            if remote_module_path:
                cmd = remote_module_path

        rm_tmp = None
        if tmp and "tmp" in tmp and not C.DEFAULT_KEEP_REMOTE_FILES and not persist_files and delete_remote_tmp:
            if not self._play_context.become or self._play_context.become_user == 'root':
                # not sudoing or sudoing to root, so can cleanup files in the same step
                rm_tmp = tmp

        cmd = self._connection._shell.build_module_command(environment_string, shebang, cmd, arg_path=args_file_path, rm_tmp=rm_tmp)
        cmd = cmd.strip()
        sudoable = True
        if module_name == "accelerate":
            # always run the accelerate module as the user
            # specified in the play, not the sudo_user
            sudoable = False


        res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)

        # umount nfs
        if ansible_nfs_src and ansible_nfs_dest:
            result = self._umount_nfs(ansible_nfs_dest)
            if result['rc'] != 0:
                raise AnsibleError("umount nfs failed!!! {0}".format(result['stderr']))

        if tmp and "tmp" in tmp and not C.DEFAULT_KEEP_REMOTE_FILES and not persist_files and delete_remote_tmp:
            if self._play_context.become and self._play_context.become_user != 'root':
                # not sudoing to root, so maybe can't delete files as that other user
                # have to clean up temp files as original user in a second step
                tmp_rm_cmd = self._connection._shell.remove(tmp, recurse=True)
                tmp_rm_res = self._low_level_execute_command(tmp_rm_cmd, sudoable=False)
                tmp_rm_data = self._parse_returned_data(tmp_rm_res)
                if tmp_rm_data.get('rc', 0) != 0:
                    display.warning('Error deleting remote temporary files (rc: {0}, stderr: {1})'.format(tmp_rm_res.get('rc'), tmp_rm_res.get('stderr', 'No error string available.')))

        # parse the main result
        data = self._parse_returned_data(res)

        # pre-split stdout into lines, if stdout is in the data and there
        # isn't already a stdout_lines value there
        if 'stdout' in data and 'stdout_lines' not in data:
            data['stdout_lines'] = data.get('stdout', u'').splitlines()

        display.debug("done with _execute_module (%s, %s)" % (module_name, module_args))
        return data

集成到normal.py和async.py中,记住要将这两个插件在ansible.cfg中进行配置

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.plugins.action import ActionBase
from ansible.utils.vars import merge_hash

from common.ansible_plugins import MagicStackBase


class ActionModule(MagicStackBase, ActionBase):

    def run(self, tmp=None, task_vars=None):
        if task_vars is None:
            task_vars = dict()

        results = super(ActionModule, self).run(tmp, task_vars)
        # remove as modules might hide due to nolog
        del results['invocation']['module_args']
        results = merge_hash(results, self._execute_module(tmp=tmp, task_vars=task_vars))
        # Remove special fields from the result, which can only be set
        # internally by the executor engine. We do this only here in
        # the 'normal' action, as other action plugins may set this.
        #
        # We don't want modules to determine that running the module fires
        # notify handlers.  That's for the playbook to decide.
        for field in ('_ansible_notify',):
            if field in results:
                results.pop(field)

        return results
  • 配置ansible.cfg,将扩展的插件指定为ansible需要的action插件
  • 重写插件方法,重点是execute_module
  • 执行命令中需要指定Python环境,将需要的参数添加进去nfs挂载和卸载的参数
ansible 51 -m mysql_db -a "state=dump name=all target=/tmp/test.sql" -i hosts -u root -v -e "ansible_nfs_src=172.16.30.170:/web/proxy_env/lib64/python2.7/site-packages ansible_nfs_dest=/root/.pyenv/versions/2.7.10/lib/python2.7/site-packages ansible_python_interpreter=/root/.pyenv/versions/2.7.10/bin/python"
1
ansible 51 -m mysql_db -a "state=dump name=all target=/tmp/test.sql" -i hosts -u root -v -e "ansible_nfs_src=172.16.30.170:/web/proxy_env/lib64/python2.7/site-packages ansible_nfs_dest=/root/.pyenv/versions/2.7.10/lib/python2.7/site-packages ansible_python_interpreter=/root/.pyenv/versions/2.7.10/bin/python"

ansible笔记(1):ansible的基本概念

一些基础概念

ansible是什么?
它是一个”配置管理工具”,它是一个”自动化运维工具”,如果你没有使用过任何配置管理工具,不要害怕,看完这篇文章,你自然会对ansible有所了解。

ansible能做什么?
正如其他配置管理工具一样,ansible可以帮助我们完成一些批量任务,或者完成一些需要经常重复的工作。
比如:同时在100台服务器上安装nginx服务,并在安装后启动它们。
比如:将某个文件一次性拷贝到100台服务器上。
比如:每当有新服务器加入工作环境时,你都要为新服务器部署redis服务,也就是说你需要经常重复的完成相同的工作。
这些场景中我们都可以使用到ansible。

看到这里,你可能会说,我编写一些脚本,也能够满足上面的工作场景,为什么还要使用ansible呢?没错,使用脚本也可以完成这些工作,不过我还是推荐你使用ansible,因为ansible支持一些优秀的特性,比如”幂等性”,”幂等性”是什么意思呢?举个例子,你想把一个文件拷贝到目标主机的某个目录上,但是你不确定此目录中是否已经存在此文件,当你使用ansible完成这项任务时,就非常简单了,因为如果目标主机的对应目录中已经存在此文件,那么ansible则不会进行任何操作,如果目标主机的对应目录中并不存在此文件,ansible就会将文件拷贝到对应目录中,说白了,ansible是”以结果为导向的”,我们指定了一个”目标状态”,ansible会自动判断,”当前状态”是否与”目标状态”一致,如果一致,则不进行任何操作,如果不一致,那么就将”当前状态”变成”目标状态”,这就是”幂等性”,”幂等性”可以保证我们重复的执行同一项操作时,得到的结果是一样的,这种特性在很多场景中相对于脚本来说都有一定优势,单单这样说,可能并不容易理解,当你在后面真正使用到时,自然会有自己的体会,所以此处不用纠结,继续向下看。

如果你了解过其他的配置管理工具,比如puppet或者saltstack,那么你一定知道,如果我们想要使用puppet管理100台主机,就要在这100台主机上安装puppet对应的agent(客户端代理程序),而ansible则不同,ansible只需要依赖ssh即可正常工作,不用在受管主机上安装agent,也就是说,只要你能通过ssh连接到对应主机,你就可以通过ansible管理对应的主机。

经过上述描述,我想你应该对ansible已经有了一个初步的、大概的印象:
ansible是一个配置管理工具,可以帮助我们完成一些批量工作或者重复性工作,ansible通过ssh管理其他受管主机,并且具有一些特性,比如幂等性、剧本、模板,角色等,我们会慢慢的介绍这些特性以及怎样使用ansible。

怎样使用ansible呢?我们通过一条简单的命令开始认识它吧,命令如下
注:执行如下命令前,需要进行一些配置,如下命令才能正常执行,后文中会对这些操作进行描述,此处先行略过
Shell

ansible 10.1.1.60 -m ping

上述命令表示,使用ansible去ping 10.1.1.60这台主机,很容易理解吧。
“ping”是ansible中的一个模块,这个模块的作用就是ping对应的主机,ansible调用ping模块,就相当于我们手动执行ping命令一样,上述命令中的”-m ping”表示调用ping模块,当然,ansible肯定不止这一个模块,它有很多模块,不同的模块可以帮助我们完成不同的工作,你应该已经猜到了,我们在实际使用时,会使用到各种模块,ansible是基于这些模块完成实际任务的。

刚才,我们使用了一个简单的ansible命令作为示例,但是如果想要让上述命令正常执行,则必须同时满足两个最基本的条件,如下
条件一、ansible所在的主机可以通过ssh连接到受管主机。
条件二、受管主机的IP地址等信息已经添加到ansible的”管理清单”中。

之前说过,ansible不用在受管主机上安装agent,但是它需要依赖ssh,所以,条件一并不难理解,但是,在满足条件一的情况下,还要同时满足条件二,也就是说,即使ansible所在的主机能够通过ssh连接到受管主机,仍然需要将受管主机的IP地址、ssh端口号等信息添加到一个被称作为”清单(Inventory)”的配置文件中,如果对应的主机信息在ansible的”清单”中不存在,那么ansible则无法操作对应主机,后文会详细的介绍怎样配置ansible的”清单”。

好了,基本概念先了解到这里,现在需要动动手了。

一些基础配置

我们首先要做的就是安装ansible。
但是在安装之前,先介绍一下我的演示环境。
我有四台主机,IP地址分别如下

10.1.1.71

10.1.1.70

10.1.1.61

10.1.1.60

我将主机10.1.1.71(后文中简称71)作为配置管理主机,所以我们需要在71上安装ansible,剩下的主机作为受管主机,主机71和主机70的的操作系统版本为centos7.4,主机61和主机60的操作系统版本为centos6.9。

我使用yum源的方式安装ansible,因为安装ansible需要epel源,所以我配置了阿里的epel源和centos7系统镜像源,yum源配置如下
Shell

# pwd
/etc/yum.repos.d

# cat aliBase.repo
[aliBase]
name=aliBase
baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-$releasever

# cat aliEpel.repo
[aliEpel]
name=aliEpel
baseurl=https://mirrors.aliyun.com/epel/$releaseverServer/$basearch/
enabled=1
gpgcheck=0

yum源配置完成后,安装ansible

yum install ansible

此时yum源中对应的版本为ansible-2.4.2.0-1

安装完毕,不过别急,我们还需要做一些其他的基本配置,在介绍ansible的概念时,我们说过,如果想要通过ansible管理某主机,还需要将对应主机的信息添加到ansible的”配置清单”中,清单中没有的主机无法通过ansible进行配置管理,现在,我们就来介绍一下ansible的”清单”,当安装完ansible以后,ansible会提供一个默认的”清单”,这个清单就是/etc/ansible/hosts,打开此文件,你会看到一些配置示例,没错,还是熟悉的配方,还是熟悉的味道,此文件使用的就是INI的配置风格,那么,我们一起来看看怎样进行配置吧。

以我们的演示环境为例,我们想要通过ansible主机管理60主机,所以,最直接的方式就是将它的IP地址写入到/etc/ansible/hosts文件中,配置如下,在/etc/ansible/hosts文件底部写入如下IP
10.1.1.60
就是这么简单,那么,完成上述配置,就能够通过ansible主机管理10.1.1.60这台主机了吗?我们来动手试试,看看会发生什么情况。

执行之前的示例命令:ansible 10.1.1.60 -m ping
使用ansible去ping主机10.1.1.60,返回结果如下

未分类

从命令的返回信息中可以看到,10.1.1.60不可达,也就是说,ansible无法通过ssh连接到主机60。
返回上述信息是正常的,因为ansible主机并不知道10.1.1.60这台主机的用户名和密码,所以ansible无法通过ssh连接到它。
所以,我们还需要在清单中,配置10.1.1.60主机的ssh信息,才能够进行正确的进行连接,配置示例如下:

未分类

修改清单文件,在之前的主机IP后加入ssh的相关配置信息,如上图所示
ansible_port 用于配置对应主机上的sshd服务端口号,在实际的生产环境中,各个主机的端口号通常不会使用默认的22号端口,所以用此参数指定对应端口。
ansible_user 用于配置连接到对应主机时所使用的用户名称。
ansible_ssh_pass 用于配置对应用户的连接密码。
所以,上图中的配置表示,10.1.1.60这台主机的sshd服务监听在22号端口,当ansible通过ssh连接到主机60时,会使用主机60的root用户进行连接,主机60的root用户的密码为123123
好了,主机60的ssh信息已经配置完毕,我们再来尝试一下,看看之前的命令能不能正常执行,如下

未分类

可以看到,上述命令已经正常执行了,ansible主机成功的ping通了10.1.1.60,从此以后,我们就可以通过ansible主机,管理10.1.1.60这台主机了。

其实,为了更加方便的使用,ansible还支持对主机添加别名,当主机存在别名时,我们可以通过主机的”别名”管理对应主机。
比如,我们想要将10.1.1.60这台主机的别名命名为test60,那么,我们在配置清单时,可以进行如下配置

未分类

如上图所示,当为主机配置别名时,主机的IP地址必须使用anible_host关键字进行指明,否则ansible将无法正确的识别对应的主机。
主机的别名配置完成后,则可以使用主机的别名管理对应主机,示例如下。

未分类

不过,如果你只使用了上述方式配置了主机,则无法通过主机的IP进行管理了,除非你同时使用了别名的方式与IP的方式配置两个主机条目。

注意:上述配置参数都是ansible2.0版本以后的写法,2.0版本之前,应遵从如下写法

ansible_port应该写成ansible_ssh_port
ansible_user应该写成ansible_ssh_user
ansible_host应该写成ansible_ssh_host

因为当前演示环境的ansible版本为2.4,所以,我们使用新的写法进行演示,2.4版本同时也兼容之前的语法。

上述参数,其实都是为了创建ssh连接所使用的,而说到ssh,我们都知道,创建ssh连接时,可以基于密码进行认证,也可以基于密钥进行认证,而在生产环境中,为了提高安全性,我们通常会基于密钥进行ssh认证,甚至会禁用密码认证,那么,当ansible主机需要与受管主机建立ssh连接时,能够基于密钥进行认证码?必须能的。
其实,在实际的使用环境中,我们通常会在”配置管理机(ansible主机)”中生成密钥,然后通过公钥认证的方式连接到对应的受管主机中,如果你对基于密钥认证的方式还不是特别了解,则可以参考如下文章,此处不再对相应配置进行详细的描述:

http://www.zsythink.net/archives/2375

那么,我们就在ansible主机中生成密钥,并进行相应的配置吧。
首先,生成默认格式的密钥对,私钥与公钥。
Shell

# ssh-keygen

然后将生成的公钥加入到10.1.1.60的认证列表

# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

好了,公钥认证的相关操作配置完成,此刻,我们已经可以通过ansible主机免密码连接到主机60中了。

因为配置了密钥认证,所以可以实现免密码创建ssh连接,既然已经能够免密码创建ssh连接,那么在配置”主机清单”时,就没有必要再提供对应主机的用户名与密码了,所以,在完成了密钥认证的相关配置后,我们可以将清单中的配置精简为如下格式。

未分类

或者使用别名的格式

未分类

当然,如果你的受管服务器中的sshd服务使用了默认的22号端口,上述配置中的ansible_port也是可以省略的,为了方便演示,演示环境中的所有受管主机均使用默认的sshd端口号。

如果你的ansible主机上同时存在多对密钥,有可能需要通过不同的密钥连接不同的受管主机,这个时候,你可以通过ssh-agent帮助我们管理密钥,如果你还不了解ssh-agent,那么可以参考如下文章:

http://www.zsythink.net/archives/2407

如果你不想使用ssh-agent管理密钥,也可以通过ansible_ssh_private_key_file参数,指定连接对应主机时所使用的私钥,由于演示环境中并没有同时使用多对密钥,所以此处不再赘述。

在今后的演示中,默认使用密钥认证的方式连接到对应主机,我会提前配置好各个受管主机的密钥认证,后文中将不再对密钥认证的配置过程进行描述。
好了,说了这么多,我想你应该已经了解了ansible的基本概念,以及ansible的一些最基本的配置,在之后的文章中,我们会徐徐渐进,慢慢的介绍ansible的。

ansible任务的异步执行

ansible方便在于能批量下发,并返回结果和呈现。简单、高效。
但有的任务执行起来却不那么直接,可能会花比较长的时间,甚至可能会比ssh的超时时间还要长。这种情况任务是不是没法执行了?
ansible考虑到了这种情况,官方文档介绍了这个问题的解决方法,就是让下发的任务执行的连接变为异步:任务下发之后,长连接不再保持,而是每隔一段时间轮询结果,直到任务结束。

这是官网相关的介绍: http://docs.ansible.com/ansible/latest/playbooks_async.html

他们在playbook的任务中加入两个参数:async和poll。

  • async参数值代表了这个任务执行时间的上限值。即任务执行所用时间如果超出这个时间,则认为任务失败。此参数若未设置,则为同步执行。
  • poll参数值代表了任务异步执行时轮询的时间间隔。

官方给出例子:

  ----
    hosts: all
    remote_user: root
    tasks:
      - name: simulate long running op (15 sec), wait for up to 45 sec, poll every 5 sec
        command: /bin/sleep 15
        async: 45
        poll: 5

这时候已经不怕任务超时了。可以执行一个45s的任务,当然也可以根据需要自己设置。另外,如果poll为0,就相当于一个不关心结果的任务。

如果还想要更方便地看轮询结果,ansible还提供了这个模块async_status。

  ---
    # Requires ansible 1.8+
    - name: 'YUM - fire and forget task'
      yum: name=docker-io state=installed
      async: 1000
      poll: 0
      register: yum_sleeper

    - name: 'YUM - check on fire and forget task'
      async_status: jid={{ yum_sleeper.ansible_job_id }}
      register: job_result
      until: job_result.finished
      retries: 30

第一个job执行异步任务,并且注册了一个名字叫yum_sleeper,用于提供给第二个job作为轮询对象,并且poll设为0,它自己不再轮询。

第二个job使用async_status模块,进行轮询并返回轮询结果。准备检查30次。结果如下:

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [cloudlab001]

TASK [YUM - fire and forget task] **********************************************
ok: [cloudlab001]

TASK [YUM - check on fire and forget task] *************************************
FAILED - RETRYING: TASK: YUM - check on fire and forget task (29 retries left).
FAILED - RETRYING: TASK: YUM - check on fire and forget task (28 retries left).
FAILED - RETRYING: TASK: YUM - check on fire and forget task (27 retries left).
FAILED - RETRYING: TASK: YUM - check on fire and forget task (26 retries left).
FAILED - RETRYING: TASK: YUM - check on fire and forget task (25 retries left).
FAILED - RETRYING: TASK: YUM - check on fire and forget task (24 retries left).
changed: [cloudlab001]

PLAY RECAP *********************************************************************
cloudlab001                : ok=3    changed=1    unreachable=0    failed=0

ansible fetch 批量下载服务器文件

今天使用 ansible 进行批量巡检操作。

思路是写一个 Playbooks,将巡检脚本上传到所有服务器 /tmp 目录下,然后执行,并取回输出的文件。输出的文件路径为:/tmp/log/ip.txt 。ip 为本机 ip 。

Playbooks 内容如下:

---
- hosts:  test
  remote_user: toptea

  tasks:
  - name: transfer file to server
    copy: src=/root/xunjian.sh dest=/tmp/xunjian.sh mode=755

  - name: zhixing 
    become: yes
    become_method:  su
    shell:  /bin/bash -x /tmp/pswd.sh 

上传文件使用 copy 模块,执行文件用 shell 模块都没问题。

取回文件出了问题,每台服务器的文件名都是不一样的。

取回文件使用 fetch 模块。测试了如下语句,行不通:

ansible all -m fetch -a "src=/tmp/log/* dest=/tmp/"

疯子哥让我去看官方文档。

http://docs.ansible.com/ansible/latest/fetch_module.html#examples
 fetch:
      src: /tmp/{{ inventory_hostname }}.txt
      dest: /tmp/ss-{{ inventory_hostname }}
      flat: yes

使用这个就可以从所有服务器上下载文件。解释一下:

//fetch 是调用这个模块
 fetch:
 //src 是远程服务器的路径,这里的 inventory_hostname 就是填在 /etc/ansible/hosts 文件里面的内容。比如说 hosts 文件你填的是 192.168.1.3
// 那这里的 {{inventory_hostname}}.txt 就是 192.168.1.3.txt
      src: /tmp/{{ inventory_hostname }}.txt
      dest: /tmp/ss-{{ inventory_hostname }}
      flat: yes

发现问题了吗?对,这个脚本要求你的文件名必须包含 inventory_hostname ,

如果没有怎么办呢?使用下面的脚本:

  tasks:
    - name: fucking
      find:
        paths: /tmp/log/
        patterns: "*"
        recurse: no
      register: file_2_fetch

    - name: fuck your bitch
      fetch:
        src: "{{ item.path }}"
        dest: /tmp/
        flat: yes
      with_items: "{{ file_2_fetch.files }}"

解释一下:

首先调用 find,paths 即你存放文件的路径。 patterns 即你要跟的关键字,这里是 *,即通配符,匹配所有文件。你可以写为 *.txt ,匹配所有 txt 文件。
第二行调用 fetch ,ansible 的 Fetches a file from remote nodes ,
src 即上面的find 查到出来的结果。

执行结果如下:

[root@master ~]# ansible-playbook main.yaml 

PLAY [test] ************************************************************************************

TASK [Gathering Facts] *************************************************************************
ok: [192.168.153.22]

TASK [fucking] *********************************************************************************
ok: [192.168.153.22]

TASK [fuck your bitch] *************************************************************************
ok: [192.168.153.22] => (item={u'uid': 0, u'woth': False, u'mtime': 1516180038.2560008, u'inode': 34964981, u'isgid': False, u'size': 0, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/tmp/log/192.168.153.22.txt', u'xusr': False, u'atime': 1516181632.1700034, u'isdir': False, u'ctime': 1516181291.6150029, u'isblk': False, u'wgrp': False, u'xgrp': False, u'dev': 64768, u'roth': True, u'isfifo': False, u'mode': u'0644', u'rusr': True})
ok: [192.168.153.22] => (item={u'uid': 0, u'woth': False, u'mtime': 1516182493.8110049, u'inode': 1762530, u'isgid': False, u'size': 0, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/tmp/log/1.txt', u'xusr': False, u'atime': 1516182504.3540049, u'isdir': False, u'ctime': 1516182493.8110049, u'isblk': False, u'wgrp': False, u'xgrp': False, u'dev': 64768, u'roth': True, u'isfifo': False, u'mode': u'0644', u'rusr': True})
changed: [192.168.153.22] => (item={u'uid': 0, u'woth': False, u'mtime': 1516182519.4070048, u'inode': 1762531, u'isgid': False, u'size': 0, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/tmp/log/2.pdf', u'xusr': False, u'atime': 1516182519.4070048, u'isdir': False, u'ctime': 1516182519.4070048, u'isblk': False, u'wgrp': False, u'xgrp': False, u'dev': 64768, u'roth': True, u'isfifo': False, u'mode': u'0644', u'rusr': True})

PLAY RECAP *************************************************************************************
192.168.153.22             : ok=3    changed=1    unreachable=0    failed=0   

[root@master ~]# ls /tmp/
192.168.153.22.txt  1.txt  2.pdf

自动化运维工具Ansible使用教程

一、简介

1.1 基本概念

ansible是一个基于python开发的轻量级自动化运维管理工具,可以用来批量执行命令,安装程序,支持playbook编排。它通过ssh协议来连接主机,去中心化,相对比puppet和saltstack无需安装客户即可实现文件传输、命令执行、应用部署、配置管理、任务编排等,显得更为简单与轻量。ansible只是提供一种框架,其基于模块工作的,本身没有批量部署。

1.2 核心组件

(1)、连接插件connection plugins:负责和被监控端实现通信;

(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;

(3)、各种模块核心模块、command模块、自定义模块;

(4)、借助于插件完成记录日志邮件等功能;

(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。

1.3 工具特性

(1)、no agents:不需要在被管控主机上安装任何客户端;

(2)、no server:无服务器端,使用时直接运行命令即可;

(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;

(4)、yaml,not code:使用yaml语言定制剧本playbook;

(5)、ssh by default:基于SSH工作;

(6)、strong multi-tier solution:可实现多级指挥。

1.4 流程架构

未分类

1.5 优缺点

优点:

  • 轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
  • 批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
  • 使用python编写,维护更简单;
  • 使用push方式,控制节点向其他节点推方式,可先测试变更,方便控制管理。

缺点:

基于ssh,串行,故超过500台主机效率较低;

二、安装部署

名称  主机名         IP地址

A主机 ansible-A   172.20.4.10
B主机 ansible-B   172.20.4.11
C主机 ansible-C   172.20.4.12

2.1 Ansible安装

安装方式可使用源码编译安装,也可以更新yum源后yum安装,由于依赖较多模块,编译安装易出现异常,此次采用yum安装,CentOS 6.x安装epel源后,直接可以yum安装,python版本2.6以上,在各个节点均需要安装

rpm -ivh http://mirrors.sohu.com/Fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install ansible -y

2.2 各主机SSH互信

例如:在A主机执行以下命令,将公钥发送到B主机

ssh-keygen -t rsa                             #创建公钥与私钥
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]            #将公钥传输给对端服务器

此时A服务器可以免密码登录B服务器

未分类

同样方式,可以做A到C主机,如果控制端为B主机,需要B反向将公钥发布到A主机,实现互信。

2.3 命令参数介绍

Usage: ansible [options]

Options:
-a MODULE_ARGS, --args=MODULE_ARGS                 #制定调用的模块(ansible-doc查看模块)
module arguments
--ask-vault-pass      ask for vault password         #加密文件
-B SECONDS, --background=SECONDS              #后台等待多少秒
run asynchronously, failing after X seconds
(default=N/A)
-C, --check           don't make any changes; instead, try to predict some      #不执行命令,值执行命令检查
of the changes that may occur
-D, --diff            when changing (small) files and templates, show the
differences in those files; works great with --check
-e EXTRA_VARS, --extra-vars=EXTRA_VARS                                  #调用外部变量
set additional variables as key=value or YAML/JSON
-f FORKS, --forks=FORKS                                        #一次执行并发的连接数
specify number of parallel processes to use
(default=5)
-h, --help            show this help message and exit
-i INVENTORY, --inventory-file=INVENTORY                                #调用的hosts文件
specify inventory host path
(default=/etc/ansible/hosts) or comma separated host
list.
-l SUBSET, --limit=SUBSET                                      #限定主机列表中的某台主机执行
further limit selected hosts to an additional pattern
--list-hosts          outputs a list of matching hosts; does not execute        #列出直接列表中主机
anything else
-m MODULE_NAME, --module-name=MODULE_NAME                              #调用执行模块
module name to execute (default=command)
-M MODULE_PATH, --module-path=MODULE_PATH
specify path(s) to module library (default=None)
--new-vault-password-file=NEW_VAULT_PASSWORD_FILE 
new vault password file for rekey
-o, --one-line        condense output
--output=OUTPUT_FILE  output file name for encrypt or decrypt; use - for
stdout
-P POLL_INTERVAL, --poll=POLL_INTERVAL
set the poll interval if using -B (default=15)
--syntax-check        perform a syntax check on the playbook, but do not
execute it
-t TREE, --tree=TREE  log output to this directory
--vault-password-file=VAULT_PASSWORD_FILE
vault password file
-v, --verbose         verbose mode (-vvv for more, -vvvv to enable          #命令输出详细输出
connection debugging)
--version             show program's version number and exit
Connection Options:
control as whom and how to connect to hosts

-k, --ask-pass      ask for connection password                          #需要安装sshpass  输入密码
--private-key=PRIVATE_KEY_FILE, --key-file=PRIVATE_KEY_FILE
use this file to authenticate the connection
-u REMOTE_USER, --user=REMOTE_USER                                       #ssh执行命令的用户,默认为当前执行ansible的用户
connect as this user (default=None)
-c CONNECTION, --connection=CONNECTION
connection type to use (default=smart)
-T TIMEOUT, --timeout=TIMEOUT                                      #执行命令的超时时间 (default=10)
override the connection timeout in seconds
(default=10)
--ssh-common-args=SSH_COMMON_ARGS
specify common arguments to pass to sftp/scp/ssh (e.g.
ProxyCommand)
--sftp-extra-args=SFTP_EXTRA_ARGS
specify extra arguments to pass to sftp only (e.g. -f,
-l)
--scp-extra-args=SCP_EXTRA_ARGS
specify extra arguments to pass to scp only (e.g. -l)
--ssh-extra-args=SSH_EXTRA_ARGS
specify extra arguments to pass to ssh only (e.g. -R)

Privilege Escalation Options:
control how and which user you become as on target hosts

-s, --sudo          run operations with sudo (nopasswd) (deprecated, use
become)            #sudo
-U SUDO_USER, --sudo-user=SUDO_USER      #sudo
desired sudo user (default=root) (deprecated, use
become)
-S, --su            run operations with su (deprecated, use become)
-R SU_USER, --su-user=SU_USER                                       #su 的时候切换到那个用户
run operations with su as this user (default=root)
(deprecated, use become)

2.4 配置相关文件

修改主机文件inventory:,此文件定义执行命令的主机列表

未分类

设置ansible.cfg参数

inventory =/etc/ansible/hosts             #定义资源清单inventory文件的位置,一般保持默认
library =/usr/share/my_modules/           #library指向ansible模块的目录,一般保持默认
forks =10                                 #设置多少个进程同时工作
sudo_user=root                            #设置默认执行命令的用户,也可在playbook中重新设置此参数
remote_port=22                            #制定连接被管理的管理端口,默认为22
timeout =10                               #设置SSH连接的超时时间间隔,单位为秒

2.5 测试

ansible agent -m command -a "touch /tmp/aaa" -vvv
#-m  使用command模块  -a 使用command里面支持的命令参数 -vvv 查看详细过程

未分类

三、模块介绍

ansible模块较多,对应可以查看相关文档,此处列出一下日常工作中常用的模块

【copy】模块

ansible agent -m copy -a "src=/root/test.sh dest=/tmp"

【file】

调用-s 参数,需要客户端能够无密码使用sudo命令;

ansible agent -m file -a "dest=/tmp/test.sh mode=755 owner=root group=root" -s

【script】

ansible agent -m script -a "/tmp/test.sh"

【shell】创建用户

ansible agent -m shell -a "/tmp/test.sh" 

【group】创建组

ansible agent -m group -a "name=test1 state=present" -s

【user】

ansible agent -m user -a "name=xuel home=/home/xuel state=present" -s

【yum】

可以提供的status:absent,present,installed,removed,latest
ansible agent -m yum -a "name=httpd state=latest" -s

【server】

可以提供的status:running,started,stopped,restarted,reloaded

【cron】

ansible agent -m cron -a 'name="my job" minute=*/1 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate time1.aliyun.com"'

【get_url】

ansible agent -m get_url -a "url=http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm dest=/tmp"

【synchronize】需要安装rsync

ansible agent -m synchronize -a "src=/root/test.file dest=/tmp"
模块默认使用的为推送push,如果想使用pull功能需添加mode=pull
ansible agent -m synchronize -a "mode=pull src=/tmp/test.file dest=/root/"

【ini_file】

ansible agent -m ini_file -a "dest=/tmp/test.ini section=Mongo option=Host value=127.0.0.1"

该模块Python需要安装ConfigParser

四、ansible-playbook介绍

4.1 核心组件

hosts             #执行的远程主机列表
tasks             #任务集
varniables        #内置变量或自定义变量
templates         #可替换模版
handlers          #触发操作

4.2 命令

Usage: ansible-playbook playbook.yml
ansible-playbook test1.yml             #执行剧本
ansible-vault encrypt test1.yml         #加密剧本
ansible-vault decrypt test1.yml         #加密剧本
ansible-vault view test1.yml            #加密剧本 

4.3 YAML语法

1.“—”顶行首写

2.#代码注释

3.缩进统一,不可混用空格与tab

4.缩进级别椅子

5.区分大小写

6.k/v值可以同行写也可换行写,同行使用:分割,换行需要-分割

7.一个网址的功能代码需要最少的元素包括name:task

8.一个name只能包括一个task

4.4 安装并启动mysql playbook实例

---
- hosts: agent
  remote_user: root
  tasks:
  - name: install mysql-server
    yum: name=mysql-server state=present
  - name: start mysql-server
    service: name=mysqld state=started
  - name: check mysql service
    shell: ps -ef |grep mysqld

执行次playbook将mysql数据库安装到agent服务分组里:

未分类

未分类

如何离线安装ansible

在有网络的情况下,ansible还是很好安装的。但如果你的生产环境有很严格的网络要求,不能够连接外网,你又需要在生产环境上使用ansible。那只有使用离线的方式来安装。但很可惜的是,ansible官方提供的安装包,比如rpm包,并没有包含它所需要的依赖,直接安装是无法使用的。因此需要找个方法自己把所有的依赖解决。

解决的方法有很多,这里列一个比较简单的。首先,找一台能够上网的机器,并且拥有和你的生产服务器有相同linux版本(你的开发环境或测试环境一定有这样的机器)。然后在上头安装对应的工具(二选一):

  • yum-downloadonly
  • Yumdownloader

yum-downloadonly

安装 “downloadonly” 插件:

(RHEL5)
# yum install yum-downloadonly

(RHEL6)
# yum install yum-plugin-downloadonly

在运行yum install时,使用–downloadonly”选项:

yum install --downloadonly --downloaddir=<directory> <package>

确认你需要的package和对应的dependency包已经被保存在了你设置的下载目录。

注意:

  • 在使用插件之前,请检查/etc/yum/pluginconf.d/downloadonly.conf以确认此插件是“enabled = 1”
  • 这仅适用于“yum install / yum update”而不适用于“yum groupinstall”。你可以使用“yum groupinfo”来确认group中的包含软件包,再用yum install下载。
  • 如果仅指定包名称,则下载最新的可用包(如sshd)。否则,您可以指定完整的软件包名称和版本(例如httpd-2.2.3-22.el5)。
  • 如果不使用–downloaddir选项,文件将默认保存在/var/cache/yum/inrhel-{arch}-channel/packages
  • 如果需要,您可以使用相同的命令下载多个软件包。

Yumdownloader

如果你想获取已安装的软件包,那么请使用yumdownloader。

安装yum-utils软件包:

# yum install yum-utils

运行命令,然后运行所需的软件包:

# yumdownloader <package>

注意:

  • 包默认直接保存在当前工作目录中; 也可以使用–destdir选项来指定一个存储位置。
  • 如果您需要下载依赖关系,请务必添加–resolve。

ansible-playbook组件解析及操作全解

一、ansible-playbook介绍

playbook是由一个或多个”play”组成的列表。play的主要功能在于将事先归为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来将,所谓的task无法是调用ansible的一个module。将多个paly组织在一个playbook中,即可以让他们联通起来按事先编排的机制同唱一台大戏。

1、playbook基础组件

hosts playbook中的每一个paly的目的都是为了让某个或某些以某个指定用户的身份执行任务。hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分割主机组。

user remote_user则用于指定远程主机上的执行任务的用户。

任务列表:

play的主体部分是task list. task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。

action

任务执行过程

handlers

用于当前关注的资源发生变化时采取一定指定的操作

2、实例

[root@node1 playbook]# cat web.yml
- hosts: test  \主机组,在/etc/ansible/hosts定义
  remote_user: root  \远端执行任务的用户
  tasks: \任务
  - name: install httpd  \任务描述
    command: yum -y install httpd  \调用ansible的command模块安装httpd
  - name: provide httpd.conf \任务描述
copy: src="/root/httpd.conf" dest="/etc/httpd/conf/httpd.conf" \调用ansible的copy模块,httpd安装完成后将事先准备好的httpd.conf文件复制到/etc/httpd/conf目录下
    tags: conf  \给此任务打标记,可单独执行标记的任务,使用 ansible-playbook -C 命令执行
    notify:  \文件内容变更通知
    - server restart  \通知到指定的任务
  - name: server start  \任务描述
    service: name=httpd state=started enabled=true \调用ansible的service模块的属性定义安装完成httpd以后httpd服务的管理
  handlers: \定义接受关注的资源变化后执行的动作
  - name: server restart  \任务描述
    service: name=httpd state=restarted   \当关注的资源发生变化后调用service模块,采取的响应的动作

执行过程如下:

[root@node1 playbook]# ansible-playbook web.yml 

PLAY [test] ******************************************************************* 

GATHERING FACTS *************************************************************** 
ok: [172.16.2.13]

TASK: [install httpd] ********************************************************* 
changed: [172.16.2.13]

TASK: [provide httpd.conf] **************************************************** 
changed: [172.16.2.13]

TASK: [server start] ********************************************************** 
changed: [172.16.2.13]

NOTIFIED: [server restart] **************************************************** 
changed: [172.16.2.13]

PLAY RECAP ******************************************************************** 
172.16.2.13                : ok=5    changed=4    unreachable=0    failed=0

二、ansible的roles介绍:

ansible的roles用于层次性、结构化地组织palybook。roles能够根据层次型结构自动装载变量文件、tasks及handlers等。要使用roles只需要playbook中使用include指令即可。

rules的组成:

root@node1 playbook]# tree  roles/
roles/ \ansible所有的信息都放到此目录下面对应的目录中
└── nginx  \角色名称
    ├── default  \为当前角色设定默认变量时使用此目录,应当包含一个main.yml文件;
    ├── files  \存放有copy或script等模块调用的文件
    ├── handlers \此目录总应当包含一个main.yml文件,用于定义各角色用到的各handler
    ├── meta \应当包含一个main.yml,用于定义角色的特殊设定及其依赖关系;1.3及以后版本支持
    ├── tasks \至少包含一个名为main.yml的文件,定义了此角色的任务列表,可使用include指令
    ├── templates \template模块会自动在此目录中寻找Jinja2模板文件
    └── vars  \应当包含一个main.yml文件,用于定义此角色用到的变量

roles介绍完了,那么我们就利用ansible的roles来配置nginx

1、首先按照上面的要求创建要用到的目录

[root@node1 playbook]# mkdir -pv roles/nginx/{tasks,files,templates,handlers,vars,meta,default}

2、准备nginx配置文件

准备nginx.conf配置文件,使用模板文件配置

[root@node1 playbook]# cd roles/nginx/templates/
[root@node1 ~]# ansible all -m setup | grep ansible_processor_cores
        "ansible_processor_cores": 1,  \获取ansible的要调用的相关函数
[root@node1 playbook]# cd roles/nginx/templates/   \模板文件一定要放到此目录     
[root@node1 templates]# vim nginx.conf 
worker_processes {{  ansible_processor_cores }};  \调用获取到的函数

准备nginx的default.conf文件

[root@node1 playbook]# ls -l roles/nginx/files/
-rw-r--r--. 1 root root 1290 Nov 12  2014 default.conf

3、准备nginx的rpm包

[root@node1 playbook]# ls -l  roles/nginx/files/
-rw-r--r--. 1 root root   1290 Nov 12  2014 default.conf
-rw-r--r--. 1 root root 319456 Mar 29 20:44 nginx-1.4.7-1.el6.ngx.x86_64.rpm

4、在tasks目录中配置任务列表

[root@node1 playbook]# cd  roles/nginx/tasks/
[root@node1 tasks]# vim  main.yml 
  - name: copy nginx.rpm
    copy: src=nginx-1.4.7-1.el6.ngx.x86_64.rpm  dest=/tmp/nginx-1.4.7-1.el6.ngx.x86_64.rpm
  - name: install nginx
    shell: yum -y  install /tmp/nginx-1.4.7-1.el6.ngx.x86_64.rpm
  - name: provides nginx.conf
    template: src=nginx.conf  dest=/etc/nginx/nginx.conf
    tags: nginxconf
    notify:
    - server restart
  - name: provides default.conf
    copy: src=default.conf dest=/etc/nginx/conf.d/default.conf 
    tags: nginxconf
  - name: server start
    service: name=nginx enabled=true state=started

5、在handlers目录中配置定义handler信息

[root@node1 playbook]# cd roles/nginx/handlers/
[root@node1 handlers]# vim  main.yml 
- name: server restart
  service: name=nginx  state=restarted

6、在roles同一级目录中创建site.yml文件

[root@node1 playbook]# cat site.yml 
- hosts: nginx
  remote_user: root
  roles:
  - nginx

7、应用配置

[root@node1 playbook]# ansible-playbook site.yml 

PLAY [nginx] ****************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [172.16.2.13]

TASK: [nginx | copy nginx.rpm] ************************************************ 
ok: [172.16.2.13]

TASK: [nginx | install nginx] ************************************************* 
changed: [172.16.2.13]

TASK: [nginx | provides nginx.conf] ******************************************* 
changed: [172.16.2.13]

TASK: [nginx | provides default.conf] ***************************************** 
changed: [172.16.2.13]

TASK: [nginx | server start] ************************************************** 
changed: [172.16.2.13]

NOTIFIED: [nginx | server restart] ******************************************** 
changed: [172.16.2.13]

PLAY RECAP ******************************************************************** 
172.16.2.13                : ok=7    changed=5    unreachable=0    failed=0

8、在node2主机上查看nginx是否已启动

[root@node2 ~]# ss -tpln | grep 80
LISTEN     0      128                       *:80                       *:*      users:(("nginx",8934,8),("nginx",8936,8))

9、roles目录总体结构

[root@node1 playbook]# tree roles/
roles/
└── nginx
    ├── default
    ├── files
    │  ├── default.conf
    │  └── nginx-1.4.7-1.el6.ngx.x86_64.rpm
    ├── handlers
    │  └── main.yml
    ├── meta
    ├── tasks
    │  └── main.yml
    ├── templates
    │  └── nginx.conf
    └── vars

Ansible基于服务树进行分组全量接口调用

Ansible APi

说明

品茶:代码是基于我们的服务树结构进行构建,如果需要自己构建相应服务树则可以根据group host inventory进行自行构建。我们中带有中文,所以命令行模式需要2.0才可以调中文,1.9需要改代码。直接调模块不受影响。

Info

ansible2.0更贴近于ansible cli的常用命令执行方式,不同于上一版本只能发送单个命令或playbook;而更推荐用户在调用ansibleAPI的时候,将playbook的每个task拆分出来,获取每个task的结果。能够跟灵活处理在执行批量作业过程中的各种反馈。

Info 2

将执行操作的队列模型,包含各类环境参数设置,归结到“ansible.executor.task_queue_manager”类中
将执行过程中的各个task的设置,或者说playbook中的编排内容,归结到“ansible.playbook.play”中

Import Packge

from collections import namedtuple #有命元组
from ansible.parsing.dataloader import DataLoader #数据解析
from ansible.vars import VariableManager # 变量管旦
from ansible.inventory import Inventory # 主机配置信息
from ansible.playbook.play import Play # 剧本
from ansible.executor.task_queue_manager import TaskQueueManager # 任务消息队列
from ansible.plugins.callback import CallbackBase #回调

Info

  • inventory –> 由ansible.inventory模块创建,用于导入inventory文件
  • variable_manager –> 由ansible.vars模块创建,用于存储各类变量信息
  • loader –> 由ansible.parsing.dataloader模块创建,用于数据解析
  • options –> 存放各类配置信息的数据字典
  • passwords –> 登录密码,可设置加密信息
  • stdout_callback –> 回调函数

Example Code

# #coding:utf8
import json
import sys

from ansible.runner import Runner
from ansible.inventory.group import Group
from ansible.inventory.host import Host
from ansible.inventory import Inventory
from ansible import playbook
from ansible import callbacks
from ansible import utils

from cmdb import groups


class CmdbInventory(object):
    '''
    Get ansible.inventory for cmdb parse tree
    '''

    def __init__(self):

        self.cmdbs = groups()
        self.inventory = self.init_inventory()

    def init_inventory(self, inventory=Inventory(host_list=[])):
        '''

        :param inventory: default param, init cmdb Tree info.
        :return: ansible.inventory type
        '''

        for name in self.cmdbs:
            if name == "_meta": # 主机变量,暂不处理
                pass
            g = Group(name=name)

            # 设置组环境变量
            if self.cmdbs[name].get("vars", None):
                vars = self.cmdbs[name]["vars"]
                for k,v in vars.iteritems():
                    g.set_variable(k, v)

            # 添加主机进主机组
            if self.cmdbs[name].get("hosts", None):
                hosts = self.cmdbs[name]["hosts"]
                for host in hosts:
                    h = Host(name=host)
                    g.add_host(h)

            inventory.add_group(g)

        # 处理子组
        for name in self.cmdbs:
            if self.cmdbs[name].get("children", None):
                children = self.cmdbs[name]["children"]
                for child in children:
                    g = inventory.get_group(name)
                    child = inventory.get_group(child)
                    g.add_child_group(child)

        # 处理主机的环境变量
        hostvars = self.cmdbs.get("_meta",{}).get("hostvars", {})
        if hostvars:
           for host in hostvars:
               inve_host = inventory.get_host(host)
               for k, v in hostvars[host].iteritems():
                   inve_host.set_variable(k, v)

        return inventory

class Ansible(object):
    def __init__(self, transport="paramiko", module_name="ping",
                 module_args="", pattern="", remote_user="",
                 remote_pass="", play_book=False, yml_path=""):
        '''
        Run a ansible task
        :param transport: paramiko, ssh, smart
        :param module_name: ansible module name
        :param module_args: ansible module args
        :param pattern: ansible pattern
        :param remote_user: transport user
        :param remote_pass: transport password
        :return: ansible task result
        '''

        if not remote_user or not remote_pass:
            raise ValueError("Ansible class need params remote_user, remote_pass")

        if play_book:
            if not yml_path:
                raise ValueError("playbook need params yml_path")
        else:
            if not module_name or not pattern:
                raise ValueError("Ad-hoc need params module_name, pattern")

        if transport not in ("paramiko", "ssh", "smart"):
            raise ValueError("params transport not in paramiko, ssh, smart.")

        self.transport = transport
        self.module_name = module_name
        self.module_args = module_args
        self.pattern = pattern.decode("utf-8")  # 这是因为中文问题
        self.remote_user = remote_user
        self.remote_pass = remote_pass
        self.play_book = play_book
        self.yml_path = yml_path

        # A 通过解析方式(这是一种单独的方式)
        # ci = CmdbInventory()
        # inventory = ci.inventory

        # B 通过脚本方式(这是一种全量的方式,可通过修改文件名)
        self.inventory = Inventory(host_list="cmdb.py")

    def task(self):
        '''Ansible Ad-Hoc'''

        try:
            runner = Runner(
                transport=self.transport,
                module_name=self.module_name,
                module_args=self.module_args,
                pattern=self.pattern,
                forks=10,
                inventory=self.inventory,
                remote_user=self.remote_user,
                remote_pass=self.remote_pass
            )
            result =  runner.run()
            return True, result
        except Exception as e:
            return False, str(e)

    def playbook(self):
        stats = callbacks.AggregateStats()
        playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = callbacks.PlaybookRunnerCallbacks(stats=stats, verbose=utils.VERBOSITY)

        # B 通过脚本方式(这是一种全量的方式,可通过修改文件名)
        inventory = Inventory(host_list="cmdb.py")

        pb = playbook.PlayBook(
            inventory=self.inventory,
            playbook=self.yml_path,
            stats=stats,
            callbacks=playbook_cb,
            runner_callbacks=runner_cb,
            check=True,
            transport=self.transport,
            remote_user=self.remote_user,
            remote_pass=self.remote_pass
        )

        result = pb.run()
        return True, result

if __name__ == "__main__":
    ansible = Ansible(remote_user="",
                      remote_pass="",
                      play_book=True,
                      yml_path="playbooks/ping.yml")
    # result = ansible.task()
    result = ansible.playbook()
    print json.dumps(result, indent=4)