Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: In a playbook, filter a role by tag(s) without passing at the command-line

In Ansible 1.7, I can use --tags from the command-line to only run a subset of that playbooks tasks.

But I'm wanting to bake into my playbook to run a set of roles with only tasks that match tags. That is, I don't want to have to pass this in via the command-line since it will be the same every time.

At first I thought it was this command, but this does the opposite: tagging tasks with these tags instead of filtering them out based on this.

roles:   - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] } 

I can imagine implementing this using conditionals but tags would be a much more elegant way of achieving this.

like image 909
Mike Biglan MS Avatar asked Sep 04 '14 20:09

Mike Biglan MS


People also ask

How do you use tags in Ansible roles?

If you assign the always tag to a task or play, Ansible will always run that task or play, unless you specifically skip it ( --skip-tags always ). Fact gathering is tagged with 'always' by default. It is only skipped if you apply a tag and then use a different tag in --tags or the same tag in --skip-tags .

How do you use Ansible role in playbook?

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules. In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.

How do I skip a specific task in Ansible?

Ansible has a well-known mechanism to only run a certain list of tasks: tags. When we call ansible-playbook with the --tags parameter, we only execute tasks that have one of the specified tasks. There is also a --skip-tags option, which runs all tasks except those with the specified tags.


1 Answers

You only have the following options with the current version of Ansible:

  1. Specify the tags on the command line
  2. Use a variable instead of a tag to conditionally run tasks
  3. Split your webserver role into multiple roles and use role dependencies for the common tasks

This feature request has come up on the mailing list a few times and I haven't seen any indication from the dev team that it will be added as a new feature.

like image 126
jarv Avatar answered Oct 14 '22 23:10

jarv