CentOS 7
Sponsored Link

Ansible : Use Playbook#42015/07/15

 
To use "notify", "handlers", it's possible to execute a task which is defined in "handlers" after a task completed which has "notify" method.
[1] For example, create a Playbook which sshd is restarted after editing sshd_config.
[cent@dlp ~]$
vi playbook_sample.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  handlers:
  - name: restart sshd
    service: name=sshd state=restarted
  tasks:
  - name: edit sshd_config
    lineinfile: >
      dest=/etc/ssh/sshd_config
      regexp="{{ item.regexp }}"
      line="{{ item.line }}"
    with_items:
    - { regexp: '^#PermitRootLogin', line: 'PermitRootLogin no' }
    notify: restart sshd
    tags: Edit_sshd_config

[cent@dlp ~]$
ansible-playbook playbook_sample.yml --ask-become-pass

SUDO password:

PLAY [target_servers] * ** ** ** ** ** ** ** ** ** ** ** ** ** 

GATHERING FACTS * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK: [edit sshd_config] * ** ** ** ** ** ** ** ** ** ** ** ** 
changed: [10.0.0.52] => (item={'regexp': '^#PermitRootLogin', 'line': 'PermitRootLogin no'})
changed: [10.0.0.51] => (item={'regexp': '^#PermitRootLogin', 'line': 'PermitRootLogin no'})

NOTIFIED: [restart sshd] * ** ** ** ** ** ** ** ** ** ** ** ** 
changed: [10.0.0.51]
changed: [10.0.0.52]

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

# confirm

[cent@dlp ~]$
ansible target_servers -m command -a "grep '^PermitRootLogin' /etc/ssh/sshd_config" -b --ask-become-pass

SUDO password:
10.0.0.51 | success | rc=0 >>
PermitRootLogin no

10.0.0.52 | success | rc=0 >>
PermitRootLogin no
Matched Content