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
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 .
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.
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.
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.
Ansible tags use "or" not "and" as a comparison. Your solution to create yet another tag is the appropriate one.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With