3 minutes
Ansible Code Organization
At work the Ansible repo that I work with is not as organized as I’d like. I’d like to make sure that the one that I am starting to commit to in my Lab is not going to end up in the same state. In order to help keep it tidy I figured it would be a good idea to separate out some of the tasks that I have in the roles I am working on. Then I could call import_tasks
on them in the main.yml
with some conditionals. This would also allow me to resuse groups of tasks from one role in another. I started out with something like this:
I was attempting to use the never
reserved tag to make sure that a task was only executed when I specifically included the tag. I guess I misunderstood the documentation because the way I found that it works is that when you specify the never
tag on a task it will only execute when you include any tag. So with the setup about and running:
I would get the following output:
So even though I only included tags_a
in the command I got the exection of both tasks with the never
tag.
I couldn’t really find a workaround that seems correct so I ended up adding my own conditional to the import_tasks
tasks to check the tags:
Now when I run the playbook with just the tags_a
tag I only get those tasks and likewise for tags_b
. Additionally when I don’t include any tags all both are skipped:
I have commited this testing role to my Lab if you are interesting it adding something like that for you own test or just want to copy this: