amazon.aws.ec2_eni – Create and optionally attach an Elastic Network Interface (ENI) to an instance

Note

This plugin is part of the amazon.aws collection (version 1.4.0).

To install it use: ansible-galaxy collection install amazon.aws.

To use it in a playbook, specify: amazon.aws.ec2_eni.

New in version 1.0.0: of amazon.aws

Synopsis

  • Create and optionally attach an Elastic Network Interface (ENI) to an instance. If an ENI ID or private_ip is provided, the existing ENI (if any) will be modified. The ‘attached’ parameter controls the attachment status of the network interface.

Requirements

The below requirements are needed on the host that executes this module.

  • python >= 2.6

  • boto

Parameters

Parameter Choices/Defaults Comments
allow_reassignment
boolean
    Choices:
  • no ←
  • yes
Indicates whether to allow an IP address that is already assigned to another network interface or instance to be reassigned to the specified network interface.
attached
boolean
    Choices:
  • no
  • yes
Specifies if network interface should be attached or detached from instance. If omitted, attachment status won't change
aws_access_key
string
AWS access key. If not set then the value of the AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY or EC2_ACCESS_KEY environment variable is used.
If profile is set this parameter is ignored.
Passing the aws_access_key and profile options at the same time has been deprecated and the options will be made mutually exclusive after 2022-06-01.

aliases: ec2_access_key, access_key
aws_ca_bundle
path
The location of a CA Bundle to use when validating SSL certificates.
Only used for boto3 based modules.
Note: The CA Bundle is read 'module' side and may need to be explicitly copied from the controller if not run locally.
aws_config
dictionary
A dictionary to modify the botocore configuration.
Only the 'user_agent' key is used for boto modules. See http://boto.cloudhackers.com/en/latest/boto_config_tut.html#boto for more boto configuration.
aws_secret_key
string
AWS secret key. If not set then the value of the AWS_SECRET_ACCESS_KEY, AWS_SECRET_KEY, or EC2_SECRET_KEY environment variable is used.
If profile is set this parameter is ignored.
Passing the aws_secret_key and profile options at the same time has been deprecated and the options will be made mutually exclusive after 2022-06-01.

aliases: ec2_secret_key, secret_key
debug_botocore_endpoint_logs
boolean
    Choices:
  • no ←
  • yes
Use a botocore.endpoint logger to parse the unique (rather than total) "resource:action" API calls made during a task, outputing the set to the resource_actions key in the task results. Use the aws_resource_action callback to output to total list made during a playbook. The ANSIBLE_DEBUG_BOTOCORE_LOGS environment variable may also be used.
delete_on_termination
boolean
    Choices:
  • no
  • yes
Delete the interface when the instance it is attached to is terminated. You can only specify this flag when the interface is being modified, not on creation.
description
string
Optional description of the ENI.
device_index
integer
Default:
0
The index of the device for the network interface attachment on the instance.
ec2_url
string
Url to use to connect to EC2 or your Eucalyptus cloud (by default the module will use EC2 endpoints). Ignored for modules where region is required. Must be specified for all other modules if region is not used. If not set then the value of the EC2_URL environment variable, if any, is used.

aliases: aws_endpoint_url, endpoint_url
eni_id
string
The ID of the ENI (to modify).
If eni_id=None and state=present, a new eni will be created.
force_detach
boolean
    Choices:
  • no ←
  • yes
Force detachment of the interface. This applies either when explicitly detaching the interface by setting instance_id=None or when deleting an interface with state=absent.
instance_id
string
Instance ID that you wish to attach ENI to.
Since version 2.2, use the attached parameter to attach or detach an ENI. Prior to 2.2, to detach an ENI from an instance, use None.
name
string
Name for the ENI. This will create a tag called "Name" with the value assigned here.
This can be used in conjunction with subnet_id as another means of identifiying a network interface.
AWS does not enforce unique Name tags, so duplicate names are possible if you configure it that way. If that is the case, you will need to provide other identifying information such as private_ip_address or eni_id.
private_ip_address
string
Private IP address.
profile
string
Uses a boto profile. Only works with boto >= 2.24.0.
Using profile will override aws_access_key, aws_secret_key and security_token and support for passing them at the same time as profile has been deprecated.
aws_access_key, aws_secret_key and security_token will be made mutually exclusive with profile after 2022-06-01.

