Ansible : Playbook を利用する (notify)2024/09/11 |
[notify] と [handlers] を利用することで、[notify] を指定したタスク実行後に [handlers] で定義したタスクを実行することができます。 |
|
[1] | 以下の例は、管理対象ホストの [sshd_config] を編集した後、[sshd] を再起動する Playbook です。 |
freebsd@dlp:~ $
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 prohibit-password' notify: restart sshd tags: Edit_sshd_config ansible-playbook playbook_sample.yml --ask-become-pass BECOME password: PLAY [target_servers] ********************************************************************** TASK [Gathering Facts] ********************************************************************* ok: [10.0.0.51] ok: [10.0.0.52] TASK [lineinfile] ************************************************************************** changed: [10.0.0.52] changed: [10.0.0.51] RUNNING HANDLER [restart sshd] ************************************************************* changed: [10.0.0.52] changed: [10.0.0.51] 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 # 確認 freebsd@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 prohibit-password 10.0.0.51 | CHANGED | rc=0 >> PermitRootLogin prohibit-password |
Sponsored Link |
|