Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - is it possible to add tags to hosts inside inventory?

As the topic says, my question is if its possible to add tags to the hosts described inside the inventory?

My goal is to be able to run the ansible-playbook on specific host/group of hosts which has that specific tag e.g only on servers with tag 'Env=test and Type=test'

So for example when I run the playbook:

ansible-playbook -i hosts test.yml --extra-vars "Env=${test} Type=${test}"

I will pass the tags in the command and it will run only on the filtered hosts.

Thanks a lot!

Update:

Alternatively maybe doing something like in dynamic inventory? https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#developing-inventory

[tag_Name_staging_foo]

[tag_Name_staging_bar]

[staging:children]
tag_Name_staging_foo
tag_Name_staging_bar
like image 278
rivar_ Avatar asked Dec 31 '18 14:12

rivar_


People also ask

How do you add tags in Ansible?

Adding tags to includes If you add mytag to a dynamic include, then run that playbook with --tags mytag , Ansible runs the include itself, runs any tasks within the included file or role tagged with mytag , and skips any tasks within the included file or role without that tag.

Where can you use tags in Ansible?

A tag is an attribute that you can set to an Ansible structure (plays, roles, tasks), and then when you run a playbook you can use –tags or –skip-tags to execute a subset of tasks. Now to see the effect of tags we can –list-tasks applying tags filters to know which tasks will run.

How do tags work Ansible?

Tags are metadata that you can attach to the tasks in an Ansible playbook. They allow you to selectively target certain tasks at runtime, telling Ansible to run (or not run) certain tasks.

What is dynamic inventory in Ansible?

A dynamic inventory is a script written in Python, PHP or any other programming language. It comes in handy in cloud environments such as AWS where IP addresses change once a virtual server is stopped and started again.


2 Answers

To answer your question

Is it possible to add tags to hosts inside inventory to run the ansible-playbook on a specific host/group of hosts?

No, tags apply ONLY to the tasks

When you apply tags: attributes to structures other than tasks, Ansible processes the tag attribute to apply ONLY to the tasks they contain. Applying tags anywhere other than tasks is just a convenience so you don’t have to tag tasks individually.

like image 179
Vladimir Botka Avatar answered Oct 02 '22 05:10

Vladimir Botka


Hosts don't have ansible "tags"; tasks have tags and they'e used to conditionally execute the tasks, not conditionally target hosts.

There's a few ways to conditionally target hosts, and the best way in my experience is ansible groups. Put the hosts you want to target in a group; then either target this group directly in a play:

 - hosts: my_host_group
   tasks: [ ... ] 

Or limit the playook to a subset of the hosts targeted in a play:

ansible-playbook -l my_limited_hosts_group playbook.yaml
like image 31
Daniel Farrell Avatar answered Oct 02 '22 06:10

Daniel Farrell