Reputation: 1529
I am trying to use the "always" and "block" capabilities defined here - https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html
I have a main.yml file that looks like this -
- block:
- include: git_clone_and_combining.yml
run_once: yes
delegate_to: localhost
- include: combined_repo_push.yml
run_once: yes
delegate_to: localhost
- include: deploy_code.yml
always:
- include: resume_asg.yml
delegate_to: localhost
It runs through the playbooks as expected, but the always
block before "resume_asg.yml" script appears to be ignored, as it does not run on failure. Is this use of blocks not supported on plays?
EDIT - Opened a ticket with Redhat about this, and they opened a Bug report here - https://github.com/ansible/ansible/issues/72941. Always
and block
do not appear to work in main.yml when a run_once
argument is specified.
Thanks.
Upvotes: 1
Views: 6233
Reputation: 68024
Quoting from include
This module will still be supported for some time but we are looking at deprecating it in the near future.
A: I can reproduce the problem with include. To solve the problem, use include_tasks. For example
shell> cat pb.yml
- hosts: localhost
tasks:
- block:
- include_tasks: test_fail.yml
run_once: true
always:
- include_tasks: tasks-always.yml
shell> cat test_fail.yml
- command: "{{ cmd|default('true') }}"
shell> cat tasks-always.yml
- debug:
msg: Always
give (abridged)
shell> ansible-playbook pb.yml
TASK [command] ****
changed: [localhost]
TASK [debug] ****
ok: [localhost] =>
msg: Always
shell> ansible-playbook pb.yml -e "cmd=false"
TASK [command] ****
fatal: [localhost]: FAILED! => changed=true
cmd:
- 'false'
delta: '0:00:00.003130'
end: '2020-12-11 21:29:26.481652'
msg: non-zero return code
rc: 1
start: '2020-12-11 21:29:26.478522'
stderr: ''
stderr_lines: <omitted>
stdout: ''
stdout_lines: <omitted>
TASK [debug] ****
ok: [localhost] =>
msg: Always
The always section works as expected.
Examples
shell> cat pb.yml
- hosts: localhost
tasks:
- block:
- command: "{{ cmd|default('true') }}"
always:
- debug:
msg: Always
shell> ansible-playbook pb.yml
PLAY [localhost] ****
TASK [command] ****
changed: [localhost]
TASK [debug] ****
ok: [localhost] =>
msg: Always
...
shell> ansible-playbook pb.yml -e "cmd=false"
PLAY [localhost] ****
TASK [command] ****
fatal: [localhost]: FAILED! => changed=true
cmd:
- 'false'
delta: '0:00:00.004007'
end: '2020-12-11 06:57:10.055177'
msg: non-zero return code
rc: 1
start: '2020-12-11 06:57:10.051170'
stderr: ''
stderr_lines: <omitted>
stdout: ''
stdout_lines: <omitted>
TASK [debug] ****
ok: [localhost] =>
msg: Always
...
The included tasks give the same results
shell> cat pb.yml
- hosts: localhost
tasks:
- block:
- command: "{{ cmd|default('true') }}"
always:
- include: tasks-always.yml
shell> cat tasks-always.yml
- debug:
msg: Always
The role gives the same results
shell> cat pb.yml
- hosts: localhost
roles:
- test-01
shell> cat roles/test-01/tasks/main.yml
- block:
- command: "{{ cmd|default('true') }}"
always:
- include: tasks-always.yml
shell> cat roles/test-01/tasks/tasks-always.yml
- debug:
msg: Always
Upvotes: 4