I have an Ansible inventory inventory.ini
structured as follows (shortened for the example):
[prod:children]
prod_first_plateform
[prod_first_plateform:children]
database_prod_first_plateform
embedded_prod_first_plateform
ldap_prod_first_plateform
proxy_prod_first_plateform
[database_prod_first_plateform]
serverA team=developer
serverZ team=integrator
[embedded_prod_first_plateform]
serverB team=integrator
serverY team=integrator
[ldap_prod_first_plateform]
serverC team=developer
serverX team=integrator
[proxy_prod_first_plateform]
serverD team=developer
serverW team=developer
The requirement is as follows:
I would like to use this inventory in different contexts:
hosts: all
)hosts: prod_first_plateform
, in this example)team
host variable, while adhering to this inventory syntax, without adding a new host group.My issue lies with this third point.
My idea would be to use a syntax which works in my playbook like hosts: all[team=developer]
.
I know this syntax doesn't work, but I try to found a correct way to achieve that.
Is this currently possible with this inventory?
You can use the group_by
module in a preliminary play in order to create yourself groups from a variable.
Given:
- hosts: all
gather_facts: false
tasks:
- group_by:
key: "hosts_of_team_{{ team }}"
when: team is defined
- hosts: hosts_of_team_developer
gather_facts: false
tasks:
- debug:
var: ansible_play_hosts
run_once: true
Running it on your inventory, the second play would yield:
ok: [serverA] =>
ansible_play_hosts:
- serverA
- serverC
- serverD
- serverW
As expected, since we are targeting the hosts where the variable team
equal developer
via hosts: hosts_of_team_developer
.