Tuesday, 4 August 2015

Ansible: Using multiple tags and untagged tag together

I have lots of Ansible playbooks with many roles in each. However when you are installing different minor version of the same software stack, there are only minor differences between the steps. In this case it does not make much sense to "copy paste" the whole role so I just wanted to use tags. I wanted to use untagged tasks as common tasks and tagged tasks for version specific tasks. To make it clear here is an example. If you have a long os related role which does ssh config, web config, database install and creation and many more but sometimes you need java-6 or java-7 it is easy to add task and tag those according to this. Than my theory was that I can run
ansible-playbook --tags=untagged,java6
to install the stack with java6 and
ansible-playbook --tags=untagged,java7
to install same stack with java7. However this does not work.

I have checked the Ansible source code and found why it is not working. Since I was not sure if this is a bug or by design I have opened a issue and described the problem.

Brian Coca was kind to answer quickly and as you can read he wrote this is by design but he was also kind to consider it as a feature request. Hope will be accepted. However if you need this modified behaviour now you can either check the issue or read the solution below.

Ansible 1.9

The corresponding code part is the tasks_to_run_in_play() function in playbook/__init__.py

Original code:
elif 'untagged' in self.only_tags:
    if task_set == u:
        should_run = True

Proposed fix:
elif 'untagged' in self.only_tags and task_set == u:
    should_run = True