# create multiple ec2 instances assign statis IP and tags

**URL:** <https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236>\
**Category:** Ansible Project\
**Created:** [January 21, 2022, 2:28pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236 "2022-01-21T14:28:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 21, 2022, 2:28pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/1 "2022-01-21T14:28:45Z")

</div>

hi

any idea how I can create multiple ec2 instances and tag it

main.yml

- include\_vars: vars/main.yml
- name: create the ec2 instance  
ec2:  
assign\_public\_ip: no  
group\_id: ‘{{ deploy\_env.sg\_group }}’  
instance\_type: “{{ deploy\_env.instance\_type }}”  
image: “{{ deploy\_env.image }}”  
wait: true  
wait\_timeout: 600  
count: 10  
region: “{{ deploy\_env.region }}”  
vpc\_subnet\_id: “{{ deploy\_env.vpc\_subnet\_id}}”  
private\_ip: “{{ deploy\_env.assign\_ip }}”  
instance\_tags:  
Name: “{{ deploy\_env.instance\_tags.name }}”

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 22, 2022, 4:42pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/2 "2022-01-22T16:42:56Z")

</div>

any idea? where do i put my ec2 tag names and assign to the instances

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 22, 2022, 5:19pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/3 "2022-01-22T17:19:07Z")

</div>

Depends on how many more requirements you come up with in every new mail

---

<div class="post-metadata">

**Author:** ![Evan\_Hisey](https://avatars.discourse-cdn.com/v4/letter/e/ce73a5/32.png) [@Evan\_Hisey](https://forum.ansible.com/u/Evan_Hisey)\
**Post date:** [January 22, 2022, 5:44pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/4 "2022-01-22T17:44:29Z")

</div>

I would say you are 95% done. You are just missing a loop and list of lists. Looking at the snippet, I think you may be making life a touch more difficult than needed. Are you writing a role or a playbook? What is your programing/ansible experience level? I see a similar mistakes in other emails you have posted, so trying to gauge answer level, as what you seem to be doing is while not advanced, also not trivial as it requires a decent understanding of aws, lists, ansible and the limits of loops.

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 23, 2022, 3:01pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/5 "2022-01-23T15:01:27Z")

</div>

I am just trying to create 2 or more ec2 instances. I need to assign them static ips and unique tag names. the playbook I created can only do one at a time and I am not sure how to modify it to do multiple

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 24, 2022, 8:51am UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/6 "2022-01-24T08:51:01Z")

</div>

Hii

> I am just trying to create 2 or more ec2 instances.

Here you say 2

> > > - name: create the ec2 instance  
> > > ec2:  
> > > assign\_public\_ip: no  
> > > group\_id: ‘{{ deploy\_env.sg\_group }}’  
> > > instance\_type: “{{ deploy\_env.instance\_type }}”  
> > > image: “{{ deploy\_env.image }}”  
> > > wait: true  
> > > wait\_timeout: 600  
> > > count: 10

But here it says 10 are created.  
[https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2\_module.html#parameter-count](https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_module.html#parameter-count)

Assuming this is an oversight, this should work:

- include\_vars: vars/main.yml

- name: create the ec2 instance

ec2:

assign\_public\_ip: no

group\_id: ‘{{ deploy\_env.sg\_group }}’

instance\_type: “{{ deploy\_env.instance\_type }}”

image: “{{ deploy\_env.image }}”

wait: true

wait\_timeout: 600

count: 1

region: “{{ deploy\_env.region }}”

vpc\_subnet\_id: “{{ deploy\_env.vpc\_subnet\_id}}”

private\_ip: “{{ item.ip }}”

instance\_tags:

Name: “{{ item.name }}”

loop: “{{ instances }}”

And then in your vars file:

deploy\_env:

instance\_type: t2.micro

image: ami-02d03ce209db75523

sg\_group:

- “sg-057771872265bfda6”

region: us-west-1

vpc\_subnet\_id: subnet-0d9d37440a2265163

instances:

- name: test1

ip: 10.10.1.10

- name: test2

ip: 10.10.1.12

- name: test43

ip: 10.10.1.77

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [January 25, 2022, 5:45pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/7 "2022-01-25T17:45:41Z")

</div>

Thank you. This works great.

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [February 8, 2022, 3:05pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/8 "2022-02-08T15:05:16Z")

</div>

hi,

any idea why I keep getting this error when i run this asyn job?

fatal: [localhost]: FAILED! =\> {  
“ansible\_job\_id”: “ec2.ansible\_job\_id”,  
“attempts”: 1,  
“changed”: false,  
“finished”: 1,  
“invocation”: {  
“module\_args”: {  
“\_async\_dir”: “/Users/tonywong/.ansible\_async”,  
“jid”: “ec2.ansible\_job\_id”,  
“mode”: “status”  
}  
},  
“msg”: “could not find job”,  
“started”: 1  
}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 8, 2022, 3:11pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/9 "2022-02-08T15:11:14Z")

</div>

yes

You loop over the ec2\_instance task.  
So, you will also need to loop over the result.

I think this should work:

- name: wait for instance creation to complete  
async\_status:  
jid: “{{ item.ansible\_job\_id }}”  
register: ec2\_jobs  
until: ec2\_jobs.finished  
retries: 300  
loop: “{{ ec2.results }}”

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [February 8, 2022, 3:55pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/10 "2022-02-08T15:55:53Z")

</div>

awesome. Thanks.

so is there a way to have my inventory file in its own separate text file.?

I have it all in the vars.yml file now?

deploy\_env:  
instance\_type: t2.micro  
image: ami-02d03ce209db75523  
sg\_group:

- “sg-057771872265bfda6”  
region: us-west-1  
vpc\_subnet\_id: subnet-0d9d37440a2265163  
instance\_tags:  
date: 1.20.2022

instances:

- name: test1  
ip: 10.10.1.10
- name: test2  
ip: 10.10.1.11

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [February 8, 2022, 3:59pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/11 "2022-02-08T15:59:20Z")

</div>

also what if I got 100s servers to deploy but from different instance types/image\_ids/SGs/Subnets ?

would i have to create separate env variables for these?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 8, 2022, 4:04pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/12 "2022-02-08T16:04:37Z")

</div>

Just create another file and include that

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 8, 2022, 4:08pm UTC](https://forum.ansible.com/t/create-multiple-ec2-instances-assign-statis-ip-and-tags/36236/13 "2022-02-08T16:08:02Z")

</div>

You define what you need.  
Settle on a data structure that suits your use case.  
It all depends on how different each instance is.  
Is there no common pattern?  
You can supply defaults in the task, and then optionally have them in your instances list.

instances:

- name: foo  
type: t4g.nano  
ip: 10.1.1.1
- name: bar  
ip: 10.1.2.3

'etc etc
