Skip to content

Instantly share code, notes, and snippets.

@chadoh
Created June 14, 2013 15:12
Show Gist options
  • Select an option

  • Save chadoh/5782635 to your computer and use it in GitHub Desktop.

Select an option

Save chadoh/5782635 to your computer and use it in GitHub Desktop.
---
# Run this with ansible-playbook and supply the private key for your EC2/Euca
# user (to access the instance in the second play), e.g:
# ansible-playbook eucalyptus-ec2-deploy.yml -v --private-key=/path/to/ec2/pri/key
- name: Set up a new ubuntu instance and register it in your ansible inventory file
hosts: local
connection: local
user: root
gather_facts: false
vars:
keypair: pd-app-server
instance_type: c1.medium
security_group: default
image: ami-d0f89fb9 # default ubuntu, provided by the aws wizard
tasks:
# Launch 1 instances with the following parameters. Register the output.
- name: Launch instance
local_action: ec2 key_name={{ keypair }} group={{ security_group }}
instance_type={{ instance_type }} image={{ image }}
instance_tags='{"Name":"{{ instance_name }}", "role":"dev"}'
wait=yes count=1
register: ec2
# Use with_items to add each instance's public IP to a new hostgroup for
# use in the next play.
- name: Add new instances to host group
local_action: add_host hostname={{item.dns_name}} groups=dev
ansible_ssh_user=ubuntu
with_items: ${ec2.instances}
# Use the ec2_vol module to create volumes for attachment to each instance.
# Use with_items to attach to each instance (by returned id) launched
# previously.
#- name: Create a volume and attach
#local_action: ec2_vol volume_size=10 instance={{item.id}}
#with_items: ${ec2.instances}
- name: provision the newly created instance
hosts: dev
vars:
ruby: ree-1.8.7-2012.02
prince: prince_8.1-5_ubuntu12.04_amd64.deb
phantomjs: phantomjs-1.9.0-linux-x86_64
tasks:
- include: ../tasks/apt.yml
- include: ../tasks/ruby.yml
- include: ../tasks/shell_setup.yml
- include: ../tasks/db.yml
- include: ../tasks/prince.yml
- include: ../tasks/phantomjs.yml
@chadoh
Copy link
Copy Markdown
Author

chadoh commented Jun 14, 2013

Output when running it:

 ⮁ ansible-playbook ansible/playbooks/launch_ubuntu_instance.yml -vvv --private-key=~/.ssh/pd-app-server --extra-vars "instance_name=generic-dev"


 ____________________________________________________________
/ PLAY [Set up a new ubuntu instance and register it in your \
\ ansible inventory file]                                    /
 ------------------------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


 _______________________
< TASK: Launch instance >
 -----------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


<127.0.0.1> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-1371225156.01-231929362694114 && chmod a+rx $HOME/.ansible/tmp/ansible-1371225156.01-231929362694114 && echo $HOME/.ansible/tmp/ansible-1371225156.01-231929362694114']
<127.0.0.1> REMOTE_MODULE ec2 key_name=pd-app-server group=default instance_type=c1.medium image=ami-d0f89fb9 instance_tags='{"Name":"generic-dev", "role":"dev"}' wait=yes count=1 wait_timeout=960
<127.0.0.1> PUT /var/folders/jp/ptnscfcs3q3g6hbym2ml9ncm0000gn/T/tmpYaeItR TO /Users/chadoh/.ansible/tmp/ansible-1371225156.01-231929362694114/ec2
<127.0.0.1> EXEC ['/bin/sh', '-c', '/usr/bin/python /Users/chadoh/.ansible/tmp/ansible-1371225156.01-231929362694114/ec2; rm -rf /Users/chadoh/.ansible/tmp/ansible-1371225156.01-231929362694114/ >/dev/null 2>&1']
changed: [localhost] => {"changed": true, "instances": [{"ami_launch_index": "0", "architecture": "x86_64", "dns_name": "ec2-54-234-146-56.compute-1.amazonaws.com", "hypervisor": "xen", "id": "i-d66ebcb7", "image_id": "ami-d0f89fb9", "instance_type": "c1.medium", "kernel": "aki-88aa75e1", "key_name": "pd-app-server", "launch_time": "2013-06-14T15:52:37.000Z", "placement": "us-east-1d", "private_dns_name": "ip-10-202-135-141.ec2.internal", "private_ip": "10.202.135.141", "public_dns_name": "ec2-54-234-146-56.compute-1.amazonaws.com", "public_ip": "54.234.146.56", "ramdisk": null, "root_device_name": "/dev/sda1", "root_device_type": "ebs", "state": "running", "state_code": 16, "virtualization_type": "paravirtual"}]}
 _______________________________________
