Adrimus
Adrimus

Reputation: 15

Ansible dynamic inventory could not resolve hostname

In ansible (please see my Repo I have a dynamic inventory (hosts_aws_ec2.yml). It shows this

ansible-inventory -i hosts_aws_ec2.yml --graph
@all:
  |--@aws_ec2:
  |  |--linuxweb01
  |  |--winweb01
  |--@iis:
  |  |--winweb01
  |--@linux:
  |  |--linuxweb01
  |--@nginx:
  |  |--linuxweb01
  |--@ungrouped:
  |--@webserver:
  |  |--linuxweb01
  |  |--winweb01
  |--@windows:
  |  |--winweb01

When I run any playbook, for example configure_iis_web_server.yml or ping_novars.yml in my repo It says host is unreachable.

ansible-playbook ping_novars.yml -i hosts_aws_ec2.yml  --ask-vault-pas --limit linuxweb01

PLAY [linux] ******************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************
fatal: [linuxweb01]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname linuxweb01: Name or service not known", "unreachable": true}

PLAY RECAP ********************************************************************************************************************
linuxweb01                 : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0

ansible all -i hosts_aws_ec2.yml -m debug -a "var=ip" --ask-vault-pass shows that it finds the ip addresses for the files in host_vars folder.

winweb01 | SUCCESS => {
    "ip": "3.92.5.126"
}
linuxweb01 | SUCCESS => {
    "ip": "52.55.134.86"
}

I used to have this working when I didn't have this in hosts_aws_ec2.yml:

hostnames:
  - tag:Name

and the files in host_vars where the actual public IPv4 DNS addresses for example ec2-3-92-5-126.compute-1.amazonaws.com.yml instead of winweb01.Then the inventory would list the public dns not the name.

Is there anyway to use the name tag in the inventory but provide the ip address?

Upvotes: 0

Views: 943

Answers (1)

Adrimus
Adrimus

Reputation: 15

I was able to make it work by adding compose to my dynamic host script:

  hostnames:
    - tag:Name
  compose:
    ansible_host: public_dns_name

found answer here: Displaying a custom name for a host

Upvotes: 0

Related Questions