'Ansible include_tasks will not run when tags are specified
I am including some tasks like shown below in my main.yml (as one of the tasks in a list of several other tasks)
- name : remove swarm
include_tasks : swarm.undo.yml
tags : [ 'never', 'debug' ]
when I run the playbook specifying the --tags never
It just don't run, but shows the output below.
PLAY [all] *****************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************
ok: [node2]
ok: [node4]
ok: [node3]
ok: [node5]
ok: [node1]
ok: [node6]
TASK [swarm : remove swarm] ************************************************************************************************************************************
included: /home/xbox/Work/Infra/roles/swarm/tasks/swarm.undo.yml for node1, node2, node3, node4, node5, node6
PLAY RECAP *****************************************************************************************************************************************************
node1 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node4 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node5 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node6 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
If I do not specify and comment out the other tasks in that group (plus remove the never
tag). It runs as it should.
Why is this behavior? What should I do to make it run correctly?
Solution 1:[1]
The behaviour seems normal, and you probably don't have any tasks tagged with never
in swarm.undo.yml
.
As per the documentation the tags are applicable for tasks and plays. So when you specify --tags never
, the include_tasks
(which is a task) gets included as expected. But only the tasks inside swarm.undo.yml
that have the never
tag will run.
Quoting from the above link:
Ansible reserves two tag names for special behavior:
always
andnever
. If you assign thealways
tag to a task or play, Ansible will always run that task or play, ...
Option 1: So one way is to make sure the relevant tasks in swarm.undo.yml
have the never
tag.
Option 2: If there are lot of tasks and all should be tagged with never
, you can consider converting this task file as a playbook (with tag at play level) and use import_playbook
.
Example swarm.undo.yml
:
- hosts: myhosts
tags:
- never
tasks:
# your tasks go here
Then in your main.yml
:
- name: Include swarm undo
import_playbook: swarm.undo.yml
Solution 2:[2]
When this changed in Ansible I had to add blocks to all my included files with the relevant tags configured.
However as of Ansible 2.7 you can tell Ansible to apply specific tags to all the included tasks. This means you don't need to make any changes in swarm.undo.yml
. Your main.yml
example above would look like this:
- name: remove swarm
include_tasks:
file: swarm.undo.yml
# Adds these tags to all included tasks
tags: [ 'never', 'debug' ]
tags: [ 'never', 'debug' ]
Solution 3:[3]
You should use "apply" keyword, this doesn't work if you write without that one. The correct syntax (from documentation) is
- name: Apply tags to tasks within included file
include_tasks:
file: install.yml
apply:
tags:
- install
If you want to run all tasks using one tags there is the best way below:
- name: Include and run an inner and an outer task
include_tasks:
file: install.yml
apply:
tags: install
tags: install
Solution 4:[4]
you could run with this options to debug:
ansible-playbook -i xxx tasks/xx.yml --list-tags
ansible-playbook -i xxx tasks/xx.yml --list-tasks
if tags is all
, all tasks will run
Solution 5:[5]
Tags never
and always
Are reserved you should not use them. Unless you know what they mean (bellow).
About tags on include_tasks
You must tag your include_tasks
with always
on it. To make filtering by tags work. By this issue.
- name : my tasks with tags inside
include_tasks : mytasks.yml
tags : [ 'always' ]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | seshadri_c |
Solution 2 | pemontto |
Solution 3 | Yuri Jolobov |
Solution 4 | å¼ é¦†é•¿ |
Solution 5 |