Hi all,
I’m new to Ansible, so please forgive me if this is a silly question. I’m working on loading inventory from multiple sources, one of which is dynamic using the DigitalOcean inventory script. I am able to successfully run this and have Ansible load my cloud hosts into the inventory. Sweet!!!
My issue is that I would like to assign a group based on the source of the dynamically loaded hosts, e.g. everything loaded with the digital_ocean.py script belongs to a group “digital_ocean”, everything loaded from an ec2.py script would belong to “ec2”, etc… I would be OK if the group name was assigned directly from the dynamic inventory script file name, but more likely I imagine it could be specified as a setting in the .ini file associated with the dynamic inventory script.
I am testing this behavior by running this command:
ansible all -m debug -a msg="{{ hostvars[inventory_hostname]['group_names'] }}"
… and I currently get output like this:
`
1.2.3.4 | SUCCESS => {
“msg”: [
“123456”,
“distro_Ubuntu”,
“… other cool stuff …”
]
}
…
`
I’m wondering if there is any variable I can set, either in digital_ocean.ini or in my Ansible config, to get output like this:
`
1.2.3.4 | SUCCESS => {
“msg”: [
“digital_ocean”,
“123456”,
“distro_Ubuntu”,
“… other cool stuff …”
]
}
…
`
I have tried using the “group_variables” setting in digital_ocean.ini but I haven’t been able to coax it into doing what I want.
I know I could modify my copy of digital_ocean.py manually to add the behavior I want, but I would prefer to get this happening in a more clean and sustainable way. If this behavior isn’t currently supported and the inventory script is the right place for the change (rather than a setting or modification for the core of Ansible), I would be more than happy to take a shot at making the necessary changes to digital_ocean.py and submitting a pull request to offer the contribution up for consideration.
Please let me know if this question makes sense and perhaps I’m missing an obvious way to do this, or let me know if I can provide any other information to help discuss. Thanks in advance, and I’m loving the framework so far!!
- Daniel