# Simple playbook to start an ec2 instance

**URL:** <https://forum.ansible.com/t/simple-playbook-to-start-an-ec2-instance/13719>\
**Category:** Ansible Project\
**Created:** [October 18, 2013, 8:52pm UTC](https://forum.ansible.com/t/simple-playbook-to-start-an-ec2-instance/13719 "2013-10-18T20:52:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soumya\_Simanta](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Soumya\_Simanta](https://forum.ansible.com/u/Soumya_Simanta)\
**Post date:** [October 18, 2013, 8:52pm UTC](https://forum.ansible.com/t/simple-playbook-to-start-an-ec2-instance/13719/1 "2013-10-18T20:52:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Timothy\_Gerla](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Timothy\_Gerla](https://forum.ansible.com/u/Timothy_Gerla)\
**Post date:** [October 18, 2013, 9:08pm UTC](https://forum.ansible.com/t/simple-playbook-to-start-an-ec2-instance/13719/2 "2013-10-18T21:08:00Z")

</div>

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](https://github.com/ansible/ansible-examples/blob/master/language_features/eucalyptus-ec2.yml)

It may be helpful.

Good luck,

-Tim

---

<div class="post-metadata">

**Author:** ![Soumya\_Simanta](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Soumya\_Simanta](https://forum.ansible.com/u/Soumya_Simanta)\
**Post date:** [October 21, 2013, 3:46am UTC](https://forum.ansible.com/t/simple-playbook-to-start-an-ec2-instance/13719/3 "2013-10-21T03:46:14Z")

</div>

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](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
