6. Playbook cycle, conditional judgment in playbook, handlers in Playbook

Keywords: Linux ansible shell ssh network

1. playbook cycle

#vi/etc/ansible/while.yml //Add the following
---
- 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: A file module is used here, followed by a path, if it is a file, you can write/tmp/1.txt, multiple files, you can use a loop, with_items as the object of the loop

Execution: ansible-playbook while.yml

Error: The other machine does not have these three files, add state=touch before mode l to create one.

Re-execute: ansible-playbook while.yml

Execution succeeded, creating it before defining its permissions.


2. Conditional Judgment in playbook

#vi/etc/ansible/when.yml //Add the following
---
- 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"

Explain:

Here hosts write testhost, which makes no sense to write a machine.

Facts are collected here, and this line can also be deleted. By default, facts are collected and used next.

Ansible yw02-m setup can view all facter information.

When is a conditional judgment, the shell is executed when the condition is true.

From the information collected by facts, find out if the address of ipv4 under ansible_ens33 is the IP, if the condition is valid, the shell is executed, if it is not, it is executed directly.

When there is a rating, the dots below each level are punctuated, and the equal sign is written directly without a rating.

when is not only for facts, but also for other situations, such as determining whether a file or directory exists.

"ansible_facts":            //Top Level
"ansible_all_ipv4_addresses":[  //Judgment starts at this level, here is an array, listing all ipv4, but there are two ipv6 below it, so you need to judge two, so this is not what you want.
            "192.168.98.134", 
            "192.168.149.132"
        ], 
...
"ansible_ens33": {
            "active": true, 
            "device": "ens33", 
...
 "hw_timestamp_filters": [], 
            "ipv4": {
                "address": "192.168.149.132", 
                "broadcast": "192.168.149.255", 
                "netmask": "255.255.255.0", 
                "network": "192.168.149.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


3. handlers in playbook

After task execution, there are actions to be performed after the server changes, such as restarting the service after we have modified the configuration file.

#vi/etc/ansible/handlers.yml//Add the following
---
- 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: The following handlers-related operations will not be invoked until the copy module actually executes successfully.That is, if the contents of 1.txt and 2.txt are the same, shell-related commands inside handlers will not be executed.This is a good way to restart the service when the configuration file changes.Similar to command1 && command2 (handlers), the handler needs to be associated with notify before it can be executed successfully.

Posted by genius on Fri, 10 Jan 2020 09:13:32 -0800