Ubuntu 22.04
Sponsored Link

Ansible : Playbook का उपयोग करें (notify)2023/09/27

 
[notify], [handlers] का उपयोग करने के लिए, किसी कार्य को निष्पादित करना संभव है जिसे [handlers] में परिभाषित किया गया है, एक कार्य पूरा होने के बाद जिसमें [notify] विधि है।
[1] उदाहरण के लिए, एक Playbook बनाएं जिसे [sshd] [sshd_config] संपादित करने के बाद पुनः प्रारंभ किया जाए।
ubuntu@dlp:~$
vi playbook_sample.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  handlers:
  - name: restart sshd
    service: name=ssh state=restarted
  tasks:
  - lineinfile:
      path: /etc/ssh/sshd_config
      regexp: '^PermitRootLogin'
      line: 'PermitRootLogin no'
    notify: restart sshd
    tags: Edit_sshd_config

ubuntu@dlp:~$
ansible-playbook playbook_sample.yml --ask-become-pass

BECOME password:

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

TASK [Gathering Facts] *********************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK [lineinfile] **************************************************************
changed: [10.0.0.52]
changed: [10.0.0.51]

RUNNING HANDLER [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    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

# सत्यापित करें

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

BECOME password:
10.0.0.52 | CHANGED | rc=0 >>
PermitRootLogin no
10.0.0.51 | CHANGED | rc=0 >>
PermitRootLogin no
मिलान सामग्री