ansible ssh prompt known_hosts issue

Question:

I’m running Ansible playbook and it works fine on one machine.

On a new machine when I try for the first time, I get the following error.

This error can be resolved, if I first go to the source machine (from where I’m running the ansible playbook) and manually ssh to the target machine (as the given user) and enter “yes” for known_hosts file entry.

Now, if I run the same ansible playbook second time, it works without an error.

Therefore, how can I suppress the prompt what SSH gives while making ssh known_hosts entry for the first time for a given user (~/.ssh folder, file known_hosts)?

I found I can do this if I use the following config entries in ~/.ssh/config file.

~/.ssh/config

i.e. if I place the above code in the user’s ~/.ssh/config file of a remote machine and try Ansible playbook for the first time, I won’t be prompted for entring “yes” and playbook will run successfully (without requiring the user to manually create a known_hosts file entry from the source machine to the target/remote machine).

My questions:
1. What security issues I should take care if I go ~/.ssh/config way
2. How can I pass the settings (what’s there in the config file) as parameters/options to ansible at command line so that it will run first time on a new machine (without prompting / depending upon the known_hosts file entry on the source machine for the target machine?

Answer:

The ansible docs have a section on this. Quoting:

Ansible has host key checking enabled by default.

If a host is reinstalled and has a different key in ‘known_hosts’,
this will result in an error message until corrected. If a host is not
initially in ‘known_hosts’ this will result in prompting for
confirmation of the key, which results in an interactive experience if
using Ansible, from say, cron. You might not want this.

If you understand the implications and wish to disable this behavior,
you can do so by editing /etc/ansible/ansible.cfg or ~/.ansible.cfg:

Alternatively this can be set by the ANSIBLE_HOST_KEY_CHECKING
environment variable:

Also note that host key checking in paramiko mode is reasonably slow, therefore switching to ‘ssh’ is also recommended when using this feature.

Leave a Reply