Issue
I have looked at this post, but it doesn't really help me.
Symptom
Using EC2 Dynamic Inventory, I am unable to configure the ansible_host:
var to the private-ip-address:
of a specific interface when there are two interfaces available with private-ip-address:
on different subnets.
Reason for question
The two interfaces in use on the hosts are management
and server-lan
. The Ansible Control node can only access hosts via the management
interface.
Note: I've simplified the code for reading and changed the host and IP details.
I have the following very simple example ec2_aws inventory file.
plugin: aws_ec2
regions:
- us-west-x
filters:
tag:Name: ec2-host-01
instance-state-name: running
keyed_groups:
# Add hosts to tag_Name_Value groups for each Name/Value tag pair
- prefix: tag
key: tags
hostnames:
- tag:Name
compose:
ansible_host: private-ip-address
My sample host has 2 Network interfaces management
and server-lan
, as identified in the description:
.
{
"_meta": {
"hostvars": {
"ec2-host-01": {
.....
"network_interfaces": [
{...
"description": "management",
"private_ip_addresses": [
{
"primary": true,
"private_dns_name": "ip-10-0-1-1.eu-west-x.compute.internal",
"private_ip_address": "10.0.1.1"
}
],
},
{...
"description": "server-lan",
"private_ip_addresses": [
{
"primary": true,
"private_dns_name": "ip-10-0-20-1.eu-west-x.compute.internal",
"private_ip_address": "10.0.20.1"
}
],
}
...
}
I'm not sure if this makes a difference, but both interfaces have primary: true
.
This is the output I currently get from the ansible-inventory
command. The ec2 plugin appears to randomly assign the ansible_host
. Where as I need to set the ansible_host
var to the IP address of the management interface. Currently, I am unable to specify the private-ip-address of the management interface.
{
"_meta": {
"hostvars": {
"ec2-host-01": {
"ami_launch_index": 0,
"ansible_host": "10.0.20.1", ## NB: This should be 10.0.1.1
"architecture": "x86_64",
"block_device_mappings": [
....
Here is the output of the ansible-inventory --graph --vars
command:
| |--ec2-host-01
| | |--{ansible_host = 10.0.20.1}
...............
| | |--{network_interfaces = [{'attachment': {'attach_time': datetime.datetime(2022, 7, 13, 13, 45, 38, tzinfo=tzlocal()), 'attachment_id': 'eni-attach-xxxxxxxxxxxxxxxxx', 'delete_on_termination': False, 'device_index': 1, 'status': 'attached', 'network_card_index': 0}, 'description': 'server-lan', 'groups': [{'group_name': 'server_group', 'group_id': 'xxxxxxxxxxxxxxxxxxx'}], 'ipv6_addresses': [], 'mac_address': 'xx:xx:xx:xx:xx:xx', 'network_interface_id': 'eni-xxxxxxxxxxxxxxxxx', 'owner_id': '123456789123', 'private_dns_name': 'ip-10-0-20-1.eu-west-x.compute.internal', 'private_ip_address': '10.0.20.1', 'private_ip_addresses': [{'primary': True, 'private_dns_name': 'ip-10-0-20-1.eu-west-x.compute.internal', 'private_ip_address': '10.0.20.1'}], 'source_dest_check': True, 'status': 'in-use', 'subnet_id': 'subnet-management-xxxxxx', 'vpc_id': 'vpc-abc123xxxxxxxxxx', 'interface_type': 'interface'}, {'attachment': {'attach_time': datetime.datetime(2022, 7, 13, 13, 45, 38, tzinfo=tzlocal()), 'attachment_id': 'eni-attach-xxxxxxxxxxxxxxxxx', 'delete_on_termination': False, 'device_index': 0, 'status': 'attached', 'network_card_index': 0}, 'description': 'management', 'groups': [{'group_name': 'server_group', 'group_id': 'xxxxxxxxxxxxxxxxxxx'}], 'ipv6_addresses': [], 'mac_address': 'xx:xx:xx:xx:xx:xx', 'network_interface_id': 'eni-xxxxxxxxxxxxxxxxx', 'owner_id': '123456789123', 'private_dns_name': 'ip-10-0-1-1.eu-west-x.compute.internal', 'private_ip_address': '10.0.1.1', 'private_ip_addresses': [{'primary': True, 'private_dns_name': 'ip-10-0-1-1.eu-west-x.compute.internal', 'private_ip_address': '10.0.1.1'}], 'source_dest_check': True, 'status': 'in-use', 'subnet_id': 'subnet-lan-xxxxxxxxxxxxxx', 'vpc_id': 'vpc-abc123xxxxxxxxxx', 'interface_type': 'interface'}]}
| | |--{private_dns_name = ip-10-0-20-1.eu-west-x.compute.internal}
| | |--{private_dns_name_options = {'hostname_type': 'ip-name', 'enable_resource_name_dns_a_record': False, 'enable_resource_name_dns_aaaa_record': False}}
| | |--{private_ip_address = 10.0.20.1}
...............
| | |--{source_dest_check = True}
| | |--{state = {'code': 16, 'name': 'running'}}
| | |--{state_transition_reason = }
...............
| | |--{usage_operation = RunInstances}
| | |--{usage_operation_update_time = 2022-07-13 13:45:38+00:00}
Required Result
I need to be able to configure the aws_ec2.yml
configuration file (possibly via the compose:
key) to set the ansible_host:
var as the private-ip-address
of the management
interface.
compose:
# I need to set this var to: 10.0.1.1 eg: the management interface IP.
ansible_host: private-ip-address ???????
β.εηοιτ.βε has already provided a solution to be able to retrieve the management
and server-lan
ip addresses during a playbook run (here), but this is different, as this is defining the inventory vars.
Solution
plugin: aws_ec2
cache: yes
regions:
- eu-central-1
hostnames:
- tag:Name
filters:
instance-state-name:
- running
compose:
ansible_host: network_interfaces | selectattr('description', 'defined') | selectattr('description', '==', 'management') | map(attribute='private_ip_addresses') | map(attribute='private_ip_address')
Answered By - Markus Answer Checked By - Mildred Charles (WPSolving Admin)