In a “Development or Test” context (e.g. using Vagrant to spin up temporary machines), it is usually not desired to store the host key in ~/.ssh/known_hosts
file, as the related host:port is frequently reused by other machines. In this kind of simple context, we also assume that ssh-keyscan tool is not used.
At the moment, it is possible to configure Ansible to not modify the known host file the following way:
-
paramiko connection: ANSIBLE_PARAMIKO_RECORD_HOST_KEYS=false
-
ssh connection: ANSIBLE_SSH_ARGS=‘-o UserKnownHostsFile=/dev/null’
(Note that there is no ANSIBLE_SSH_RECORD_HOST, equivalent to ANSIBLE_PARAMIKO_RECORD_HOST)
This change request idea comes from the Vagrant context, where by default Ansible is configured so that the known host keys are not considered (via ANSIBLE_HOST_KEY_CHECKING=False). But we’d also want that the known host file is not modified by Ansible.
Of course, we can easily implement this behavior with the parameters listed above, but I think that having an “ssh-connection-agnostic” way to configure Ansible would be more comprehensive and could be useful in any other contexts that uses temporary “unsafe machines” that share the same host:port addresses.
Therefore I see three possible approaches to resolve this issue:
-
Change the behavior of “ANSIBLE_HOST_KEY_CHECKING=False”, so that the known_hosts file is not modified in this case.
-
Introduce a new option (e.g. ANSIBLE_RECORD_HOST_KEYS, set to true by default), impacting both (ssh and parmiko) connection modes (This would deprecate ANSIBLE_PARAMIKO_RECORD_HOST by the way)
-
Introduce a new option (e.g. ANSIBLE_USER_KNOWN_HOSTS_FILE), to optionally specify the host file to use by both (ssh and parmiko) connection modes.
I prefer the first approach (change behavior of “ANSIBLE_HOST_KEY_CHECKING=False”) for the following reasons:
-
A host key that we don’t check - or even trust - should actually not be stored as known host.
-
For openssh connection, it corresponds to usual “-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no” combination.
-
These ‘unnecessary’ entries in the known hosts file will never be automatically replaced when the host key changes, leading to keep outdated information. Having a key mismatch when using ANSIBLE_HOST_KEY_CHECKING=False can lead to confusing situations, like the SSH forwarding problem documented in https://gist.github.com/glenjamin/7f1d4335e7a9760b75e5.
-
The backward compatibility on Ansible safe default (ANSIBLE_HOST_KEY_CHECKING=True) is maintained, and only the “non-standard” mode is impacted.
So, I’d like to know if more people are interested in this change request and if you would you accept a patch for that?
If yes, which approach do you prefer? For the first approach, I already created d90436ea65a82aab1f746c503d8a0b68a5e315a8, as a possible base for a pull request.
Many thanks in advance for your reviews!
Gilles
Related References:
- https://github.com/ansible/ansible/issues/3694#issuecomment-22530734
(where the possible addition of an option to allow setting -o UserKnownHostsFile=/dev/null was discussed) - https://github.com/mitchellh/vagrant/issues/3900
(original motivation for this discussion)