# Turn off host\_key\_checking when using Python API

**URL:** <https://forum.ansible.com/t/turn-off-host-key-checking-when-using-python-api/16852>\
**Category:** Ansible Project\
**Created:** [November 22, 2014, 6:19pm UTC](https://forum.ansible.com/t/turn-off-host-key-checking-when-using-python-api/16852 "2014-11-22T18:19:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [November 22, 2014, 6:19pm UTC](https://forum.ansible.com/t/turn-off-host-key-checking-when-using-python-api/16852/1 "2014-11-22T18:19:05Z")

</div>

We have a Python program that calls the ansible Python API – e.g.:

```python

pb = ansible.playbook.PlayBook(
playbook=playbook,
host_list=host_list,
stats=stats,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
extra_vars=extra_vars,
private_key_file=private_key_file,
subset=host
)

pb.run()

```

How can I specify that I want host\_key\_checking off?

There are a few things that work, but I’m trying to avoid for various reasons:

1. Putting `host_key_checking = False` in ansible.cfg – trying to avoid this because the user could run our tool from any directory so we don’t know that ansible.cfg will be in their current directory and we don’t want to force them to have a ~/.ansible.cfg or /etc/ansible/ansible.cfg

2. Setting environment variable `ANSIBLE_HOST_KEY_CHECKING`. The following works, but it’s a little gross because it’s messing with global state and it has to be done before importing ansible for it to take effect:

```auto

os.environ['ANSIBLE_HOST_KEY_CHECKING'] = 'False'

import ansible.runner
import ansible.playbook
from ansible import callbacks

```

Is there some other way to just pass in a parameter or something to turn off host key checking? It doesn’t look like it from a quick scan of the code. Wonder if a PR that added an optional parameter might be something that would be considered?

Related:

[https://groups.google.com/forum/#!msg/ansible-project/OuHJwG0LLTY/qp95qAq-PNUJ](https://groups.google.com/forum/#!msg/ansible-project/OuHJwG0LLTY/qp95qAq-PNUJ)

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [November 23, 2014, 3:29am UTC](https://forum.ansible.com/t/turn-off-host-key-checking-when-using-python-api/16852/2 "2014-11-23T03:29:12Z")

</div>

Here’s another hacky way to do it:

```auto

pb = ansible.playbook.PlayBook(
playbook=playbook,
host_list=host_list,
stats=stats,
callbacks=playbook_cb,
runner_callbacks=runner_cb,
extra_vars=extra_vars,
private_key_file=private_key_file,
subset=host
)

ansible.constants.HOST_KEY_CHECKING = False

pb.run() # This runs the playbook

```

but it would be cool if I could pass it to the PlayBook constructor.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 24, 2014, 10:47pm UTC](https://forum.ansible.com/t/turn-off-host-key-checking-when-using-python-api/16852/3 "2014-11-24T22:47:38Z")

</div>

Some of the v2/ refactoring will enable some new API options that may allow for making this easier - or it may be something we can entertain after those refactorings are more complete.

At this time though, it’s not a great point to get involved, as it’s still too emergent.

We will let you know when this time is.

---

<div class="post-metadata">

**Author:** ![Marc\_Abramowitz](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marc\_Abramowitz](https://forum.ansible.com/u/Marc_Abramowitz)\
**Post date:** [November 25, 2014, 12:44am UTC](https://forum.ansible.com/t/turn-off-host-key-checking-when-using-python-api/16852/4 "2014-11-25T00:44:36Z")

</div>

Cool, thanks Michael!
