Skip to content

Instantly share code, notes, and snippets.

@kpfleming
Last active Feb 14, 2021
Embed
What would you like to do?
Limit Ansible handler runs to once per playbook run
%YAML 1.2
---
- ansible.builtin.include_tasks: "{{ playbook_dir ~ '/imports/reload_service_handler.yml' }}"
vars:
unit_name: nginx
listen: reload_nginx
%YAML 1.2
---
# variables to pass in:
# unit_name: unit name (include extension if not '.service')
# daemon_reload: boolean indicating whether systemd daemon reload is needed
- block:
- name: enable reload
when: hostvars[inventory_hostname][_flag_name] is not defined
ansible.builtin.set_fact:
"{{ _flag_name }}": true
- name: reload service
when: hostvars[inventory_hostname][_flag_name]
ansible.builtin.systemd:
name: "{{ unit_name }}"
state: reloaded
daemon_reload: "{{ daemon_reload|default(false) }}"
- name: disable additional reloads
when: hostvars[inventory_hostname][_flag_name]
ansible.builtin.set_fact:
"{{ _flag_name }}": false
vars:
_flag_name: "{{ '_reload_' ~ unit_name|replace('.','_') }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment