Ansible之playbook拓展

  1、handlers和notify结合使用触发条件html

  handlers同tasks是属同级,至关于一个特殊任务列表,这些任务同前文说的tasks里的任务没有本质的不一样,用于当关注的资源发生变化时,才会采起必定的操做。notify此action可用于在每个play的最后被触发,这样可避免屡次有改变发生时都执行指定的操做,仅在全部的变化发生完成后一次性地执行指定操做,在notify中列出的操做称为handler,换句话说当所关注的资源发生变化时notify将调用handlers中定义的操做。其中notify所在任务就是被监控的任务资源变化的任务,notify能够调用多个handlers定义的操做,一个handlers里能够定义不少任务。node

---
- hosts: websers
  remote_user: root

  tasks:
    - name: create apache group
      group: name=apache gid=80 system=yes
    - name: create apache user
      user: name=apache uid=80 group=apache system=yes shell=/sbin/nologin home=/var/www/html 
    - name: install httpd
      yum: name=httpd
    - name: copy config file
      copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/
      notify: restart httpd service

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

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

  说明:notify后指定的名称必需要和handlers里的任务名称相同,如不一样handlers所定义的任务将不会执行,至关于没有notify调用handlers里的任务。python

  在某些状况下,咱们可能同时须要调用多个handlers,或者须要使用handlers其余handlers,ansible能够很简单的实现这些功能,以下所示nginx

  1)调用多个handlersweb

---
- hosts: websers
  remote_user: root

  tasks:
    - name: create apache group
      group: name=apache gid=80 system=yes
    - name: create apache user
      user: name=apache uid=80 group=apache system=yes shell=/sbin/nologin home=/var/www/html 
    - name: install httpd
      yum: name=httpd
    - name: copy config file
      copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/
      notify: 
        - restart httpd service
        - check httpd process

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

  handlers:
    - name: restart httpd service
      service: name=httpd state=restarted
    - name: check httpd process                                                                                      
      shell: /usr/bin/killall -0 httpd &> /tmp/httpd.log

  说明:调用多个handlers咱们须要在notify中写成列表的形式,一样咱们被触发的任务名称须要同handlers里的被调用的任务名称彻底相同shell

  2)handlers调用handlersapache

---
- hosts: websers
  remote_user: root

  tasks:
    - name: create apache group
      group: name=apache gid=80 system=yes
    - name: create apache user
      user: name=apache uid=80 group=apache system=yes shell=/sbin/nologin home=/var/www/html 
    - name: install httpd
      yum: name=httpd
    - name: copy config file
      copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/
      notify: restart httpd service

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

  handlers:
    - name: restart httpd service
      service: name=httpd state=restarted
      notify: check httpd process                                                                                    
    - name: check httpd process
      shell: /usr/bin/killall -0 httpd &> /tmp/httpd.log

  说明:handlers调用handlers,则直接在handlers中使用notify选项就能够。数组

在使用handlers咱们须要注意一下几点:bash

  1)handlers只有在其所在任务被执行时才会被运行,handlers定义的任务它不会像task任务那样,自动会从上至下依次执行,它只会被notify所在的任务发生状态改变时才会触发handlers 的任务执行,若是一个任务中定义了notify调用handlers,但因为条件的判断等缘由,该任务还没有执行,那么notify调用的handlers一样也不会执行。app

  2)handlers只会在play的末尾运行一次;若是想要在一个playbook的中间运行handlers,则须要使用meta模块来实现,如:-mate: flush_handlers

  2、playbook中变量的使用

ansible中变量的命名规范同其余语言或系统中变量命名规则很是相似。变量名以英文大小写字母开头,中间能够包含下划线和数字,ansible变量的来源有不少,具体有如下几点:

  1)ansible setup模块,这个模块能够从远程主机上获取不少远程主机的基本信息,它所返回的全部变量均可以直接调用,有关setup说明请参考本人博客http://www.javashuo.com/article/p-ppxguweq-gt.html

  2)在/etc/ansible/hosts中定义,此文件是ansible执行名时默认加载的主机清单文件,在里面除了可定义咱们要管理的主机外,咱们还能够定义针对单个主机定义单独的变量,咱们把针对单独某一台主机定义的变量叫作普通变量(也可叫作主机变量);还有一种变量它不是针对单独一个主机,它针对某一个组里的全部主机,咱们把这种变量叫作公共组变量。主机清单中定义的变量优先级是普通变量高于公共变量。

    2.1)主机变量,能够在主机清单中定义主机时为其添加主机变量以便于在playbook中使用,以下所示

[websers]
192.168.0.128 http_port=80 maxRequestsPerChild=808
192.168.0.218 http_port=81 maxRequestsPerChild=909

    2.2)主机组变量,组变量是指定赋予给指定组内全部主机上的在playbook中可以使用的变量,以下所示

[websers]
192.168.0.128 http_port=80 
192.168.0.218 http_port=81 
[websers:vars]
maxRequestsPerChild=909

  3)经过命令行指定变量(-e指定变量赋值,能够说多个但须要用引号引发或者一个变量用一个-e指定赋值),这种在命令行指定的优先级最高。以下所示

ansible-playbook -e 'package_name1=httpd package_name2=nginx' test_vars.yml

  4)在playbook中定义变量,最多见的定义变量的方法是使用vars代码块,以下所示

---
- hosts: websers
  remote_user: root
  vars:
    - abc: xxx 
    - bcd: aaa  

  5)在独立的变量yml文件中定义,在playbook中使用vars_files代码块引用其变量文件,以下所示

[root@test ~]#cat vars.yml 
---
package_name1: vsftpd
package_name2: nginx
[root@test ~]#cat test_vars.yml 
---
- hosts: websers
  remote_user: root
  vars_files:
    - vars.yml
  tasks:
    - name: install package1
      yum: name={{ package_name1 }}
    - name: install package2
      yum: name={{ package_name2 }}
[root@test ~]#

  6)在role中定义,这个后续说到角色在作解释

  变量的调用方式:第一种在playbook中使用变量须要用“{{}}”将变量括起来,表示括号里的内容是一个变量,且先后必须有空格,有时用“{{  variable_name }}"才生效;第二种是ansible-playbook -e 选项指定其变量,ansible-playbook -e "hosts=www user=xxxx" test.yml

  在主机清单中定义变量的方法虽然简单直观,可是当所须要定义的变量有不少时,而且被多台主机使用时,这种方法显得很是麻烦,事实上ansible的官方手册中也不建议咱们把变量直接定义到hosts文件中;在执行ansible命令时,ansible会默认会从/etc/ansible/host_vars/和/etc/ansible/group_vars/两个目录下读取变量定义文件,若是/etc/ansible/下没有以上这两个目录,咱们能够手动建立,而且能够在这两个目录下建立与hosts文件中的主机名或主机组同名的文件来定义变量。好比咱们要给192.168.0.218 这个主机定义个变量文件,咱们能够在/etc/ansible/host_vars/目录下建立一个192.168.0.218的空白文件,而后在文件中以ymal语法来定义所需变量便可。以下所示

[root@test ~]#tail -6 /etc/ansible/hosts 
## db-[99:101]-node.example.com
[websers]
192.168.0.128 
192.168.0.218 
[appsers]
192.168.0.217
[root@test ~]#cat /etc/ansible/host_vars/192.168.0.218 
---
file1: abc
file2: bcd
[root@test ~]#cat test.yml 
---
- hosts: 192.168.0.218
  remote_user: root
  
  tasks:
    - name: touch file1
      file: name={{ file1 }} state=touch
    - name: toch file2
      file: name={{ file2 }} state=touch
[root@test ~]#ansible-playbook test.yml 

PLAY [192.168.0.218] ************************************************************************************************

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

TASK [touch file1] **************************************************************************************************
changed: [192.168.0.218]

TASK [toch file2] ***************************************************************************************************
changed: [192.168.0.218]

PLAY RECAP **********************************************************************************************************
192.168.0.218              : ok=3    changed=2    unreachable=0    failed=0   

[root@test ~]#ansible 192.168.0.218 -m shell -a 'ls -l /root'
192.168.0.218 | SUCCESS | rc=0 >>
总用量 12
-rw-r--r--. 1 root   root    0 11月 17 16:49 abc
-rw-r--r--. 1 root   root    0 11月 17 16:49 bcd
drwxr-xr-x. 2 qiuhom root 4096 11月 11 19:18 scripts
drwxr-xr-x. 3 qiuhom root 4096 11月 11 19:28 test
-rw-r--r--. 1 root   root   57 11月 13 19:15 test_cron_file

