Bypassing host key checking in ansible
To bypass host key authenticity in ansible; you can follow any of the method:
(1) Using ansible.cfg file:
In the playbook/project directory, create a file ansible.cfg and add the following line:
[defaults]
host_key_checking = False
Note, that this method will work globally and all the hosts that are present in inventory/hosts file will suffer the effect unless mentioned in the hosts file.
(2) Exporting environment variable from your machine : (Temporary method)
export ANSIBLE_HOST_KEY_CHECKING=False
This is the temporary method and will only work as long the session is connected (SSH-session). Once, you break the session, the environment variable will be removed.
(3) Exporting environment variable from your machine: (Permanent method)
To set the variable “ANSIBLE_HOST_KEY_CHECKING=False ” permanently, edit the ~/.bashrc file of the user running the ansible commands. Add the below line at the end of the file and save and exit.
export ANSIBLE_HOST_KEY_CHECKING=False
Then, source the file so that changes take effect for the current ssh session accordingly by using the command below:
source ~/.bashrc
(3) Using ansible ssh commom arguments Variable:
(a) You can specify SSH options directly in your inventory file for specific hosts or groups:
[myhosts]
host1 ansible_ssh_common_args='-o StrictHostKeyChecking=no'
host2 ansible_ssh_common_args='-o StrictHostKeyChecking=no'
(b) You can also define the the above arguments in playbooks:
For example:
-name: Playbook for copying files from local machine to remote server
hosts: all
vars:
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
roles:
- copy_files
(4) By simply using cli interface and running the ansible command:
For example:
ansible-playbook -i '52.8.13.17,' -e 'host=52.8.13.17' test-copy-files.yml --private-key /home/ansible-user/.ssh/ansible-user.key --ssh-extra-args='-p 2222' -e "ansible_ssh_common_args='-o StrictHostKeyChecking=no'" --check