# Assigning Groups Based on Dynamic Inventory Source

**URL:** <https://forum.ansible.com/t/assigning-groups-based-on-dynamic-inventory-source/21599>\
**Category:** Ansible Project\
**Created:** [May 29, 2016, 8:37pm UTC](https://forum.ansible.com/t/assigning-groups-based-on-dynamic-inventory-source/21599 "2016-05-29T20:37:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Rozeboom](https://avatars.discourse-cdn.com/v4/letter/d/35a633/32.png) [@Daniel\_Rozeboom](https://forum.ansible.com/u/Daniel_Rozeboom)\
**Post date:** [May 29, 2016, 8:37pm UTC](https://forum.ansible.com/t/assigning-groups-based-on-dynamic-inventory-source/21599/1 "2016-05-29T20:37:28Z")

</div>

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](https://github.com/ansible/ansible/blob/devel/contrib/inventory/digital_ocean.py). 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
