I’m running an ansible playbook which calls an initial task that runs on localhost to add known_hosts ssh keys for all hosts returned by the inventory. This playbook runs fine if I use an explicit inventory list.
However, if I run the same playbook using aws_ec2.yml dynamic inventory and limit it to a single host or group it does not resolve localhost.
How can I configure an explicit localhost for ansible to resolve?
Here is what I run…
ansible-playbook build.yml -i ../inventories/DYNAMIC/aws_ec2.yml --limit HOST104
PLAY [Write key to known hosts] **********************************************************************************************************************************************************
skipping: no hosts matched
-----------------------------------------------------------------------------------------------------
Here is my playbook.
- name: Wait for connection to host
hosts: localhost
connection: local
gather_facts: no
become: true
tasks:
- name: Write Keys to known_hosts
include_tasks: ../tasks/add-known-hosts.yml
I tried to call the aws_ec2.yml dynamically and specifically add localhost to the limit which does resolve the localhost. However, then it tries to write the ssh keys to known_hosts for ALL hosts instead of limiting to just a single host or group.
ansible-playbook build.yml -i ../inventories/DYNAMIC/aws_ec2.yml --limit HOST104:localhost
TASK [Write key to known hosts] *************************************************************************************************************************************************************
changed: [localhost -> 127.0.0.1] => (item=HOST100)
changed: [localhost -> 127.0.0.1] => (item=HOST101)
changed: [localhost -> 127.0.0.1] => (item=HOST102)
changed: [localhost -> 127.0.0.1] => (item=HOST103)
changed: [localhost -> 127.0.0.1] => (item=HOST104)
Here is my task to write keys to known_hosts...
- name: Write key to known hosts
shell: "ssh-keyscan {{ hostvars[item].ansible_host }} >> ~/.ssh/known_hosts"
when: hostvars[item].ansible_host is defined
with_items: "{{ groups['all'] }}"
user24647888 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.