diff options
| author | Colin Wilk <colin.wilk@tum.de> | 2023-10-11 15:34:10 +0200 |
|---|---|---|
| committer | Colin Wilk <colin.wilk@tum.de> | 2023-10-11 15:49:45 +0200 |
| commit | c9c812343831be1bb56965b2aeff41eecdae0a11 (patch) | |
| tree | 4a5eec53821831cfc746788f631e8b283bfebe3b /tasks | |
| parent | 6da60ee68e98d97f2c58ea7dca39d85c9b81370b (diff) | |
| download | ansible-role-prometheus-target-c9c812343831be1bb56965b2aeff41eecdae0a11.tar.gz ansible-role-prometheus-target-c9c812343831be1bb56965b2aeff41eecdae0a11.zip | |
Add run_once option for handlers
This adds the run_once option for the two handler types (shell and
command). The variables `prometheus_target_handler_command_run_once`
and `prometheus_target_handler_shell_run_once` are introduced to
control this behavior.
For this change a switch from the Ansible Handlers to regular tasks was
necessary to enforce run_once behavior with delegated hosts. If you
don't rely on handler specific features such as deferring, nothing
changes when using the role.
Signed-off-by: Colin Wilk <colin.wilk@tum.de>
Diffstat (limited to 'tasks')
| -rw-r--r-- | tasks/handlers.yml | 34 | ||||
| -rw-r--r-- | tasks/lineinfile.yml | 8 | ||||
| -rw-r--r-- | tasks/main.yml | 7 |
3 files changed, 46 insertions, 3 deletions
diff --git a/tasks/handlers.yml b/tasks/handlers.yml new file mode 100644 index 0000000..b1d9b59 --- /dev/null +++ b/tasks/handlers.yml @@ -0,0 +1,34 @@ +--- +- name: Run command hook # noqa inline-env-var + ansible.builtin.command: + argv: '{{ prometheus_target_handler_command.argv | default(omit) }}' + chdir: '{{ prometheus_target_handler_command.chdir | default(omit) }}' + cmd: '{{ prometheus_target_handler_command.cmd | default(omit) }}' + creates: '{{ prometheus_target_handler_command.creates | default(omit) }}' + free_form: '{{ prometheus_target_handler_command.free_form | default(omit) }}' + removes: '{{ prometheus_target_handler_command.removes | default(omit) }}' + stdin: '{{ prometheus_target_handler_command.stdin | default(omit) }}' + stdin_add_newline: '{{ prometheus_target_handler_command.stdin_add_newline | default(omit) }}' + become: '{{ prometheus_target_handler_command_become }}' + become_method: '{{ prometheus_target_handler_command_become_method | default(omit) }}' + become_user: '{{ prometheus_target_handler_command_become_user | default(omit) }}' + delegate_to: '{{ prometheus_target_host }}' + run_once: '{{ prometheus_target_handler_command_run_once }}' + when: prometheus_target_handler_command_enabled + +- name: Run shell hook + ansible.builtin.shell: + chdir: '{{ prometheus_target_handler_shell.chdir | default(omit) }}' + cmd: '{{ prometheus_target_handler_shell.cmd | default(omit) }}' + creates: '{{ prometheus_target_handler_shell.creates | default(omit) }}' + executable: '{{ prometheus_target_handler_shell.executable | default(omit) }}' + free_form: '{{ prometheus_target_handler_shell.free_form | default(omit) }}' + removes: '{{ prometheus_target_handler_shell.removes | default(omit) }}' + stdin: '{{ prometheus_target_handler_shell.stdin | default(omit) }}' + stdin_add_newline: '{{ prometheus_target_handler_shell.stdin_add_newline | default(omit) }}' + become: '{{ prometheus_target_handler_shell_become }}' + become_method: '{{ prometheus_target_handler_shell_become_method | default(omit) }}' + become_user: '{{ prometheus_target_handler_shell_become_user | default(omit) }}' + delegate_to: '{{ prometheus_target_host }}' + run_once: '{{ prometheus_target_handler_shell_run_once }}' + when: prometheus_target_handler_shell_enabled diff --git a/tasks/lineinfile.yml b/tasks/lineinfile.yml index 0367be9..8c60ba0 100644 --- a/tasks/lineinfile.yml +++ b/tasks/lineinfile.yml @@ -17,6 +17,8 @@ delegate_to: '{{ prometheus_target_host }}' loop: '{{ prometheus_target_exporter + ([] if prometheus_target_skip_default_exporters else prometheus_target_default_exporters) }}' - notify: - - Run command hook - - Run shell hook + register: lineinfile + +- name: Export fact + ansible.builtin.set_fact: + changed: '{{ changed or lineinfile.changed }}' diff --git a/tasks/main.yml b/tasks/main.yml index 158191d..f8d6caa 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -2,3 +2,10 @@ - name: Select strategy ansible.builtin.include_tasks: file: '{{ prometheus_target_strategy }}.yml' + vars: + changed: false + +- name: Run handlers + ansible.builtin.include_tasks: + file: handlers.yml + when: changed |