[root@test ~]#

  说明:可看到咱们定义在/etc/ansible/host_vars/下的主机变量文件中的变量生效了。

同理,咱们要想针对某个组的主机定义一些变量,咱们只须要在/etc/ansible/group_vars/目录下建立与主机清单中的主机组同名的文件便可。

  3、使用高阶变量

  对于普通变量,例如由ansible命令行设定的,hosts文件中定义的以及playbook中定义的和变量文件中定义的,这些变量都被称为普通变量或者叫简单变量,咱们能够在playbook中直接用双大括号加变量名来读取变量内容;除此之外ansible还有数组变量或者叫作列表变量,以下所示:

[root@test ~]#cat vars.yml 
---
packages_list:
  - vsftpd
  - nginx
[root@test ~]#

  列表定义完成后咱们要使用其中的变量能够列表名加下标的方式去访问,有点相似shell脚本里的数组的使用,以下所示

[root@test ~]#cat test.yml 
---
- hosts: 192.168.0.218
  remote_user: root
  
  vars_files:
    - vars.yml
  tasks:
    - name: touch file
      file: name={{ packages_list[0] }} state=touch
    - name: mkdir dir
      file: name={{ packages_list[1] }} state=directory
[root@test ~]#

  说明:咱们要使用列表中的第一个元素变量,咱们能够写成vars_list[0],使用第二个变量则下标就是1,依此类推

[root@test ~]#ansible *218 -m shell -a 'ls -l /root'
192.168.0.218 | SUCCESS | rc=0 >>
总用量 12
-rw-r--r--. 1 root   root    0 11月 17 16:49 abc
-rw-r--r--. 1 root   root    0 11月 17 16:49 bcd
drwxr-xr-x. 2 qiuhom root 4096 11月 11 19:18 scripts
drwxr-xr-x. 3 qiuhom root 4096 11月 11 19:28 test
-rw-r--r--. 1 root   root   57 11月 13 19:15 test_cron_file

[root@test ~]#ansible-playbook test.yml 

PLAY [192.168.0.218] ************************************************************************************************

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

TASK [touch file] ***************************************************************************************************
changed: [192.168.0.218]

TASK [mkdir dir] ****************************************************************************************************
changed: [192.168.0.218]

PLAY RECAP **********************************************************************************************************
192.168.0.218              : ok=3    changed=2    unreachable=0    failed=0   

[root@test ~]#ansible *218 -m shell -a 'ls -l /root'
192.168.0.218 | SUCCESS | rc=0 >>
总用量 16
-rw-r--r--. 1 root   root    0 11月 17 16:49 abc
-rw-r--r--. 1 root   root    0 11月 17 16:49 bcd
drwxr-xr-x. 2 root   root 4096 11月 17 17:23 nginx
drwxr-xr-x. 2 qiuhom root 4096 11月 11 19:18 scripts
drwxr-xr-x. 3 qiuhom root 4096 11月 11 19:28 test
-rw-r--r--. 1 root   root   57 11月 13 19:15 test_cron_file
-rw-r--r--. 1 root   root    0 11月 17 17:23 vsftpd

[root@test ~]#

  说明:可看到咱们建立的文件和目录在目标主机已经生成

上面的用法是典型的python列表的用法,在python中读取列表中的元素就是用下标的表示来读取相应的元素的值。接下咱们将介绍另一种更为复杂的变量,它相似python中的字典几率,但比字典的维度要高,更像是二维字典。ansible内置变量ansible_eth0就是这样一种,它用来保存远端主机上面eth0接口的信息,包括ip地址和子网掩码等。以下所示

[root@test ~]#cat test.yml     
---
- hosts: 192.168.0.218
  remote_user: root
  
  tasks:
    - debug: var=ansible_eth0 
[root@test ~]#ansible-playbook test.yml 

PLAY [192.168.0.218] ************************************************************************************************

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

