Interview Prep/Ansible/Variables, Facts and Precedence Problems

A `delegate_to: localhost` task fails with `/usr/bin/python3.9: No such file or directory`

AdvancedTroubleshootingVariables, Facts and Precedence Problems
Answer

Likely causes → localhost is defined explicitly in the inventory, so it belongs to all and inherits ansible_python_interpreter: /usr/bin/python3.9 from group_vars/all.yml. That path exists on the targets, but not on the controller or in the execution environment.

Diagnose → ansible-inventory --host localhost --vars | grep python

Fix →

# host_vars/localhost.yml
ansible_connection: local
ansible_python_interpreter: "{{ ansible_playbook_python }}"

Or remove localhost from the inventory and rely on implicit localhost, which already uses ansible_playbook_python.

Prevent → Set the interpreter per OS group (rhel8, ubuntu), not in all.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence Problems