Using ansible and EC2 to create instances... exact_count not being honored (sort of)

64 views
Skip to first unread message

Dayton Jones

unread,
Nov 17, 2017, 3:24:59 PM11/17/17
to Ansible Project
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

Reply all
Reply to author
Forward
0 new messages