# Using ansible and EC2 to create instances... exact\_count not being honored (sort of)

**URL:** <https://forum.ansible.com/t/using-ansible-and-ec2-to-create-instances-exact-count-not-being-honored-sort-of/26067>\
**Category:** Ansible Project\
**Created:** [November 17, 2017, 8:24pm UTC](https://forum.ansible.com/t/using-ansible-and-ec2-to-create-instances-exact-count-not-being-honored-sort-of/26067 "2017-11-17T20:24:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [November 17, 2017, 8:24pm UTC](https://forum.ansible.com/t/using-ansible-and-ec2-to-create-instances-exact-count-not-being-honored-sort-of/26067/1 "2017-11-17T20:24:59Z")

</div>

I’m trying to get ansible to create ec2 instances using the following play…works great the first time but there is an issue on subsequent runs…

When first run, the host gets created, tagged,etc as expected. But when the playbook is run again, a new instance is spun up if the vpc\_subnet\_id is not the same as the original instance (creation gets skipped as expected if the subnet is the same). I have 3 availability zones in every vpc, and it doesn’t matter which zone the instance is created in, but I only want one instance of “instance\_profile\_name” per vpc…

Is there way that I’m missing so that “exact\_count” and “count\_tag” will take into account the entire vpc and not the specific subnet that is returned during the facts gathering?

ansible 2.4.1.0  
config file = ./ansible.cfg  
ansible python module location = /usr/lib/python2.7/site-packages/ansible

executable location = /usr/bin/ansible  
python version = 2.7.14 (default, Nov 2 2017, 18:42:05) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]

- name: Gather subnet facts  
ec2\_vpc\_subnet\_facts:  
profile: “{{ aws\_account\_name }}”  
region: “{{ region }}”  
filters:  
vpc-id: “{{ vpc\_id }}”  
“tag:Public”: “True”  
register: subnet\_facts

- name: Create the EC2 instance  
ec2:  
profile: “{{ aws\_account\_name }}”  
region: “{{ region }}”  
vpc\_subnet\_id: “{{ (subnet\_facts.subnets | random).id }}”

image: “{{ (ami\_search.results | first).ami\_id }}”  
instance\_type: “{{ instance\_type }}”  
key\_name: “{{ keypair\_name }}”  
instance\_profile\_name: “{{ instance\_profile\_name }}”  
volumes:

- device\_name: “{{ root\_device\_name }}”  
volume\_type: gp2  
volume\_size: 20  
delete\_on\_termination: yes  
assign\_public\_ip: yes  
group: “{{ security\_groups }}”  
instance\_tags:  
Name: “[{{ vpc\_name }}] - {{ inventory\_hostname.split(‘.’) | first }}”  
hostname: “{{ inventory\_hostname }}”  
exact\_count: 1  
count\_tag:  
Name: “[{{ vpc\_name }}] - {{ inventory\_hostname.split(‘.’) | first }}”  
hostname: “{{ inventory\_hostname }}”  
termination\_protection: yes  
monitoring: no  
wait: yes
