Can I bypass host key checking in Ansible?

Ansible is an IaCInfrastructure as Code open-source software suite written in Python. It handles problems relating to software provisioning, updating, configuration management, and application functionality. Moreover, it creates an automated IT experience that streamlines work between a central server and multiple remote servers. Ansible uses files to store our automation code for all these tasks. These files are called an Ansible Playbook.

Furthermore, Ansible is an agentless push model software. This means that our host machines do not have to install any software to work, and Ansible pushes its commands directly to its hosts from the central server via SSH. This creates an easy-to-use setup, making it a prevalent software.

Note: To learn how to setup Ansible, check this Answer.

Definition

We need to understand the requirement before we try to solve our problem. As discussed before, Ansible establishes its connection with its remote IPs by an SSHSecure Shell or Secure Socket Shell connection. A host key refers to the SSH key pair used for authenticating and establishing the relationship between the central server and its remote servers. This works by verifying the established connection, i.e., the host's identity. This protects us against potential security threats by checking if the host's identity has changed since the last link.

Methods

Now we can answer the question we raised at the start. Yes, we can bypass host key checking. We can try the following methods to bypass the host key.

Configuration settings

Firstly, we need our Ansible configuration file. To do so, we will follow the following points.

  1. Open the "ansible.cfg" file in the "/etc/ansible" directory.

  2. Now inside the file, we need to set host_key_checking to False. This ensures that the SSH connection does not request a host key check, or we can use the following command to set the environment variable to False.

export ANSIBLE_HOST_KEY_CHECKING=False
Environment variable
  1. Lastly, we must run the following commands to disable global and local host key checking.

ansible_ssh_extra_args='-o StrictHostKeyChecking=no'
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
Terminal commands
  1. Now we can run our Ansible commands without the requirement of host key checking.

Playbook commands

If we are using the playbooks feature in Ansible to run our commands, we can use the following methods to bypass the host key.

  1. First, we must create a playbook in our "playbooks" folder in the Ansible directory.

--| etc/ansible
--| ansible.cfg // configuration file
--| hosts // stores hosts
--| roles // stores processes
--| playbooks // stores scripts
Ansible directory hierarchy
  1. We must save our YML playbook file and write our code as needed. An example code has been given below. The vars task in our YML file defines the bypass arguments by setting the host key checking value to no.

---
- name: filename
hosts: EducativeGroup
vars:
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
tasks:
# add the rest of your code here
  1. Now we can run our Ansible commands without the requirement of host key checking.

Conclusion

Ansible provides us with the freedom to modify the processes to our requirements. However, some security and performance measures should not be changed. We can bypass the host key checking in our scenario by using a few commands. Still, it is not recommended as it opens our central server and our remote servers to outside threats, which can compromise the security of our network.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved