creating and provisioning euca/ec2/cloud* instances using ansible in one step
October 25, 2012
So that if we shut things down or an instance dies – it can be brought back up automatically without someone having to explicitly say ‘go start this up’. I’ve also been working on setting up ansible such that we can run it via cron on our central command host to maintain our systems.
So, of course, I’ve combined those two things into one task :)
We want certain cloud instances to:
- always exist
- have the same ip each time they come up.
These are for things like the build master for COPRs/PPAs or for the jenkins-master server or for any number of random services where they need a persistent ip throughout recreations. For the ips we’re using euca-associate-address after allocating a number to our account.
So I started on a mechanism to create those instances and give me results I could use in ansible for other actions. I came up with ec2 which lets you do that from a local_action call in ansible.
Once you have that module in your ansible library path you just need to:
- add the host to your ansible inventory file
- take this playbook example
- modify it for your host
- run it like: ansible-playbook thatplaybook.yml
you’ll need euca2ools installed along with the deps for ansible.
Here’s what the playbook does:
- it looks to see if the host is running (by a simple nc connect to port 22)
- if it is not running then it will run ec2 with your args
- then associate the ip
- wait for the host to get the ip and become available
- then it goes on to the next play which is normal ansible provisioning
which is perfect, for the next time you run – steps 2-4 won’t happen – it just goes directly to provisioning. Since ansible playbooks are idempotent you can run them as many times as you want w/o maligning your server.
Now the example playbook is all merged together – but if you use host_vars in your ansible inventory and you setup the check/create tasks as an includable task file – then the playbook for creation and provisioning of any new cloud instance you want becomes VERY short.
More to come as I finish it.