Tag Archives: ansible

Using Ansible on EC2

Using Peter Sankauskas EC2 inventory file.
Assuming Ansible is setup, see old post for setting up Ansible or here.

$ cd ansible 
$ export AWS_ACCESS_KEY_ID=aws_accesskey_id
$ export AWS_SECRET_ACCESS_KEY=aws_secret_key
$ export ANSIBLE_HOSTS=$(pwd)/plugins/inventory/ec2.py  
 # use the next line  only if you're using ssh as transport
$ export  ANSIBLE_SSH_ARGS="-o ForwardAgent=yes"  

The ansible/plugins/inventory/ec2.ini file is read by ec2.py. To limit it to us-east-1 only:

regions: us-east-1
cache_path: /tmp
cache_max_age: 300
destination_variable: public_dns_name
vpc_destination_variable: public_dns_name

Testing the inventory file:

$ ./plugins/inventory/ec2.py --list
  "i-xxxxx": [
  ""i-yyyyy": [
  "security-group_DEVGroup": [
   "us-east-1": [
  "us-east-1d": [

Now for Ansible simple commands:
Update: (11/16/2012) Use ssh agent for EC2 keypairs.

$ ssh-add /path-to/my-ec2-keypair 
$ source hacking/env-setup
$ ansible us-east-1d -u ubuntu -m ping 

ec2-xx-xx-xx-xx.compute-1.amazonaws.com | success >> {
    "ping": "pong"

ec2-yy-yy-yy-yy.compute-1.amazonaws.com | success >> {
    "ping": "pong"

Now for a sample playbook filenamed as sample.yml:

- hosts: i-xxxxx 

  - name: Do stuff
    action: command uptime 

And the command to run the playbook. Target machine is an Ubuntu Precise instance.

ansible-playbook sample.yml -u ubuntu  --verbose

PLAY [i-xxxxx] ********************* 

GATHERING FACTS ********************* 
ok: [ec2-xx-xx-xx-xx.compute-1.amazonaws.com]

TASK: [Do stuff] ********************* 
changed: [ec2-xx-xx-xx-xx.compute-1.amazonaws.com] => {"changed": true, "cmd": ["uptime"], "delta": "0:00:00.006583", "end": "2012-10-10 09:16:47.070108", "rc": 0, "start": "2012-10-10 09:16:47.063525", "stderr": "", "stdout": "09:16:47 up 1 days,  2:58,  1 user,  load average: 0.14, 0.05, 0.05"}

PLAY RECAP ********************* 
ec2-xx-xx-xx-xx.compute-1.amazonaws.com : ok=2    changed=1    unreachable=0    failed=0

For the complete examples and documentation EC2 inventory files, it is found here.

Update: Nov 22,2012
There’s also a very nice post about provisioning CentOS EC2 instance with Ansible by Jan-Piet Mens.

Ansible PPA for Ubuntu Precise now available

I’ve created Ansible’s Ubuntu PPA at https://launchpad.net/~rquillo/+archive/ansible

Might help others who are using Ubuntu distro.
Using Ubuntu Precise as overlord machine, you can install Ansible using the PPA with the commands:


 $ sudo add-apt-repository ppa:rquillo/ansible 
 $ sudo apt-get update
 $ sudo apt-get install ansible -y  
 $ echo localhost > myhosts 
 $ export  ANSIBLE_HOSTS=$(pwd)/myhosts 
 $ ansible all -m ping -u ubuntu

There you go! Happy Ansibling Ubuntu users!