Skip to content

Instantly share code, notes, and snippets.

@jonahlyn
Last active November 22, 2021 20:54
Show Gist options
  • Save jonahlyn/849bc78a785a23214096c307119aaf7e to your computer and use it in GitHub Desktop.
Save jonahlyn/849bc78a785a23214096c307119aaf7e to your computer and use it in GitHub Desktop.
Run an ansible playbook on the localhost (without warnings)

inventory

[localhost]
127.0.0.1  ansible_connection=local

ansible.cfg

[defaults]
inventory = inventory

playbook.yml

---

- hosts: localhost
  tasks:
    - name: .....

Reference: ansible/ansible#33132 (comment)

@AlanCoding
Copy link

I'm not sure if this is recommendable. For me, it leads to a new warning with:

[DEPRECATION WARNING]: Distribution fedora 35 on host localhost should use /usr/bin/python3, but is using /usr/bin/python for backward
compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version
2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

I believe the reasons is that the "implicit" localhost sets both ansible_connection and ansible_python_interpreter. Looking forward, it seems dangerous to try and override the implicit localhost because who knows what you might miss? The implicit localhost has the canonical behavior (which is very much preferred, see the python interpreter issue).

As crazy as it sounds, the best way to run without errors against localhost would seem to modify your inventory file to be

foobar.invalid

That is, make an inventory with an arbitrary invalid host. The playbook will then only target the true implicit localhost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment