ansible4:playbook介绍及使用

简介

  上一篇文章讲了模块的基本使用,我们可以在命令行使用对应模块执行一些远程的操作,但是在工作中会有一些重复的、稍复杂的一些操作,比如主机初始化、批量安装软件并配置好等等,我们不可能使用一条一条敲远程命令,得准备一个类似于脚本一样的东西,将各种步骤组合在一起,随拿随用。ansible的剧本(playbook)就是这么个作用,编写剧本需要遵循yaml语法,下面我们举例理解一下。

基础使用

  1. 在远端创建一个目录,并在目录内创建文件。

    # 根据前面的知识,需要用到如下命令:
      0 19:38:56 root@ck-ansible,172.16.2.9:~ # ansible ck-node1 -m file -a 'name=/root/mmm state=directory'
      0 19:39:01 root@ck-ansible,172.16.2.9:~ # ansible ck-node1 -m file -a 'name=/root/mmm/a.txt state=touch'
    # 换成剧本来写就是:
      0 19:41:59 root@ck-ansible,172.16.2.9:~ # mkdir -p /server/ops_ansible
      0 19:42:05 root@ck-ansible,172.16.2.9:~ # cd /server/ops_ansible
      0 19:42:07 root@ck-ansible,172.16.2.9:/server/ops_ansible # vim test1.yaml
    ---			# 以‘---’开头表明这是一个yaml文件,不写也没有影响。
    - hosts: ck-node1
      tasks:
      - name: create directory mmm
        file: name=/root/mmm state=directory
      - name: touch a.txt
        file: name=/root/mmm/a.txt state=touch
    # 检查yaml的语法是否正确。
      0 19:47:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook --syntax-check test1.yaml
    playbook: test1.yaml		# 返回名称代表语法没问题。
    # 测试执行,模拟一下过程不是真正执行。
      0 19:48:11 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook --check test1.yaml
    # 正式执行。
      0 19:49:12 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test1.yaml
    

handlers说明

  1. 根据上面任务的执行结果去决定handlers里面任务是否需要执行,它与tasks平级,缩进一致。

      0 19:54:37 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test2.yaml
    ---
    - hosts: ck-node1
      tasks:
      - name: create directory nnn
        file: name=/root/nnn state=directory
        notify: touch a.txt
    
      handlers:
      - name: touch a.txt
        file: name=/root/nnn/b.txt state=touch
    # 在远程主机上将/root/nnn目录手动创建出来。
      0 19:54:39 root@ck-node1,172.16.15.21:~ # mkdir -p /root/nnn
    # 在ansible主机执行playbook。
      0 19:57:15 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test2.yaml
      0 19:57:55 root@ck-node1,172.16.15.21:~ # ll /root/nnn
    总用量 0
    
  2. handlers的执行顺序与其定义的顺序有关,与被“notify”的顺序无关。

      0 20:13:33 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test3.yaml 
    ---
    - hosts: ck-node1
      tasks:
      - name: create directory yyy
        file: name=/root/yyy state=directory
        notify: touch b.txt
      - name: create directory xxx
        file: name=/root/xxx state=directory
        notify: touch a.txt
    
      handlers:
      - name: touch a.txt
        file: name=/root/xxx/a.txt state=touch
      - name: touch b.txt
        file: name=/root/yyy/b.txt state=touch
      0 20:15:05 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test3.yaml
    
    PLAY [ck-node1] **************************************************************************************************************************
    
    TASK [Gathering Facts] *******************************************************************************************************************
    ok: [ck-node1]
    
    TASK [create directory yyy] **************************************************************************************************************
    changed: [ck-node1]
    
    TASK [create directory xxx] **************************************************************************************************************
    changed: [ck-node1]
    
    RUNNING HANDLER [touch a.txt] ************************************************************************************************************
    changed: [ck-node1]
    
    RUNNING HANDLER [touch b.txt] ************************************************************************************************************
    changed: [ck-node1]
    
    PLAY RECAP *******************************************************************************************************************************
    ck-node1                   : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
    
  3. 可以通过 meta 模块的 flush handlers 让 handlers 立即执行,而非按照配置的顺序。

      0 20:18:20 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test4.yaml 
    ---
    - hosts: ck-node1
      tasks:
      - name: create directory yyy
        file: name=/root/yyy state=directory
        notify: touch b.txt
      - meta: flush_handlers
      - name: create directory xxx
        file: name=/root/xxx state=directory
        notify: touch a.txt
    
      handlers:
      - name: touch a.txt
        file: name=/root/xxx/a.txt state=touch
      - name: touch b.txt
        file: name=/root/yyy/b.txt state=touch
      0 20:18:26 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test4.yaml
    
    PLAY [ck-node1] **************************************************************************************************************************
    
    TASK [Gathering Facts] *******************************************************************************************************************
    ok: [ck-node1]
    
    TASK [create directory yyy] **************************************************************************************************************
    changed: [ck-node1]
    
    RUNNING HANDLER [touch b.txt] ************************************************************************************************************
    changed: [ck-node1]
    
    TASK [create directory xxx] **************************************************************************************************************
    changed: [ck-node1]
    
    RUNNING HANDLER [touch a.txt] ************************************************************************************************************
    changed: [ck-node1]
    
    PLAY RECAP *******************************************************************************************************************************
    ck-node1                   : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    
  4. 一个notify可以指定多个handlers。

      0 20:23:19 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test5.yaml
    ---
    - hosts: ck-node1
      tasks:
      - name: create directory zzz
        file: name=/root/zzz state=directory
        notify: touch txt
    
      handlers:
      - name: touch a.txt
        listen: touch txt
        file: name=/root/zzz/a.txt state=touch
      - name: touch b.txt
        listen: touch txt
        file: name=/root/zzz/b.txt state=touch
      0 20:23:21 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook test5.yaml
    
    PLAY [ck-node1] **************************************************************************************************************************
    
    TASK [Gathering Facts] *******************************************************************************************************************
    ok: [ck-node1]
    
    TASK [create directory zzz] **************************************************************************************************************
    changed: [ck-node1]
    
    RUNNING HANDLER [touch a.txt] ************************************************************************************************************
    changed: [ck-node1]
    
    RUNNING HANDLER [touch b.txt] ************************************************************************************************************
    changed: [ck-node1]
    
    PLAY RECAP *******************************************************************************************************************************
    ck-node1                   : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    

