A, handlers and notify conjunction with triggers
When handlers are of the same tasks at the same level, is equivalent to a specific list of tasks, these tasks with the tasks previously said in the task is not essentially different, when concern for resource changes will take certain action. This action can be used to notify the last play of each trigger, there are a plurality of times so as to avoid the specified operation change occurs, the specified operation is performed only one time after completion of all the changes are listed in the notify operation is called handler, in other words when the resource concern changes notify the recall operations defined handlers. Which notify the task that is to be the task of monitoring changes in the resource task, notify multiple handlers can invoke operations defined, a handlers can be defined in many tasks.
--- - 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
Description: The name must notify the designated tasks and handlers in the name of the same tasks as defined by the different handlers will not be executed, handlers in the task equivalent to no notify call.
In some cases, we may also need to call multiple handlers, or the use of other handlers handlers, ansible can be very easy to achieve these functions, as shown below
1) invoke multiple handlers
--- - 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
Description: We need to call multiple handlers written in the form of the notify list, the same task name we need to be triggered with handlers in the task name to be called exactly the same
2) handlers call handlers
--- - 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
Description: handlers call handlers, use the option to directly notify the handlers can.
We need to pay attention to the use of handlers in the following points:
1) handlers will only change when it is in operation when the task is executed, handlers defined tasks it will not be like task tasks as automatic execution will be from top to bottom, it will only happen to be where the task notify the state triggers task execution handlers, if a task is defined notify call handlers, but the judge conditions and other reasons, the task has not been executed, the notify call handlers also will not be executed.
2) handlers will only run at the end of a play; if you want to run handlers in the middle of a playbook, you need to use the meta module to achieve, such as: -mate: flush_handlers
Second, the use of variables in playbook
Naming ansible variable naming conventions with other languages or system variables are very similar. Variable names begin with a letter to the English case, the middle may include underscores and numbers, there are many sources of ansible variables, specifically the following points:
1) ansible setup module that can get a lot of basic information from a remote host on a remote host, it returns all variables can be called directly, about the setup instructions please refer to my blog https://www.cnblogs.com/qiuhom -1874 / p / 11853512.html
2) defined in the / etc / ansible / hosts, this file is loaded by default when ansible execution host name list of documents, in addition to which we can define the hosts to be managed, we can also define a separate variable definitions for a single host, we called the general a variable for individual hosts defined variables (can also be called host variables); there is a variable that it is not for a single host, it is for all the host of one of the group, we call this variable called public set of variables. Host variables defined in Listing priority is higher than ordinary variables Public variables.
2.1) host variable may be added to the list of hosts for the host when the host variable defined for use in playbook, as follows
[websers] 192.168.0.128 http_port=80 maxRequestsPerChild=808 192.168.0.218 http_port=81 maxRequestsPerChild=909
2.2) a host of variables, variables are set in the specified variable playbook can be used on all hosts imparted to the specified group, as shown below
[websers] 192.168.0.128 http_port=80 192.168.0.218 http_port=81 [websers:vars] maxRequestsPerChild=909
3) via the command line specified variable (-e specified variable assignment, but more needs to be said that in quotation marks or a variable with a specified assignment -e), the highest priority on the command line specified. As follows
ansible-playbook -e 'package_name1=httpd package_name2=nginx' test_vars.yml
4) defined in the variable PlayBook, the most common method is to use the definition of variables vars code block, as shown in FIG.
--- - hosts: websers remote_user: root vars: - abc: xxx - bcd: aaa
5) defined in the independent variables yml file, using vars_files reference in its block files playbook variable, as shown
[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) is defined in the role, this role when it comes to doing the follow-up explanation
Variable is called: first variable in need playbook "{}" enclose the variable indicating the contents of the brackets is a variable, sometimes "variable_name {} {}" takes effect; second It is ansible-playbook -e option to specify which variables, ansible-playbook -e "hosts = www user = xxxx" test.yml
Define variables in the host inventory method is simple and intuitive, but when you need to define variables have a lot of time, and when working with multiple hosts, this method is very troublesome, in fact ansible official manual does not suggest that we put variables defined directly to the hosts file; ansible when executing the command, ansible will default / and / etc / ansible / group_vars / variable definition file read from the two directories / etc / ansible / host_vars, if the / etc / ansible / lower no more than two directories, we can be created manually, and you can create to define the file name or host group with the same name in the hosts file host variable in the two directories. For example, we give this host 192.168.0.218 defined variables file, we can create a blank file 192.168.0.218 in the / etc / ansible / host_vars / directory, and then in a file ymal syntax to define the desired variable. As follows
[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 ~]#
Note: You can see that we define the host variable file / etc / ansible / host_vars / in the variables into effect.
Similarly, we want to define some variables for the host of a group, we only need to create a file with the same name as the host to the host group list in the / etc / ansible / group_vars / directory.
Third, the use of higher-order variables
For ordinary variables, such as variables and file playbook and defined by the ansible command set, hosts file defined in the definition of these variables are called common variable or variables called simply, we can directly in the playbook curly brackets plus variable name to read the contents of a variable; the variable or array except ansible also called the list of variables, as follows:
[root@test ~]#cat vars.yml --- packages_list: - vsftpd - nginx [root@test ~]#
After complete list defines the variables we want to use a list of names which can add suffixes way to access an array of little use similar shell script, as follows
[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 ~]#
Description: We want to use the first element in the list of variables, we can write vars_list [0], then use the second variable index is 1, and so on
[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 ~]#
Note: You can see the files and directories we created has been generated in the target host
The above usage is a typical usage of the python list, read the list of elements in python is represented by the following subject to read the value of the corresponding element. We will introduce another took a more complex variable, which is similar to the probability of the dictionary in python, but higher than the dictionary of dimension, more like a two-dimensional dictionary. ansible ansible_eth0 is a built-in variable, which is used to save the remote host interface eth0 information above, including ip address and subnet mask. As follows
[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 ~]#
Note: The above playbook on the realization of this variable ansible_eth0 debugging and prints, you can see ansible_eth0 is a relatively complex variable, which contains a dictionary, a list of a large dictionary mixed together.
We can see ansible_eht0 which contains a lot of content, we want to read one of the IPV4 address, we can use. "" Or subscript way to access, as shown below
[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 ~]#
Note: It can be seen brackets and dot calls using ansible multilevel variables are possible