Configuring Host Key Checking
If you prefer to keep host key checking enabled, you can configure it to suit your needs. Here are a few options:
Manually Accepting Host Keys
When you connect to a remote host for the first time, Ansible will prompt you to accept the host's public key. You can manually accept the key by typing "yes" when prompted:
The authenticity of host 'example.com (192.168.1.100)' can't be established.
ECDSA key fingerprint is SHA256:abcd1234efgh5678.
Are you sure you want to continue connecting (yes/no)? yes
This will add the host's public key to the known_hosts file, and Ansible will use it to verify the host's identity on subsequent connections.
Automatically Accepting Host Keys
Alternatively, you can configure Ansible to automatically accept host keys by setting the host_key_auto_add
option in the Ansible configuration file:
[defaults]
host_key_auto_add = True
This will automatically add new host keys to the known_hosts file without prompting you.
Specifying a Custom Known Hosts File
By default, Ansible uses the ~/.ssh/known_hosts
file to store host keys. However, you can specify a custom known_hosts file by setting the ANSIBLE_SSH_ARGS
environment variable:
export ANSIBLE_SSH_ARGS="-o UserKnownHostsFile=/path/to/custom/known_hosts"
ansible-playbook my_playbook.yml
This can be useful if you want to maintain separate known_hosts files for different environments or projects.
graph LR
A[Ansible] -- Prompt for Manual Acceptance --> B[known_hosts file]
A -- Automatically Accept --> C[known_hosts file]
A -- Specify Custom File --> D[/path/to/custom/known_hosts]
By configuring host key checking, you can strike a balance between security and convenience, ensuring that your Ansible connections are secure while still allowing for efficient workflow.