Ansible notify Playbook を利用する方法

環境
CentOS Stream 9

概要
[notify] と [handlers] を利用することで、[notify] を指定したタスク実行後に
[handlers] で定義したタスクを実行することができます。

操作方法
1.管理対象ホストの [sshd_config] を編集した後、[sshd] を再起動します。

$ vi playbook_sample.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  handlers:
  - name: restart sshd
    service: name=sshd state=restarted
  tasks:
  - lineinfile:
      path: /etc/ssh/sshd_config
      regexp: '^PermitRootLogin'
      line: 'PermitRootLogin no'
    notify: restart sshd
    tags: Edit_sshd_config

playbookを実行します。
$ ansible-playbook playbook_sample.yml –ask-become-pass

結果確認

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

 

Ansible

Posted by arkgame