2

Regarding How to set host_key_checking=false in ansible inventory file? I wanted to know if it is possible to change this setting on the playbook definition.

My playbook is called automatically and I cannot use export to set the ENV variable before starting the playbook.

This is what I tried:

---
- hosts: localhost
  environment:
    ANSIBLE_HOST_KEY_CHECKING: false
  roles:
    - including_vars

The problem is that if I add a host during the playbook tasks I get the following error:

UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Host key verification failed.\r\n", "unreachable": true}

This seems to indicate that the environment variable is not used.

Any idea on how I could change this setting on runtime?

techraf
  • 64,883
  • 27
  • 193
  • 198
djuarezg
  • 2,307
  • 2
  • 20
  • 34

2 Answers2

5

You can set ansible_host_key_checking / ansible_ssh_host_key_checking in the vars section of the play:

---
- hosts: localhost
  vars:
    ansible_host_key_checking: false
  roles:
    - including_vars

Important: when testing with pipelining enabled, give SSH time to expire the persistent connection, otherwise changed settings might not yet be applied.


You could also create an in-memory inventory with host_key_checking and run your real plays against that inventory (this would also work with Ansible versions older than 2.5):

----
- hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - add_host:
        name: my_host
        ansible_host: myhost.example.com
        host_key_checking: false

- hosts: my_host
  tasks:
    - ping:
techraf
  • 64,883
  • 27
  • 193
  • 198
0

Shouldn't you have used env: instead of environment:? e.g.,

---
- hosts: localhost
  env:
    ANSIBLE_HOST_KEY_CHECKING: false
  roles:
    - including_vars
robocoder
  • 233
  • 4
  • 9