tags说明

  1. 一般一个playbook中会配置多个任务,当我们只需要执行其中某个任务时,就需要用到tag了。

      0 20:26:30 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test6.yaml
    ---
    - hosts: ck-node1
      tasks:
      - name: create directory aaa
        file: name=/root/aaa state=directory
        tags: tk-a
      - name: create directory bbb
        file: name=/root/bbb state=directory
        tags: tk-b
      - name: create directory ccc
        file: name=/root/ccc state=directory
        tags: tk-c
      0 20:26:33 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook --tags=tk-b test6.yaml
    
  2. tags指定不想执行的任务。

      0 20:26:56 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook --skip-tags=tk-a,tk-b test6.yaml
    
  3. 添加tag有三种语法格式:

    # 语法一:
    tags:
      - t1
      - t2
    # 语法二:
    tags: t1,t2
    # 语法三:
    tags: ['t1','t2']
    
  4. ansible内置了5个tags。

    # always:tags包含always的任务总是会被执行,除非执行命令时使用--skip-tags跳过这个tags。
      0 20:31:35 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test7.yaml
    ---
    - hosts: ck-node1
      tasks:
      - name: create directory aaa
        file: name=/root/aaa state=directory
        tags: tk-a
      - name: create directory bbb
        file: name=/root/bbb state=directory
        tags: tk-b
      - name: create directory ccc
        file: name=/root/ccc state=directory
        tags: tk-c,always
      0 20:31:36 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook --skip-tags=tk-b test7.yaml
    # nerver(2.5版本后新加入的tag)与always的作用相反,不举例。
    # targged、untagged。
      0 20:32:03 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook --tags=tagged test7.yml		# 表示只执行有tag的任务。
      0 20:34:52 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook --skip-tags=tagged test7.yml 	# 表示跳过包含标签的任务,即使任务包含always标签。
      0 20:35:24 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook --tags=untagged test7.yml		# 表示只执行没有标签的任务,即使任务包含always标签。
      0 20:35:55 root@ck-ansible,172.16.2.9:/server/ops_ansible # ansible-playbook --skip-tags=untagged test7.yml		# 表示跳过没有标签的任务。
    # PS: always和never是作为标签值存在,targged、untagged、all则是直接被调用,all是默认标签,无需指定。
    


写作不易,转载请注明出处,谢谢~~

原文地址:https://www.cnblogs.com/ccbloom/p/15508575.html