TASK [debug] ********************************************************************************************************
ok: [192.168.0.218] => {
    "ansible_eth0": {
        "active": true, 
        "device": "eth0", 
        "features": {
            "fcoe_mtu": "off [fixed]", 
            "generic_receive_offload": "on", 
            "generic_segmentation_offload": "on", 
            "highdma": "off [fixed]", 
            "large_receive_offload": "off [fixed]", 
            "loopback": "off [fixed]", 
            "netns_local": "off [fixed]", 
            "ntuple_filters": "off [fixed]", 
            "receive_hashing": "off [fixed]", 
            "rx_checksumming": "on", 
            "rx_vlan_filter": "on [fixed]", 
            "rx_vlan_offload": "on [fixed]", 
            "scatter_gather": "on", 
            "tcp_segmentation_offload": "on", 
            "tx_checksum_fcoe_crc": "off [fixed]", 
            "tx_checksum_ip_generic": "on", 
            "tx_checksum_ipv4": "off", 
            "tx_checksum_ipv6": "off", 
            "tx_checksum_sctp": "off [fixed]", 
            "tx_checksum_unneeded": "off", 
            "tx_checksumming": "on", 
            "tx_fcoe_segmentation": "off [fixed]", 
            "tx_gre_segmentation": "off [fixed]", 
            "tx_gso_robust": "off [fixed]", 
            "tx_lockless": "off [fixed]", 
            "tx_scatter_gather": "on", 
            "tx_scatter_gather_fraglist": "off [fixed]", 
            "tx_tcp6_segmentation": "off", 
            "tx_tcp_ecn_segmentation": "off", 
            "tx_tcp_segmentation": "on", 
            "tx_udp_tnl_segmentation": "off [fixed]", 
            "tx_vlan_offload": "on [fixed]", 
            "udp_fragmentation_offload": "off [fixed]", 
            "vlan_challenged": "off [fixed]"
        }, 
        "hw_timestamp_filters": [], 
        "ipv4": {
            "address": "192.168.0.218", 
            "broadcast": "192.168.0.255", 
            "netmask": "255.255.255.0", 
            "network": "192.168.0.0"
        }, 
        "ipv6": [
            {
                "address": "fe80::20c:29ff:fee8:f67b", 
                "prefix": "64", 
                "scope": "link"
            }
        ], 
        "macaddress": "00:0c:29:e8:f6:7b", 
        "module": "e1000", 
        "mtu": 1500, 
        "pciid": "0000:02:01.0", 
        "promisc": false, 
        "speed": 1000, 
        "timestamping": [
            "rx_software", 
            "software"
        ], 
        "type": "ether"
    }
}

PLAY RECAP **********************************************************************************************************
192.168.0.218              : ok=2    changed=0    unreachable=0    failed=0   

[root@test ~]#

  说明:以上playbook就实现了对ansible_eth0这个变量进行调试并打印,能够看到ansible_eth0是一个相对比较复杂的变量,里面包含了字典,列表混合一块儿的一个大字典。

咱们能够看到ansible_eht0里面包含了不少内容,咱们要想读取其中的IPV4地址,咱们能够采用“.”或者下标的方式去访问,以下所示

[root@test ~]#cat test.yml 
---
- hosts: 192.168.0.218
  remote_user: root
  
  tasks:
    - name: print ipv4  
      shell: echo {{ ansible_eth0["ipv4"]["address"] }} 
    - name: print mac
      shell: echo  {{ ansible_eth0.macaddress }}
[root@test ~]#ansible-playbook test.yml -v
Using /etc/ansible/ansible.cfg as config file

PLAY [192.168.0.218] ************************************************************************************************

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

TASK [print ipv4] ***************************************************************************************************
changed: [192.168.0.218] => {"changed": true, "cmd": "echo 192.168.0.218", "delta": "0:00:00.001680", "end": "2019-11-17 18:30:21.926368", "rc": 0, "start": "2019-11-17 18:30:21.924688", "stderr": "", "stderr_lines": [], "stdout": "192.168.0.218", "stdout_lines": ["192.168.0.218"]}

TASK [print mac] ****************************************************************************************************
changed: [192.168.0.218] => {"changed": true, "cmd": "echo 00:0c:29:e8:f6:7b", "delta": "0:00:00.001746", "end": "2019-11-17 18:30:22.650541", "rc": 0, "start": "2019-11-17 18:30:22.648795", "stderr": "", "stderr_lines": [], "stdout": "00:0c:29:e8:f6:7b", "stdout_lines": ["00:0c:29:e8:f6:7b"]}

PLAY RECAP **********************************************************************************************************
192.168.0.218              : ok=3    changed=2    unreachable=0    failed=0   

[root@test ~]#

  说明:由此能够看出ansible多级变量的调用,使用中括号和点号都是能够的

相关文章
相关标签/搜索