Search code examples
amazon-ec2ansiblenetwork-interface

Ansible: ec2_eni cannot attach interface


Using ansible I am trying to create ec2 instances and attach an extra network interface to each instance so that they will have two private IP addresses. However, for some reason, it seems that the ec2_eni module can create network interfaces, but will not attach them to the instances specified. What am I doing wrong? Below is my playbook:

---
- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Create new servers
      ec2:
        region: "{{ region }}"
        vpc_subnet_id: "{{ subnet }}"
        group_id: "{{ sec_group }}"
        assign_public_ip: yes
        wait: true
        key_name: '{{ key }}'
        instance_type: t2.micro
        image: '{{ ami }}'
        exact_count: '{{ count }}'
        count_tag:
          Name: "{{ server_name }}"
        instance_tags:
          Name: "{{ server_name }}"
      register: ec2

    - name: Show ec2 instance json data
      debug:
        msg: "{{ ec2['tagged_instances'] }}"

    - name: allocate new elastic IPs and associate it with instances
      ec2_eip:
        region: "{{ region }}"
        device_id: "{{ item['id'] }}"
      with_items: "{{ ec2['tagged_instances'] }}"
      register: eips

    - name: Show eip instance json data
      debug:
        msg: "{{ eips['results'] }}"

    - ec2_eni:
        subnet_id: "{{ subnet }}"
        state: present
        secondary_private_ip_address_count: 1
        security_groups: "{{ sec_group }}"
        region: "{{ region }}"
        device_index: 1
        description: "test-eni"
        instance_id: "{{ item['id'] }}"
      with_items: "{{ ec2['tagged_instances'] }}"

The strange thing is that the ec2_eni task succeeds, saying that it has attached the network interface to each instance when in reality it just creates the network interface and then does nothing with it.


Solution

  • As best I can tell, since attached defaults to None, but the module docs say:

    Specifies if network interface should be attached or detached from instance. If ommited, attachment status won't change

    then the code does what they claim and skips the attachment step.

    This appears to be a bug in the documentation, which claims the default is 'yes' but is not accurate.