Closed link89 closed 2 years ago
Files identified in the description:
plugins/inventory/aws_ec2.py
](https://github.com/['ansible-collections/amazon.aws', 'ansible-collections/community.aws', 'ansible-collections/community.vmware']/blob/main/plugins/inventory/aws_ec2.py)If these files are inaccurate, please update the component name
section of the description or use the !component
bot command.
@link89 Thanks for reporting this issue. I tried to reproduce using this playbook following all steps provided above, but could not
- hosts: localhost
gather_facts: no
vars:
ec2_ami_name: 'Fedora-Cloud-Base-*.x86_64*'
module_defaults:
group/aws:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token | default(omit) }}"
region: "{{ aws_region }}"
collections:
- community.aws
- amazon.aws
tasks:
- debug:
msg: "ec2 hosts: {{ groups.aws_ec2 }}"
- name: get image ID to create an instance
ec2_ami_info:
filters:
architecture: x86_64
owner-id: '{{ aws_owner_id }}'
virtualization-type: hvm
root-device-type: ebs
name: 'Fedora-Atomic-27*'
register: _images
no_log: true
- set_fact:
image_id: '{{ _images.images.0.image_id }}'
vpc_cidr: '10.{{ 256 | random(seed=resource_prefix) }}.0.0/16'
subnet_cidr: '10.{{ 256 | random(seed=resource_prefix) }}.0.0/24'
- name: create a VPC to work in
ec2_vpc_net:
cidr_block: '{{ vpc_cidr }}'
name: '{{ resource_prefix }}-vpc'
resource_tags:
Name: '{{ resource_prefix }}'
register: _vpc
- set_fact:
vpc_id: '{{ _vpc.vpc.id }}'
- name: create a subnet to use for creating an ec2 instance
ec2_vpc_subnet:
vpc_id: '{{ vpc_id }}'
cidr: '{{ subnet_cidr }}'
resource_tags:
Name: '{{ resource_prefix }}'
register: _subnet
- set_fact:
subnet_id: '{{ _subnet.subnet.id }}'
- name: create a security group to use for creating an ec2 instance
ec2_group:
name: '{{ resource_prefix }}-sg'
vpc_id: '{{ vpc_id }}'
description: "create security group for testing"
register: _secgroup
- name: create a new host
ec2:
image: '{{ image_id }}'
count_tag:
Name: '{{ resource_prefix }}'
instance_tags:
Name: '{{ resource_prefix }}'
instance_type: t2.micro
wait: yes
group_id: '{{ _secgroup.group_id }}'
vpc_subnet_id: '{{ subnet_id }}'
- meta: refresh_inventory
- debug:
msg: "ec2 hosts: {{ groups.aws_ec2 }}"
- name: Create custom DHCP options
ec2_vpc_dhcp_option:
region: "{{ aws_region }}"
dns_servers:
- 8.8.8.8
tags:
Name: custom servers
Environment: Test
register: _dhcpoptions
- name: update VPC with DHCP options
ec2_vpc_net:
cidr_block: '{{ vpc_cidr }}'
name: '{{ resource_prefix }}-vpc'
resource_tags:
Name: '{{ resource_prefix }}'
dhcp_opts_id: "{{ _dhcpoptions.dhcp_options_id }}"
register: _vpc
- meta: refresh_inventory
- debug:
msg: "ec2 hosts: {{ groups.aws_ec2 }}"
Here after the output
PLAY [localhost] ************************************************************************************************************************************************************
TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "ec2 hosts: []"
}
TASK [get image ID to create an instance] ***********************************************************************************************************************************
ok: [localhost]
TASK [set_fact] *************************************************************************************************************************************************************
ok: [localhost]
TASK [create a VPC to work in] **********************************************************************************************************************************************
changed: [localhost]
TASK [set_fact] *************************************************************************************************************************************************************
ok: [localhost]
TASK [create a subnet to use for creating an ec2 instance] ******************************************************************************************************************
changed: [localhost]
TASK [set_fact] *************************************************************************************************************************************************************
ok: [localhost]
TASK [create a security group to use for creating an ec2 instance] **********************************************************************************************************
changed: [localhost]
TASK [create a new host] ****************************************************************************************************************************************************
changed: [localhost]
TASK [meta] *****************************************************************************************************************************************************************
TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "ec2 hosts: ['ip-10-3-0-182.ec2.internal']"
}
TASK [Create custom DHCP options] *******************************************************************************************************************************************
ok: [localhost]
TASK [update VPC with DHCP options] *****************************************************************************************************************************************
changed: [localhost]
TASK [meta] *****************************************************************************************************************************************************************
TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "ec2 hosts: ['ip-10-3-0-182.ec2.internal']"
}
Could you please provide a playbook to reproduce this issue ?
@link89 Could you please let us know if the issues still persist and in case have a look at what Aubin suggested?
@link89 This issue is waiting for your response. Please respond or the issue will be closed.
Hi @abikouo @alinabuzachis We figure out a fix, I think we are missing hostnames
sections in our setup.
hostnames:
- private-ip-address
I don't know why it works if we don't change the dhcp setting thought. But it works fine now.
Summary
When I use the default dhpc that assign to my VPC, and use aws_ec2 inventory, everything works fine. But after I create a customize dhcp options that use dns servers (like 8.8.8.8)and set my VPC to use it, the output of the same inventory script will become empty.
Issue Type
Bug Report
Component Name
aws_ec2 inventory
Ansible Version
Collection Versions
AWS SDK versions
Configuration
OS / Environment
MacOS, CentOS
Steps to Reproduce
And run with
ansible-inventory -vvvv --graph -i ansible/aws_ec2.yml
Expected Results
List all instances.
Actual Results
Code of Conduct