Troubleshooting

Which Python Is Ansible Using? Controller, Target and Delegate Interpreter Diagnostics

Most "Ansible can't find Python" reports are really different interpreters being confused for one another: the controller interpreter running ansible-core, the interpreter discovered on the managed node, the interpreter a delegated task uses, and the defaults that apply to an inventory localhost versus the implicit one Ansible invents for you. This guide separates them, reproduces the controller, target and delegate distinctions with a local play that asserts each one it can establish on a single machine, and lists the diagnostic commands that tell you which is which.

Version scope and prerequisites

Written against ansible-core 2.22.0, released 2026-11-02. The Python ranges below come from the upstream support matrix and are specific to these released versions:

  • Controller Python for 2.22: 3.13 to 3.15. Support for 3.12 on the controller was dropped in this release.
  • Target Python for 2.22: 3.9 to 3.15. 2.22 is one of the releases that carries seven target versions rather than six.
  • Controller Python for 2.21: 3.12 to 3.14, with targets 3.9 to 3.14.

The controller range is narrow by design — upstream ships control-node support for the three most recently released Python versions — while the target range is deliberately wide so one controller can manage older estates. Later entries in the upstream table are planned schedules for releases that have not shipped; read the live matrix rather than planning against a row that may still move.

The reproduction below uses two inventory hosts, both over the local connection, and changes nothing on either. Run it in your own virtual environment, not in the playground on this site, which pins an older ansible-core.

Common setup

mkdir -p ~/tma-interpreter && cd ~/tma-interpreter
python3.13 -m venv .venv
.venv/bin/python -m pip install 'ansible-core==2.22.0'
.venv/bin/ansible --version
[defaults]
interpreter_python = auto_silent
[local]
localhost ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"

[probe]
probe-host ansible_connection=local

That inventory line matters more than it looks. ansible_playbook_python is a magic variable holding the absolute path of the interpreter that is running ansible-core, and pinning ansible_python_interpreter to it makes modules execute in the same virtual environment as the controller. You have to write it out because declaring localhost in inventory replaces the implicit localhost and its built-in defaults — including that very pin. An explicit localhost ansible_connection=local with no interpreter variable falls back to ordinary discovery, which is how a venv-installed collection dependency ends up "missing" on a localhost play. Pin it for localhost work; do not copy the same pin onto a remote host, where the controller's interpreter path means nothing.

Four interpreters, and which defaults apply

1. The controller interpreter

This is the Python that imports ansible-core. It is reported by ansible --version on the python version line and is available in plays as ansible_playbook_python. Its version must sit inside the controller range for your release — 3.13 to 3.15 for 2.22. Nothing about a managed node can change it, and no inventory variable affects it.

2. The target interpreter

This is the Python that actually executes module code on the managed node. Unless you configure otherwise, Ansible discovers it on the first Python module run for that host. The controller never needs to share a version with it: a 3.13 controller managing a RHEL 8 host with Python 3.9 is a supported combination for 2.22.

Discovery is controlled in two places:

  • Per host or group, with the ansible_python_interpreter inventory variable.
  • Globally, with interpreter_python in the [defaults] section of ansible.cfg, or the ANSIBLE_PYTHON_INTERPRETER environment variable.

The value is either an absolute path or one of two discovery modes. auto is the default: it walks an ordered list of well-known interpreter paths and warns that a Python installed later could change the choice. auto_silent does the same without the warning. On 2.22 the fallback order starts at python3.15 and descends through python3.9 before trying /usr/bin/python3 and python3.

auto_legacy was removed in ansible-core 2.21. It is no longer recognised as a discovery mode, so a leftover auto_legacy or auto_legacy_silent is now treated as a literal interpreter path. The failure looks like a missing binary — The module interpreter 'auto_legacy' was not found with /bin/sh: 1: auto_legacy: not found in module_stderr — which sends people hunting for a Python problem that does not exist. Use auto or auto_silent.

3. The delegated interpreter

A task with delegate_to runs on the delegate. Upstream is precise about what that means for interpreter selection: under delegation the execution interpreter, the connection, become and shell plugin options are templated using values from the delegated-to host, and every variable except inventory_hostname is consumed from that host rather than from the original task host. Even inventory_hostname_short refers to the delegate. If you need a value from the original host for one of those options, you must reach for it explicitly as {{ hostvars[inventory_hostname]['varname'] }}.

