# How to launch multiple EC2 hosts using ansible

**URL:** <https://forum.ansible.com/t/how-to-launch-multiple-ec2-hosts-using-ansible/27341>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [April 22, 2018, 3:12pm UTC](https://forum.ansible.com/t/how-to-launch-multiple-ec2-hosts-using-ansible/27341 "2018-04-22T15:12:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Love](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Love](https://forum.ansible.com/u/Love)\
**Post date:** [April 22, 2018, 3:12pm UTC](https://forum.ansible.com/t/how-to-launch-multiple-ec2-hosts-using-ansible/27341/1 "2018-04-22T15:12:46Z")

</div>

Dear Experts,

How to launch multiple EC2 instances using ansible?

I want to create an environment in AWS using below playbook but I see only one instance is being created when I execute below playbook.

Could you please help with missing code/syntax here?

Note: I’m using free account with AWS.

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [April 22, 2018, 4:26pm UTC](https://forum.ansible.com/t/how-to-launch-multiple-ec2-hosts-using-ansible/27341/2 "2018-04-22T16:26:46Z")

</div>

I think this is because you’ve defined the desired count in your vars but not actually in the invocation of the ec2 module.

Something like this should work as you expect:

- name: create webserver instances  
hosts: localhost  
connection: local  
gather\_facts: false

vars:  
ec2\_instance\_count: 2  
ec2\_instance\_tag:  
name: webserver

tasks:

- name: provision ec2 webserver instances  
ec2:  
aws\_access\_key: “{{ aws\_access\_key }}”  
aws\_secret\_key: “{{ aws\_secret\_key }}”  
count\_tag: “{{ ec2\_instance\_tag }}”  
ec2\_url: “{{ ec2\_url }}”  
exact\_count: “{{ ec2\_instance\_count }}”  
groups: “{{ ec2\_security\_group }}”  
key\_name: “{{ ec2\_key\_name }}”  
image: “{{ ec2\_image }}”  
instance\_tags: “{{ ec2\_instance\_tag }}”  
instance\_type: “{{ ec2\_instance\_type }}”  
region: “{{ ec2\_region }}”  
wait: true  
register: ec2

Cheers,

Will.

---

<div class="post-metadata">

**Author:** ![Love](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Love](https://forum.ansible.com/u/Love)\
**Post date:** [April 23, 2018, 4:09am UTC](https://forum.ansible.com/t/how-to-launch-multiple-ec2-hosts-using-ansible/27341/3 "2018-04-23T04:09:00Z")

</div>

Hi Will,

Updated changes but see an error, could you please let me know what is missing? Inserted code for your reference.

fatal: [localhost]: FAILED! =\> {“changed”: false, “msg”: “argument instance\_tags is of type \<type ‘str’\> and we were unable to convert to dict: dictionary requested, could not parse JSON or key=value”}

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [April 23, 2018, 10:09am UTC](https://forum.ansible.com/t/how-to-launch-multiple-ec2-hosts-using-ansible/27341/4 "2018-04-23T10:09:25Z")

</div>

Your ec2\_instance\_tag ‘name:webserver’ is being evaluated as a string because there’s no space, it needs to be a YAML dictionary.

If you read the docs on the module (ansible-doc ec2), they state:

> - instance\_tags  
> **a hash/dictionary of tags** to add to the new instance or for starting/stopping instance by tag; ‘{“key”:“value”}’ and  
> ‘{“key”:“value”,“key”:“value”}’  
> [Default: None]  
> version\_added: 1.0

And if you pay attention to: [http://docs.ansible.com/ansible/latest/reference\_appendices/YAMLSyntax.html](http://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html)

> _A dictionary is represented in a simple `key: value` form (the colon must be followed by a space):_

You have:

ec2\_instance\_tag:  
name:webserver

This should be:

ec2\_instance\_tag:  
name: webserver  
^

---

<div class="post-metadata">

**Author:** ![Love](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@Love](https://forum.ansible.com/u/Love)\
**Post date:** [April 24, 2018, 12:08am UTC](https://forum.ansible.com/t/how-to-launch-multiple-ec2-hosts-using-ansible/27341/5 "2018-04-24T00:08:19Z")

</div>

That worked, thank you Will.
