Ansible handlerとnotifyの使い方のサンプル

環境
RHEL 8.6

概要
notify xxxファイルを更新した時
handler xxxサービスを再起動する

操作例
httpd.confに更新がある場合にapacheを再起動します。
1.httpd.cnfの更新を行う
tasks/main.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
---
- name: Copy httpd.conf
copy:
src: xxxx/httpd.conf
dest: /etc/httpd/conf ←配布ノードのパス
notify:
- restart_apache
--- - name: Copy httpd.conf copy: src: xxxx/httpd.conf dest: /etc/httpd/conf ←配布ノードのパス notify: - restart_apache
--- 
- name: Copy httpd.conf
  copy:
    src: xxxx/httpd.conf
    dest: /etc/httpd/conf ←配布ノードのパス
  notify:
    - restart_apache

2.handlers側の処理を行う

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
---
- name: restart_apache
service:
name: httpd
state: restarted
enabled: yes
--- - name: restart_apache service: name: httpd state: restarted enabled: yes
---
- name: restart_apache
  service: 
    name: httpd
    state: restarted
    enabled: yes

 

Ansible

Posted by arkgame