Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run only ansible tasks with multiple tags?

Imagine this ansible playbook:

- name: debug foo   debug: msg=foo   tags:      - foo  - name: debug bar   debug: msg=bar   tags:      - bar  - name: debug baz   debug: msg=baz   tags:      - foo      - bar 

How can I run only the debug baz task? I want to say only run tasks which are tagged with foo AND bar. Is that possible?

I tried this, but it will run all 3 tasks:

ansible-playbook foo.yml -t foo,bar 
like image 202
chmac Avatar asked May 04 '15 17:05

chmac


People also ask

Can you run tasks that are not tagged in Ansible?

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 pass tags in Ansible?

Execute a specific task in a playbook As mentioned earlier, you can use tags to control the execution of Ansible playbooks. To specify which task to execute use the -t or –tags flag. In the command below, we are instructing Ansible to execute the first task only which has been tagged as hello.

How do I run multiple tasks in Ansible?

If you need to execute a task with Ansible more than once, write a playbook and put it under source control. Then you can use the playbook to push out new configuration or confirm the configuration of remote systems.

How do you run task only once in Ansible?

For such requirements where we need one tasks to run only once on a batch of hosts and we will be running that from Ansible controller node, we have feature parameter named run_once. When we have this parameter mentioned in a task, that task will run only once on first host it finds despite the host batch.


2 Answers

Ansible tags use "or" not "and" as a comparison. Your solution to create yet another tag is the appropriate one.

like image 131
Bruce P Avatar answered Sep 23 '22 06:09

Bruce P


Try when directive:

- name: debug foo   debug: msg=foo   tags:      - foo  - name: debug bar   debug: msg=bar   tags:      - bar  - name: debug baz   debug: msg=baz   when:     - '"foo" in ansible_run_tags'     - '"bar" in ansible_run_tags' 
like image 45
CHAN Avatar answered Sep 22 '22 06:09

CHAN