mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
Fix --force-handlers, and allow it in plays and ansible.cfg
The --force-handlers command line argument was not correctly running handlers on hosts which had tasks that later failed. This corrects that, and also allows you to specify force_handlers in ansible.cfg or in a play.
This commit is contained in:
parent
e6fa169a05
commit
652cd6cd5e
11 changed files with 123 additions and 12 deletions
|
@ -56,6 +56,20 @@ test_group_by:
|
|||
|
||||
test_handlers:
|
||||
ansible-playbook test_handlers.yml -i inventory.handlers -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS)
|
||||
# Not forcing, should only run on successful host
|
||||
[ "$$(ansible-playbook test_force_handlers.yml --tags normal -i inventory.handlers -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) | egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_B" ]
|
||||
# Forcing from command line
|
||||
[ "$$(ansible-playbook test_force_handlers.yml --tags normal -i inventory.handlers --force-handlers -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) | egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_A CALLED_HANDLER_B" ]
|
||||
# Forcing from command line, should only run later tasks on unfailed hosts
|
||||
[ "$$(ansible-playbook test_force_handlers.yml --tags normal -i inventory.handlers --force-handlers -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) | egrep -o CALLED_TASK_. | sort | uniq | xargs)" = "CALLED_TASK_B CALLED_TASK_D CALLED_TASK_E" ]
|
||||
# Forcing from command line, should call handlers even if all hosts fail
|
||||
[ "$$(ansible-playbook test_force_handlers.yml --tags normal -i inventory.handlers --force-handlers -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v -e fail_all=yes $(TEST_FLAGS) | egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_A CALLED_HANDLER_B" ]
|
||||
# Forcing from ansible.cfg
|
||||
[ "$$(ANSIBLE_FORCE_HANDLERS=true ansible-playbook --tags normal test_force_handlers.yml -i inventory.handlers -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) | egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_A CALLED_HANDLER_B" ]
|
||||
# Forcing true in play
|
||||
[ "$$(ansible-playbook test_force_handlers.yml --tags force_true_in_play -i inventory.handlers -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) | egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_A CALLED_HANDLER_B" ]
|
||||
# Forcing false in play, which overrides command line
|
||||
[ "$$(ansible-playbook test_force_handlers.yml --force-handlers --tags force_false_in_play -i inventory.handlers -e @$(VARS_FILE) $(CREDENTIALS_ARG) -v $(TEST_FLAGS) | egrep -o CALLED_HANDLER_. | sort | uniq | xargs)" = "CALLED_HANDLER_B" ]
|
||||
|
||||
test_hash:
|
||||
ANSIBLE_HASH_BEHAVIOUR=replace ansible-playbook test_hash.yml -i $(INVENTORY) $(CREDENTIALS_ARG) -v -e '{"test_hash":{"extra_args":"this is an extra arg"}}'
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
- name: echoing handler
|
||||
command: echo CALLED_HANDLER_{{ inventory_hostname }}
|
26
test/integration/roles/test_force_handlers/tasks/main.yml
Normal file
26
test/integration/roles/test_force_handlers/tasks/main.yml
Normal file
|
@ -0,0 +1,26 @@
|
|||
---
|
||||
|
||||
# We notify for A and B, and hosts B and C fail.
|
||||
# When forcing, we expect A and B to run handlers
|
||||
# When not forcing, we expect only B to run handlers
|
||||
|
||||
- name: notify the handler for host A and B
|
||||
shell: echo
|
||||
notify:
|
||||
- echoing handler
|
||||
when: inventory_hostname == 'A' or inventory_hostname == 'B'
|
||||
|
||||
- name: fail task for all
|
||||
fail: msg="Fail All"
|
||||
when: fail_all is defined and fail_all
|
||||
|
||||
- name: fail task for A
|
||||
fail: msg="Fail A"
|
||||
when: inventory_hostname == 'A'
|
||||
|
||||
- name: fail task for C
|
||||
fail: msg="Fail C"
|
||||
when: inventory_hostname == 'C'
|
||||
|
||||
- name: echo after A and C have failed
|
||||
command: echo CALLED_TASK_{{ inventory_hostname }}
|
28
test/integration/test_force_handlers.yml
Normal file
28
test/integration/test_force_handlers.yml
Normal file
|
@ -0,0 +1,28 @@
|
|||
---
|
||||
|
||||
- name: test force handlers (default)
|
||||
tags: normal
|
||||
hosts: testgroup
|
||||
gather_facts: False
|
||||
connection: local
|
||||
roles:
|
||||
- { role: test_force_handlers }
|
||||
|
||||
- name: test force handlers (set to true)
|
||||
tags: force_true_in_play
|
||||
hosts: testgroup
|
||||
gather_facts: False
|
||||
connection: local
|
||||
force_handlers: True
|
||||
roles:
|
||||
- { role: test_force_handlers }
|
||||
|
||||
|
||||
- name: test force handlers (set to false)
|
||||
tags: force_false_in_play
|
||||
hosts: testgroup
|
||||
gather_facts: False
|
||||
connection: local
|
||||
force_handlers: False
|
||||
roles:
|
||||
- { role: test_force_handlers }
|
Loading…
Add table
Add a link
Reference in a new issue