Managing groups of dynamic hosts

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. 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?

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

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

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.

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