I have an inventory of mixed RHEL and Windows hosts requiring different Connection variable values. These hosts are sorted into a parent group (their team) and child group (their app).
I could have them in multiple groups: "RHEL"/"Windows" and their team. But I'd like to have them listed in my inventory once. I'd also like to avoid defining the same set of connection variables for every RHEL host or Windows host at the host level. There's one set of variables for rhel hosts, and one set of variables for windows hosts. I do not want to create child groups for the OS because I already have child groups for the host's app.
Can I simply set a variable (os: rhel, or os: windows) at the host level, and then later assign the connection variables based on the value of this os variable? This would need to be done before ansible tries to connect to the host.
So the logic would look something like this I think?:
- ansible reads inventory hosts based on host pattern
- prepare to execute on host1
- read os value on host1
- assign ansible connection variables for host1 based on os value for host1
- connect to host1 using these newly assigned variables
- execute playbook on host1
If I can do something like this, how?
edit:
A truncated example of the inventory I think I want to use:
---
TeamOne:
vars:
team:
name: <specific team named as set in a tool we use>
id: <id>
assignment_group: <value>
children:
AppOne:
hosts:
hostname1:
os: rhel
ansible_host: <ip address>
hostname2:
os: windows
ansible_host: <ip_address>
vars:
tags:
application_appid: <appid>
application_name: <appname>
AppTwo:
hosts:
hostname3:
os: rhel
ansible_host: <ip address>
hostname4:
os: windows
ansible_host: <ip_address>
vars:
tags:
application_appid: <appid>
application_name: <appname>
FoobarTeam:
vars:
team:
name: <specific team named as set in a tool we use>
id: <id>
assignment_group: <value>
children:
FooApp:
hosts:
foohost1:
os: rhel
ansible_host: <ip address>
foohost2:
os: windows
ansible_host: <ip_address>
vars:
tags:
application_appid: <appid>
application_name: <appname>
BarApp:
hosts:
barhost1:
os: rhel
ansible_host: <ip address>
barhost2:
os: windows
ansible_host: <ip_address>
vars:
tags:
application_appid: <appid>
application_name: <appname>
Now multiply that by about 20 (and growing) teams, each with a varying number of apps under them as child groups, with any number of hosts (some as low as 2, others as high as 50 or more). We're at 250 hosts and planning to have to onboard more.
I did the following in my playbook to accomplish this. The key points are to disable
gather_factsat the play level, set your ansible connection vars usingset_factsbased on the variable in your inventory, then manually rungather_factswith thesetupmodule. After that you can write the rest of your play.