Simple playbook to start an ec2 instance

I’m new to Ansbile and trying to start a simple EC2 instance.

I’m trying to run my Ansbile playbook from an Amazon EC2 VM (host name is cfgsvr). Please let me know what am I missing here.

Here is my directory structure

  • launch_ec2
  • setup.yml
  • vars
  • main.yml
  • tasks
  • setup.yml

This is how my launch_ec2/vars/main.yml looks like

Hi Soumya,

Looks like there are a couple of problems here. First of all, you
should note the with_items and include combination is not supported
and will be removed, so you should not use that. Instead, use
with_items in conjunction with the actual ec2 module call.

Second of all, it looks like your configuration server can't connect
to AWS. I am not entirely sure why, but you should troubleshoot that
independently, maybe with the EC2 command line tools. Maybe it is a
boto configuration problem. Instead of ~/.boto, you can pass the
access key and secret key on the ec2 module's parameters, or set
environment variables.

Thirdly, have you see this example?

https://github.com/ansible/ansible-examples/blob/master/language_features/eucalyptus-ec2.yml

It may be helpful.

Good luck,

-Tim

Thanks Tim. I was able to start up EC2 instances by following the example you posted in the link below.

https://github.com/ansible/ansible-examples/blob/master/language_features/eucalyptus-ec2.yml

However, I’m having trouble connecting to the instances after they are created.

This is my playbook.

  • name: Stage instance(s)
    hosts: cfgsvr
    connection: local
    user: myuserid
    gather_facts: false

vars:
keypair: mykeypair
image: ami-2efa9d47

instance_type: m1.small #t1.micro
subnet: subnet-xxxxxx
region: us-east-1
image: ami-2efa9d47

Launch 1 instance with the following parameters. Register the output.

tasks:

  • name: Launch instance
    local_action: ec2 keypair={{keypair}} vpc_subnet_id={{subnet}} instance_type={{instance_type}} image={{image}} wait=true count=1
    register: ec2

Use with_items to add each instances 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.public_ip}} groupname=deploy
    with_items: ${ec2.instances}

  • name: Wait for the instances to boot by checking the ssh port
    local_action: wait_for host={{item.public_dns_name}} port=22 delay=60 timeout=320 state=started
    with_items: ${ec2.instances}

  • name: Breathing room
    pause: seconds=30

This play targets the new host group

  • name: Configure instance
    hosts: deploy #must match groupname in “add_host” above
    user: ubuntu
    sudo: yes
    gather_facts: true

Install the necessary software on each instance

tasks:

  • name: Get the latest updates for instance
    action: command apt-get update

  • name: Install JDK
    apt: pkg=openjdk-6-jre-headless state=latest install_recommends=no update_cache=yes
    #action: apt pkg=java-1.7.0-openjdk state=latest

  • name: Install Maven2
    apt: pkg=maven2 state=latest update_cache=yes

I’m launching the playbook using the following command.

$ansible-playbook -v ec2_launch.yml -vvvv -i inventory/ansible_hosts –private-key=/path/to/private/key

GATHERING FACTS ***************************************************************

ESTABLISH CONNECTION FOR USER: ubuntu
EXEC [‘ssh’, ‘-tt’, ‘-vvv’, ‘-o’, ‘ControlMaster=auto’, ‘-o’, ‘ControlPersist=60s’, ‘-o’, ‘ControlPath=/home/dhkarimi/.ansible/cp/ansible-ssh-%h-%p-%r’, ‘-o’, ‘Port=22’, ‘-o’, ‘IdentityFile=/home/myuserid/.ec2/myprivatekey.pem’, ‘-o’, ‘KbdInteractiveAuthentication=no’, ‘-o’, ‘PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey’, ‘-o’, ‘PasswordAuthentication=no’, ‘-o’, ‘User=ubuntu’, ‘-o’, ‘ConnectTimeout=10’, u’None’, “/bin/sh -c ‘mkdir -p $HOME/.ansible/tmp/ansible-1382270163.81-64347444346956 && chmod a+rx $HOME/.ansible/tmp/ansible-1382270163.81-64347444346956 && echo $HOME/.ansible/tmp/ansible-1382270163.81-64347444346956’”]
fatal: [None] => SSH encountered an unknown error. The output was:
OpenSSH_5.9p1 Debian-5ubuntu1, OpenSSL 1.0.1 14 Mar 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: auto-mux: Trying existing master
debug1: Control socket “/home/myuserid/.ansible/cp/ansible-ssh-None-22-ubuntu” does not exist
debug2: ssh_connect: needpriv 0
ssh: Could not resolve hostname None: Name or service not known

TASK: [Get the latest updates for instance] ***********************************
FATAL: no hosts matched or all hosts have already failed – aborting

I can log into the launched instance by using the private key I’m passing to the ansible-playbook command.

$ansible-playbook -v ec2_launch.yml -vvvv -i inventory/ansible_hosts –private-key=/path/to/private/key

Any idea why is this failing?

thanks
-Soumya