So "keeps the original host's variables" is the wrong mental model for anything connection-shaped. What does stay with the original host is ownership of the result: facts gathered by a delegated task are assigned to inventory_hostname unless you set delegate_facts: true. Two more corrections worth holding on to, because both are commonly asserted and both are wrong: run_once: true does not create an implicit localhost — it just runs the task once, on one of the play's own hosts — and connection: local does not create one either. Setting connection: local only changes the connection used for inventory_hostname; the host is still your inventory host, with your inventory's variables.

4. The implicit localhost, and why an explicit one behaves differently

If your inventory does not mention localhost and something needs it — a delegate_to: localhost, a local_action, or a reference to 127.0.0.1 or ::1 — Ansible synthesises a host for you. That synthesised host is defined with two specific connection variables, equivalent to ansible_connection: local and ansible_python_interpreter: "{{ ansible_playbook_python }}". In other words the implicit localhost already runs modules under the controller's own interpreter, which is usually what you want and is why delegated tasks so often "just work" in a virtual environment.

It is not variable-free either. Upstream documents that the implicit host is not targetable by any group, but that it does use variables from host_vars and from the all group. It does not appear in hostvars unless you ask for it by name, and inventory_file and inventory_dir are unavailable for it. There is only ever one implicit localhost, under whichever name first created it.

The direction of surprise therefore runs the other way from what people expect. Declaring a localhost entry in inventory replaces the implicit version entirely: all implicit behaviour is ignored, and the host is treated like any other, so group and host variables — including connection variables and ansible_python_interpreter — apply instead. That also changes delegate_to: localhost and local_action. The upstream delegation guide spells out the consequence for a plain local play: with the connection set to local and no ansible_python_interpreter, modules do not run under ansible_playbook_python, so set the variable yourself in host_vars/localhost.yml or on the inventory line. That is exactly the pin in the setup above.

A play that separates controller, target and delegate

This reproduction asserts three things it can actually establish on one machine: that the controller and target interpreters are distinct concepts even when they resolve to the same path, that the target version sits inside the supported range, and that a delegated task resolves its interpreter from the delegated-to host. It does not demonstrate remote discovery — you need a second machine for that — and it does not exercise the implicit localhost, because the inventory declares localhost explicitly.

The second play targets probe-host rather than localhost so that delegation is not degenerate — a play that delegates a localhost task to localhost demonstrates nothing. That is the reason the inventory above carries two entries.

---
- name: Separate the controller interpreter from the target interpreter
  hosts: local
  gather_facts: true
  gather_subset:
    - "!all"
    - "!min"
    - python

  tasks:
    - name: Show the controller and target interpreters side by side
      ansible.builtin.debug:
        msg:
          core_version: "{{ ansible_version.full }}"
          controller_interpreter: "{{ ansible_playbook_python }}"
          target_interpreter: "{{ ansible_facts.python.executable }}"
          target_python: "{{ ansible_facts.python.version.major }}.{{ ansible_facts.python.version.minor }}"

    - name: An explicit localhost pinned to the controller interpreter agrees with it
      ansible.builtin.assert:
        that:
          - ansible_facts.python.executable == ansible_playbook_python
        success_msg: >-
          This host is in inventory, so no implicit default applies; the match is
          the result of the ansible_python_interpreter pin on the inventory line.

    - name: The target Python is inside the supported target range for this release
      ansible.builtin.assert:
        that:
          - ansible_facts.python.version.major == 3
          - ansible_facts.python.version.minor >= 9
          - ansible_facts.python.version.minor <= 15

    - name: The controller requirement is a separate check
      ansible.builtin.assert:
        that:
          - ansible_version.full is version('2.22.0', '>=')
          - ansible_version.major == 2
        success_msg: ansible-core 2.22 requires controller Python 3.13 or later.

- name: A delegated task uses the delegate's own interpreter
  hosts: probe
  gather_facts: false

  tasks:
    - name: Gather Python facts on the delegate and keep them with the delegate
      ansible.builtin.setup:
        gather_subset:
          - "!all"
          - "!min"
          - python
      delegate_to: localhost
      delegate_facts: true

    - name: Report the delegate's module interpreter, not the play target's
      ansible.builtin.debug:
        msg:
          play_target: "{{ inventory_hostname }}"
          delegate_module_interpreter: "{{ hostvars['localhost'].ansible_facts.python.executable }}"

    - name: The delegate resolved its interpreter from the inventory entry for localhost
      ansible.builtin.assert:
        that:
          - hostvars['localhost'].ansible_facts.python.executable == ansible_playbook_python
        success_msg: >-
          The interpreter came from the delegated-to host, which is why delegation
          templates connection options from the delegate and not from the play target.
.venv/bin/ansible-playbook -i hosts.ini interpreter-demo.yml --syntax-check
.venv/bin/ansible-playbook -i hosts.ini interpreter-demo.yml

Expected result, with paths matching your own virtual environment:

TASK [Show the controller and target interpreters side by side] *****
ok: [localhost] => {
    "msg": {
        "controller_interpreter": "/home/you/tma-interpreter/.venv/bin/python",
        "core_version": "2.22.0",
        "target_interpreter": "/home/you/tma-interpreter/.venv/bin/python",
        "target_python": "3.13"
    }
}

TASK [Gather Python facts on the delegate and keep them with the delegate] *****
ok: [probe-host -> localhost]

TASK [Report the delegate's module interpreter, not the play target's] *****
ok: [probe-host] => {
    "msg": {
        "delegate_module_interpreter": "/home/you/tma-interpreter/.venv/bin/python",
        "play_target": "probe-host"
    }
}

localhost   : ok=5  changed=0  unreachable=0  failed=0  skipped=0
probe-host  : ok=3  changed=0  unreachable=0  failed=0  skipped=0

Read the second play's banner carefully: ok: [probe-host -> localhost] is Ansible telling you the task belongs to probe-host but executed on localhost. delegate_facts: true is what keeps the gathered facts with the delegate instead of assigning them to inventory_hostname, which is the default. Without it, the facts would be filed under probe-host and the assertion would be measuring the wrong host.

The gather_subset list is deliberate: "!all", "!min", then python collects only the Python facts, which keeps the run fast and makes it obvious which facts the assertions depend on. Note that ansible_python_version is not part of that subset — read the version from ansible_facts.python.version instead, or add the min subset back. Read facts through ansible_facts rather than the top-level ansible_* aliases; on 2.22 the aliases still work but emit a deprecation warning saying that the INJECT_FACTS_AS_VARS default of True is deprecated and that the feature is to be removed in ansible-core 2.24.

The failure case: a target interpreter that is not there

Pin the interpreter to a path that does not exist and you get the error you will actually see in the field. This play is expected to fail.

---
- name: Point a target at an interpreter that is not there (expected to fail)
  hosts: local
  gather_facts: false

  tasks:
    - name: Collect the Python facts with a bad interpreter path
      ansible.builtin.setup:
        gather_subset:
          - "!all"
          - "!min"
          - python
      vars:
        ansible_python_interpreter: /opt/tma-demo/nonexistent/python3

Expected result:

TASK [Collect the Python facts with a bad interpreter path] *****
[ERROR]: Task failed: Action failed: The module interpreter '/opt/tma-demo/nonexistent/python3'
was not found. Consider overriding the configured interpreter path for this host.

fatal: [localhost]: FAILED! => {"changed": false,
  "module_stderr": "/bin/sh: 1: /opt/tma-demo/nonexistent/python3: not found\n",
  "module_stdout": "", "rc": 127}

Three details make this message easy to read once you know the shape. rc: 127 is the shell's "command not found", so the problem is the path, not Python itself. module_stderr contains the shell's own complaint, which is where the real path appears. And the error names the module interpreter, which tells you this is a target-side problem — a controller problem fails much earlier, before any task runs, usually during pip install or on import.

Diagnostics, in the order worth running them

Start with the controller:

.venv/bin/ansible --version
.venv/bin/python -c 'import sys; print(sys.executable, sys.version)'

Expected result: the python version line in the banner and the path printed by the second command are the same interpreter. If they differ you are running an ansible-core from somewhere else on your PATH — the most common cause of "it works for me" between two engineers on the same repository.

Then find out what configuration is actually in effect:

.venv/bin/ansible-config dump --only-changed
.venv/bin/ansible-config dump | grep -i interpreter

Expected result: --only-changed prints just the settings you have overridden, each annotated with the file that set it, for example INTERPRETER_PYTHON(/home/you/tma-interpreter/ansible.cfg) = auto_silent. The second command shows INTERPRETER_PYTHON alongside INTERPRETER_PYTHON_FALLBACK, which is the ordered list discovery walks.

Then check what the inventory says about the host:

.venv/bin/ansible-inventory -i hosts.ini --host localhost
.venv/bin/ansible -i hosts.ini local -m ansible.builtin.setup -a 'gather_subset=!all,!min,python'
Inventory and fact dumps are not safe to paste into a ticket. ansible-inventory --list and --host print every variable resolved for a host, which in real inventories includes connection users, jump-host configuration, and sometimes credentials. Facts include hostnames, addresses, mounted paths and installed packages. Redact before sharing, and prefer --host for a single host over --list for the whole estate.

Finally, if discovery is choosing something unexpected, remove your pin for one run and read the warning. With interpreter_python = auto and no inventory override, the message names the interpreter it picked:

[WARNING]: Host 'localhost' is using the discovered Python interpreter at
'/usr/bin/python3.13', but future installation of another Python interpreter
could cause a different interpreter to be discovered.

That warning exists precisely because discovery is not stable across the lifetime of a host. There are two distinct responses, and they are not the same thing. Setting auto_silent only suppresses the message: discovery still runs, and the chosen interpreter can still change when a host gains a newer Python. Setting ansible_python_interpreter to an absolute path disables discovery for that host, which is what actually makes the choice stable. Pick the path when you care about reproducibility; pick auto_silent when you have accepted discovery and only want the noise gone.

Remote bootstrap: an illustration, not a runbook

The commands in this section were not executed. They install packages on a managed node. Nothing in this article runs them, and you should not run them against production. Treat them as an illustration of the shape of a bootstrap, to be adapted and tested on a host you own and can rebuild.

When a target has no Python at all, Python modules cannot run on it, so the bootstrap has to use raw, which needs no interpreter. The pattern is to install a Python, then gather facts explicitly once it exists.

---
# ILLUSTRATION ONLY - not executed, and not to be run against production.
- name: Bootstrap a target that has no Python yet
  hosts: new_hosts
  gather_facts: false
  become: true

  tasks:
    - name: Check whether any Python is present
      ansible.builtin.raw: command -v python3 || true
      changed_when: false
      register: python_probe

    - name: Install Python with the distribution's own package manager
      ansible.builtin.raw: apt-get update && apt-get install -y python3
      when: python_probe.stdout | trim | length == 0

    - name: Only now can facts be gathered
      ansible.builtin.setup:

    - name: Record which interpreter was discovered
      ansible.builtin.debug:
        msg: "{{ ansible_facts.python.executable }}"

Two notes. raw runs a shell command with no module, so it has no check-mode support and no idempotence — the when guard above is doing all the work, and check mode will not predict any of it. And the package name is distribution-specific; a single apt-get line is not a cross-platform bootstrap.

Troubleshooting checklist

  • "The module interpreter '…' was not found", rc 127. A target-side path problem. Read module_stderr, then check the inventory variable and interpreter_python for a stale pin — including a leftover auto_legacy.
  • pip rejects ansible-core 2.22 with a Requires-Python error. A controller problem: that specific message means the interpreter running pip is older than 3.13. Create the virtual environment with an explicit python3.13 or newer. Other pip failures — a resolver conflict, a missing wheel, an index or proxy error — have other causes, so read the message before assuming the Python version is at fault.
  • A module imports fine on the controller but not on the target. The library is installed in the controller's environment. Target-side dependencies belong on the target, under the target's interpreter.
  • A delegated task behaves differently from the rest of the play. It is using the delegate's interpreter. Work out which delegate you have: if localhost is not in your inventory the task is using the implicit localhost, which already defaults to the controller's own interpreter; if it is in inventory, the implicit defaults are gone and the interpreter comes from your inventory configuration. Neither is wrong — but for an explicitly declared host, pin ansible_python_interpreter deliberately rather than relying on discovery.
  • The discovery warning appears on every run. That is auto doing its job. Pin an absolute path if you want the choice to stop moving; use auto_silent if you only want the warning gone and are content for discovery to keep choosing.
  • "It works on my machine." Compare ansible --version output, then ansible-config dump --only-changed. One of you has a different config file or a different PATH.

Related reading

Sources