< TASK: Add new instances to host group >
 ---------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


creating host via 'add_host': hostname=ec2-54-234-146-56.compute-1.amazonaws.com
added host to group via add_host module: dev
changed: [localhost] => (item={u'ramdisk': None, u'kernel': u'aki-88aa75e1', u'placement': u'us-east-1d', u'private_dns_name': u'ip-10-202-135-141.ec2.internal', u'ami_launch_index': u'0', u'instance_type': u'c1.medium', u'dns_name': u'ec2-54-234-146-56.compute-1.amazonaws.com', u'launch_time': u'2013-06-14T15:52:37.000Z', u'architecture': u'x86_64', u'virtualization_type': u'paravirtual', u'public_ip': u'54.234.146.56', u'image_id': u'ami-d0f89fb9', u'state': u'running', u'root_device_name': u'/dev/sda1', u'key_name': u'pd-app-server', u'public_dns_name': u'ec2-54-234-146-56.compute-1.amazonaws.com', u'root_device_type': u'ebs', u'state_code': 16, u'hypervisor': u'xen', u'id': u'i-d66ebcb7', u'private_ip': u'10.202.135.141'}) => {"changed": true, "item": {"ami_launch_index": "0", "architecture": "x86_64", "dns_name": "ec2-54-234-146-56.compute-1.amazonaws.com", "hypervisor": "xen", "id": "i-d66ebcb7", "image_id": "ami-d0f89fb9", "instance_type": "c1.medium", "kernel": "aki-88aa75e1", "key_name": "pd-app-server", "launch_time": "2013-06-14T15:52:37.000Z", "placement": "us-east-1d", "private_dns_name": "ip-10-202-135-141.ec2.internal", "private_ip": "10.202.135.141", "public_dns_name": "ec2-54-234-146-56.compute-1.amazonaws.com", "public_ip": "54.234.146.56", "ramdisk": null, "root_device_name": "/dev/sda1", "root_device_type": "ebs", "state": "running", "state_code": 16, "virtualization_type": "paravirtual"}, "new_groups": ["dev"], "new_host": "ec2-54-234-146-56.compute-1.amazonaws.com"}
 _____________________________________________
< PLAY [provision the newly created instance] >
 ---------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


 _________________
< GATHERING FACTS >
 -----------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


<ec2-54-234-146-56.compute-1.amazonaws.com> ESTABLISH CONNECTION FOR USER: ubuntu on PORT 22 TO ec2-54-234-146-56.compute-1.amazonaws.com
fatal: [ec2-54-234-146-56.compute-1.amazonaws.com] => {'msg': 'FAILED: [Errno 61] Connection refused', 'failed': True}
 ___________________________________
< TASK: Ensure we can add apt repos >
 -----------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


FATAL: no hosts matched or all hosts have already failed -- aborting

 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


           to retry, use: --limit @/var/tmp/ansible/launch_ubuntu_instance.retry

ec2-54-234-146-56.compute-1.amazonaws.com : ok=0    changed=0    unreachable=1    failed=0
localhost                  : ok=2    changed=2    unreachable=0    failed=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment