A, playbook cycle
# vi /etc/ansible/while.yml //加入如下内容 --- - hosts: yw02 user: root tasks: - name: change mode for files file: path=/tmp/{{ item }} mode=600 with_items: - 1.txt - 2.txt - 3.txt
Description: This module uses a file, followed by the path, if it is a file, you can write /tmp/1.txt, multiple files, you can use a loop, with_items for the circulation of objects
执行:ansible-playbook while.yml
Error: the other machine does not have these three documents, increase state = touch before mode, create one.
Then execute: ansible-playbook while.yml
Executed successfully, to create, and then define its authority.
Two, playbook of conditional
# vi /etc/ansible/when.yml //加入如下内容 --- - hosts: testhost user: root gather_facts: True tasks: - name: use when shell: touch /tmp/when.txt when: ansible_ens33.ipv4.address == "192.168.149.131"
Description:
Here hosts write testhost, write a machine does not make sense.
Collected here are the facts, this line can also be deleted, the default is to collect, then to use it.
ansible yw02 -m setup can view all of facter information.
when judgment is a condition when the condition is true, we will execute this shell.
From the information gathered there to find the facts in the ipv4 address ansible_ens33 whether for IP, the condition is satisfied, the execution shell, without this condition, it will be directly executed.
When grading each level below to RBI, there is no classification directly write the equal sign.
when not just for facts, also for other situations, such as whether a file, directory exists.
"ansible_facts": // most general one "ansible_all_ipv4_addresses": [// determine the conditions from a start, there is an array, all of the ipv4 listed, but below it there are two ipv6, to judge the two words have to be judged, so this is not wanted. "192.168.98.134", "192.168.149.132" ], ... "ansible_ens33": { "Active": to true, "Device": "ens33", ... "hw_timestamp_filters": [], "IPv4": { "address": "192.168.149.132", "Broadcast": "192.168.149.255", "Netmask": "255.255.255.0", "
[root@fuxi01 ansible]# ansible-playbook when.yml PLAY [testhost] ********************************************************************************************************************** TASK [Gathering Facts] *************************************************************************************************************** fatal: [yw02]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host yw02 port 22: No route to host", "unreachable": true} ok: [yw03] ok: [127.0.0.1] TASK [use when] ********************************************************************************************************************** skipping: [127.0.0.1] [WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. changed: [yw03] PLAY RECAP *************************************************************************************************************************** 127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 yw02 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0 yw03 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Three, playbook of handlers
After performing the task, some operations after changes in the server to perform, for example, we modified the configuration file, you need to restart the service.
# vi /etc/ansible/handlers.yml//加入如下内容 --- - name: handlers test hosts: yw02 user: root tasks: - name: copy file copy: src=/etc/passwd dest=/tmp/aaa.txt notify: test handlers handlers: - name: test handlers shell: echo "111111" >> /tmp/aaa.txt
Note: Only copy module after the really successful execution, only to call the following handlers related operations. This means that if 1.txt and 2.txt content is the same, and not to perform handlers inside the shell commands. This is more suitable for the configuration file changes, restart the service operation. After similar command1 && command2 (handlers), in front of the command is successful, before the implementation of the handler, need to associate with notify.