rivar_
rivar_

Reputation: 375

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

Upvotes: 13

Views: 21337

Answers (5)

jing
jing

Reputation: 2021

I solved it this way:

hosts file:

[linux_prod]
1.1.1.100   hostname=LINUX-01

[linux_staging]
1.1.1.101   hostname=LINUX-02

[windows_prod]
1.1.1.102   hostname=WIN-01

[windows_staging]
1.1.1.103   hostname=WIN-02

[staging:children]
linux_staging
windows_staging

[prod:children]
linux_prod
windows_prod

ping.yaml file

---
- hosts: windows
  tasks:
    - name: ping
      win_ping:

- hosts: linux
  tasks:
    - name: ping
      ping:

And then do the work only for staging or production:

ansible-playbook ping.yaml --limit staging
ansible-playbook ping.yaml --limit prod

Upvotes: 0

Black Rain
Black Rain

Reputation: 121

I was searching for similar thing.

Anyway, given that as input:

all:
  hosts:
    test1: 
      tags:
        - bar
    test2:
      host_var: value
      tags:
        - foo
    test3:
      tags:
        - zap
    test4: {}
  vars:
    group_all_var: value
  children:
    other_group:
      children:
        group_x:
          hosts: test5
        group_y:
          hosts:
            test6: {}
      vars:
        g2_var2: value3
      hosts:
        test4:
          ansible_host: 127.0.0.1
    last_group:
      tags:
        - foo
      hosts: test3
      vars:
        group_last_var: value

You could just preprocess that hosts-config using a dynamic inventory or use jq to filter by tag:

(I use yq to convert first to JSON; yq itself has not the full functionality of jq; best probably would be to just use oq):

yq -ojson . test.yaml \
    | jq -re '{} as $n|path(..) as $p|getpath($p)|objects|select(.tags|IN(["foo"]))|del(.tags)|. as $o|$n|setpath($p;$o)' \
    | jq -n 'reduce inputs as $item ({}; . *= $item)'

OUTPUT

                           
{                                                                                                                                                                                              
  "all": {                                                                                                                                                                                     
    "hosts": {                                                                                                                                                                                 
      "test2": {                                                                                                                                                                               
        "host_var": "value",                                                                                                                                                                   
        "tags": [                                                                                                                                                                              
          "foo"                                                                                                                                                                                
        ]                                                                                                                                                                                      
      }                                                                                                                                                                                        
    },                                                                                                                                                                                         
    "children": {                                                                                                                                                                              
      "last_group": {                                                                                                                                                                          
        "tags": [                                                                                                                                                                              
          "foo"                                                                                                                                                                                
        ],                                                                                                                                                                                     
        "hosts": "test3",                                                                                                                                                                      
        "vars": {                                                                                                                                                                              
          "group_last_var": "value"                                                                                                                                                            
        }                                                                                                                                                                                      
      }                                                                                                                                                                                        
    }                                                                                                                                                                                          
  }                                                                                                                                                                                            
}                                                                                                                                                                                              

And then you could just pack that into a shell-script called ansible-filterhosts and just do…

ansible-filterhosts -t foo hosts.yaml | ansible-playbook -i /dev/stdin myplaybook.yaml

…or…

ansible-playbook -i <(ansible-filterhosts -t foo hosts.yaml) myplaybook.yaml

…or develop a plugin for ansible itself ;)

Upvotes: 0

Robert Cutajar
Robert Cutajar

Reputation: 3731

As other's pointed out, tags are not applicable to hosts. But...

I've found that you can use variables to filter hosts.

You can also put your hosts into multiple groups. Whether you do it dynamically, up to you.

Upvotes: 0

Vladimir Botka
Vladimir Botka

Reputation: 68254

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.

Upvotes: 10

erik258
erik258

Reputation: 16302

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

Upvotes: 4

Related Questions