aliases: aws_profile
purge_secondary_private_ip_addresses
boolean
    Choices:
  • no ←
  • yes
To be used with secondary_private_ip_addresses to determine whether or not to remove any secondary IP addresses other than those specified.
Set secondary_private_ip_addresses=[] to purge all secondary addresses.
purge_tags
boolean
added in 1.3.0 of amazon.aws
    Choices:
  • no
  • yes ←
Indicates whether to remove tags not specified in tags or name. This means you have to specify all the desired tags on each task affecting a network interface.
If tags is omitted or None this option is disregarded.
region
string
The AWS region to use. If not specified then the value of the AWS_REGION or EC2_REGION environment variable, if any, is used. See http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region

aliases: aws_region, ec2_region
secondary_private_ip_address_count
integer
The number of secondary IP addresses to assign to the network interface. This option is mutually exclusive of secondary_private_ip_addresses
secondary_private_ip_addresses
list / elements=string
A list of IP addresses to assign as secondary IP addresses to the network interface. This option is mutually exclusive of secondary_private_ip_address_count
security_groups
list / elements=string
List of security groups associated with the interface. Only used when state=present.
Since version 2.2, you can specify security groups by ID or by name or a combination of both. Prior to 2.2, you can specify only by ID.
security_token
string
AWS STS security token. If not set then the value of the AWS_SECURITY_TOKEN or EC2_SECURITY_TOKEN environment variable is used.
If profile is set this parameter is ignored.
Passing the security_token and profile options at the same time has been deprecated and the options will be made mutually exclusive after 2022-06-01.

aliases: aws_security_token, access_token
source_dest_check
boolean
    Choices:
  • no
  • yes
By default, interfaces perform source/destination checks. NAT instances however need this check to be disabled. You can only specify this flag when the interface is being modified, not on creation.
state
string
    Choices:
  • present ←
  • absent
Create or delete ENI.
subnet_id
string
ID of subnet in which to create the ENI.
tags
dictionary
added in 1.3.0 of amazon.aws
A hash/dictionary of tags to add to the new ENI or to add/remove from an existing one. Please note that the name field sets the "Name" tag.
To clear all tags, set this option to an empty dictionary to use in conjunction with purge_tags. If you provide name, that tag will not be removed.
To prevent removing any tags set purge_tags to false.
validate_certs
boolean
    Choices:
  • no
  • yes ←
When set to "no", SSL certificates will not be validated for boto versions >= 2.6.0.

Notes

Note

  • This module identifies and ENI based on either the eni_id, a combination of private_ip_address and subnet_id, or a combination of instance_id and device_id. Any of these options will let you specify a particular ENI.

  • If parameters are not set within the module, the following environment variables can be used in decreasing order of precedence AWS_URL or EC2_URL, AWS_PROFILE or AWS_DEFAULT_PROFILE, AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY or EC2_ACCESS_KEY, AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY or EC2_SECRET_KEY, AWS_SECURITY_TOKEN or EC2_SECURITY_TOKEN, AWS_REGION or EC2_REGION, AWS_CA_BUNDLE

  • Ansible uses the boto configuration file (typically ~/.boto) if no credentials are provided. See https://boto.readthedocs.io/en/latest/boto_config_tut.html

  • AWS_REGION or EC2_REGION can be typically be used to specify the AWS region, when required, but this can also be configured in the boto config file

Examples

# Note: These examples do not set authentication details, see the AWS Guide for details.

# Create an ENI. As no security group is defined, ENI will be created in default security group
- amazon.aws.ec2_eni:
    private_ip_address: 172.31.0.20
    subnet_id: subnet-xxxxxxxx
    state: present

# Create an ENI and attach it to an instance
- amazon.aws.ec2_eni:
    instance_id: i-xxxxxxx
    device_index: 1
    private_ip_address: 172.31.0.20
    subnet_id: subnet-xxxxxxxx
    state: present

# Create an ENI with two secondary addresses
- amazon.aws.ec2_eni:
    subnet_id: subnet-xxxxxxxx
    state: present
    secondary_private_ip_address_count: 2

# Assign a secondary IP address to an existing ENI
# This will purge any existing IPs
- amazon.aws.ec2_eni:
    subnet_id: subnet-xxxxxxxx
    eni_id: eni-yyyyyyyy
    state: present
    secondary_private_ip_addresses:
      - 172.16.1.1

# Remove any secondary IP addresses from an existing ENI
- amazon.aws.ec2_eni:
    subnet_id: subnet-xxxxxxxx
    eni_id: eni-yyyyyyyy
    state: present
    secondary_private_ip_address_count: 0

# Destroy an ENI, detaching it from any instance if necessary
- amazon.aws.ec2_eni:
    eni_id: eni-xxxxxxx
    force_detach: true
    state: absent

# Update an ENI
- amazon.aws.ec2_eni:
    eni_id: eni-xxxxxxx
    description: "My new description"
    state: present

# Update an ENI using name and subnet_id
- amazon.aws.ec2_eni:
    name: eni-20
    subnet_id: subnet-xxxxxxx
    description: "My new description"
    state: present

# Update an ENI identifying it by private_ip_address and subnet_id
- amazon.aws.ec2_eni:
    subnet_id: subnet-xxxxxxx
    private_ip_address: 172.16.1.1
    description: "My new description"

# Detach an ENI from an instance
- amazon.aws.ec2_eni:
    eni_id: eni-xxxxxxx
    instance_id: None
    state: present

### Delete an interface on termination
# First create the interface
- amazon.aws.ec2_eni:
    instance_id: i-xxxxxxx
    device_index: 1
    private_ip_address: 172.31.0.20
    subnet_id: subnet-xxxxxxxx
    state: present
  register: eni

# Modify the interface to enable the delete_on_terminaton flag
- amazon.aws.ec2_eni:
    eni_id: "{{ eni.interface.id }}"
    delete_on_termination: true

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key Returned Description
interface
complex
when state != absent
Network interface attributes

 
description
string
success
interface description

Sample:
Firewall network interface
 
groups
list / elements=dictionary
success
list of security groups

Sample:
[{'sg-f8a8a9da': 'default'}]
 
id
string
success
network interface id

Sample:
eni-1d889198
 
mac_address
string
success
interface's physical address

Sample:
00:00:5E:00:53:23
 
name
string
success
The name of the ENI

Sample:
my-eni-20
 
owner_id
string
success
aws account id

Sample:
812381371
 
private_ip_address
string
success
primary ip address of this interface

Sample:
10.20.30.40
 
private_ip_addresses
list / elements=dictionary
success
list of all private ip addresses associated to this interface

Sample:
[{'primary_address': True, 'private_ip_address': '10.20.30.40'}]
 
source_dest_check
boolean
success
value of source/dest check flag

Sample:
True
 
status
string
success
network interface status

Sample:
pending
 
subnet_id
string
success
which vpc subnet the interface is bound

Sample:
subnet-b0a0393c
 
tags
dictionary
success
The dictionary of tags associated with the ENI

Sample:
{'Name': 'my-eni', 'group': 'Finance'}
 
vpc_id
string
success
which vpc this network interface is bound

Sample:
vpc-9a9a9da


Authors

  • Rob White (@wimnat)

  • Mike Healey (@healem)