Search code examples
ansibleansible-2.xansible-inventory

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

Solution

  • 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.