# Managing groups of dynamic hosts

**URL:** <https://forum.ansible.com/t/managing-groups-of-dynamic-hosts/14852>\
**Category:** Ansible Project\
**Created:** [March 25, 2014, 12:54pm UTC](https://forum.ansible.com/t/managing-groups-of-dynamic-hosts/14852 "2014-03-25T12:54:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deyna\_Cegielski](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Deyna\_Cegielski](https://forum.ansible.com/u/Deyna_Cegielski)\
**Post date:** [March 25, 2014, 12:54pm UTC](https://forum.ansible.com/t/managing-groups-of-dynamic-hosts/14852/1 "2014-03-25T12:54:57Z")

</div>

Hey guys,

I’ve written a playbook that defines the configuration policies for an “application server” running nodejs. I’ve been using this playbook to provision a vagrant box (local sandbox) with great success but now I want to use this playbook to provision sandboxes in the cloud.

Ultimately I’d like to be able to spin up and provision an infinite number of environments, each existing as their own subdomain e.g. sandbox1.mydomain,com [sandbox2.mydomain.com](http://sandbox2.mydomain.com). I’ve looked into spinning up instances and dynamically so that they are added to my inventory allowing me to then target them for provisioning easily:

[appservers]  
1.2.3.4  
5.6.7.8

— hosts: appservers  
roles: nodejs

However, how do I distinguish between sandbox1 and sandbox2 in the inventory file whilst maintaining the “appserver” policy? I would like the run a deployment script that only targets the specified sandbox? I was thinking something along the lines of:

[appservers]  
1.2.3.4  
5.6.7.8

[sandbox1]  
1.2.3.4

[sandbox2]  
5.6.7.8

And then I can target --limit sandbox1 to only run the deployment task against this group.

Is this a valid way of achieving my goal?

---

<div class="post-metadata">

**Author:** ![Paul\_Durivage](https://avatars.discourse-cdn.com/v4/letter/p/94ad74/32.png) [@Paul\_Durivage](https://forum.ansible.com/u/Paul_Durivage)\
**Post date:** [March 25, 2014, 9:15pm UTC](https://forum.ansible.com/t/managing-groups-of-dynamic-hosts/14852/2 "2014-03-25T21:15:40Z")

</div>

So,

I don’t think I can exactly answer your question but I think I can get you in the right direction.

So if you were using cloud instead of Vagrant, I’d tell you to use the dynamic inventory scripts. For example, the rax.py dynamic inventory plugin groups hosts both logically and dynamically at runtime. There is a Vagrant pluging that also queries for dynamic inventory, but according to the doucmentation, all it does it return a single group called “vagrant”. The Vagrant module is here: [https://github.com/ansible/ansible/blob/devel/plugins/inventory/vagrant.py](https://github.com/ansible/ansible/blob/devel/plugins/inventory/vagrant.py)

If you’re doing your work all at once in a single ansible-playbook run (provisioning and bootstrapping), then you can use the add\_host module to assign the hosts that you provision via Ansible to an arbitrary group or groups, which you can then target with additional work.  
[http://docs.ansible.com/add\_host\_module.html](http://docs.ansible.com/add_host_module.html)

Perhaps combining the inventory script and the add\_host module you may find a solution – but AFAIK, there’s not a solution that will do exactly what you’re looking for.

---

<div class="post-metadata">

**Author:** ![tannerjc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tannerjc/32/985_2.png) [@tannerjc](https://forum.ansible.com/u/tannerjc)\
**Post date:** [March 25, 2014, 9:22pm UTC](https://forum.ansible.com/t/managing-groups-of-dynamic-hosts/14852/3 "2014-03-25T21:22:27Z")

</div>

Not sure I’m understanding the original question, but my advice for the cloud scenario is to use complex tagging.

The ec2 module in particular can create instances by an exact\_count which is based on a dictionary of tags.

# instances with tags foo & bar & baz=bang

count\_tag:

- foo
- bar
- baz: bang
