Use Ansible ec2_remote_facts to add_host, and connect to newly added host

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Use Ansible ec2_remote_facts to add_host, and connect to newly added host



I have a newly created ec2 instance. I want to query AWS using Ansible's ec2_remote_facts to find that host's ip address, add it as a host, and connect to it in the next step of the playbook.


ec2_remote_facts



Here's what I have so far -


- hosts: localhost
tasks:

- ec2_remote_facts:
region: us-east-1
filters:
image_id: ami-8b070ff4
register: ec2

- add_host:
hostname: new_instance
name: "{{ ec2.instances[0].private_ip_address }}"
ansible_ssh_user: ubuntu
ansible_ssh_private_key_file: ~/.ssh/my_key

- hosts: new_instance
tasks:

- shell: 'echo $HOSTNAME'
register: results

- debug: var=results.stdout



But I'm getting this error -


"msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname new_instance: nodename nor servname provided, or not knownrn",



I'm trying to have results be the ip of my remote host.


results





What is the question? For example: "how to run an Ansible controller so that it has access to an EC2 instance's private address"?
– techraf
1 hour ago







You mention module ec2_remote_facts in the beginning of the first paragraph but not use it in the playbook. Question also to be added. Thanks
– baptistemm
1 hour ago


ec2_remote_facts




1 Answer
1



You can use the facts to the get the IP but, I think you are taking the approach in the wrong way. After the VM is created you can grab this information that the module spits out similar to what you have. Specially if your trying to create the VM, grab IP and do more work with it. Here is a blog showing an example on how to do it. While this is for GCE, is still the same approach. https://www.redhat.com/en/blog/creating-load-balanced-web-service-cloud-ansible






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name