I have a playbook with two handlers:
- Restart the Chrony service
- Reload the Chrony sources
It is not necessary to run the second handler, after the first has been executed. In that case, how can I tell Ansible to skip the second handler?
Example:
- name: Configure NTP
hosts: jumphost
become: true
tasks:
- name: "Uninstall unwanted timesyncer"
apt:
state: absent
name:
- ntp
- systemd-timesyncd
- name: "Install Chrony"
apt: state=present name=chrony
- name: "Disable Debian pool servers"
lineinfile:
path: /etc/chrony/chrony.conf
regexp: '^pool.*'
backrefs: true
line: '#\g<0>'
notify: Restart Chrony service
- name: "Configure example ntp server"
copy:
dest: /etc/chrony/sources.d/example.sources
owner: root
group: root
mode: 0644
content: |
server ntp.example.de iburst
notify: Reload Chrony sources
handlers:
- name: Restart Chrony service
service: name=chronyd state=restarted
- name: Reload Chrony sources
command: chronyc reload sources
I have a playbook with two handlers:
- Restart the Chrony service
- Reload the Chrony sources
It is not necessary to run the second handler, after the first has been executed. In that case, how can I tell Ansible to skip the second handler?
Example:
- name: Configure NTP
hosts: jumphost
become: true
tasks:
- name: "Uninstall unwanted timesyncer"
apt:
state: absent
name:
- ntp
- systemd-timesyncd
- name: "Install Chrony"
apt: state=present name=chrony
- name: "Disable Debian pool servers"
lineinfile:
path: /etc/chrony/chrony.conf
regexp: '^pool.*'
backrefs: true
line: '#\g<0>'
notify: Restart Chrony service
- name: "Configure example ntp server"
copy:
dest: /etc/chrony/sources.d/example.sources
owner: root
group: root
mode: 0644
content: |
server ntp.example.de iburst
notify: Reload Chrony sources
handlers:
- name: Restart Chrony service
service: name=chronyd state=restarted
- name: Reload Chrony sources
command: chronyc reload sources
Share
Improve this question
edited Jan 22 at 9:45
NateDhaliwal
1431 silver badge12 bronze badges
asked Jan 21 at 11:29
cevingceving
23.9k15 gold badges117 silver badges196 bronze badges
0
1 Answer
Reset to default -2I think I got it. It is possible to register in a handler.
- name: Configure NTP
hosts: jumphost
become: true
tasks:
- name: "Uninstall unwanted timesyncer"
apt:
state: absent
name:
- ntp
- systemd-timesyncd
- name: "Install Chrony"
apt: state=present name=chrony
- name: "Disable Debian pool servers"
lineinfile:
path: /etc/chrony/chrony.conf
regexp: '^pool.*'
backrefs: true
line: '#\g<0>'
notify: Restart Chrony service
- name: "Configure example ntp server"
copy:
dest: /etc/chrony/sources.d/example.sources
owner: root
group: root
mode: 0644
content: |
server ntp.example.de iburst
notify: Reload Chrony sources
handlers:
- name: Restart Chrony service
service: name=chronyd state=restarted
register: restart
- name: Reload Chrony sources
command: chronyc reload sources
when: "restart is undefined or not restart.changed or restart.failed"