AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Python Interpreter: Fix ansible_python_interpreter Errors (Guide)

By Luca Berton · Published 2024-01-01 · Category: installation

How to configure Ansible Python interpreter. Set ansible_python_interpreter per host, group, or globally. Fix Python discovery warnings, use auto_silent mode.

Introduction

When working with Ansible, ensuring the correct Python interpreter is used on managed nodes is crucial for executing tasks seamlessly. By default, Ansible detects and uses the system’s Python version, but sometimes, specifying a particular Python interpreter is necessary—especially when dealing with multiple Python versions or specific system requirements.

This article explores how to configure the Python interpreter in Ansible using ansible_python_interpreter and why it is important for stable automation.

See also: Ansible Troubleshooting: Fix ModuleNotFoundError 'ansible'

Understanding ansible_python_interpreter

The ansible_python_interpreter variable allows users to explicitly define the Python interpreter that Ansible should use when executing tasks. This is particularly useful in environments where: • Multiple versions of Python are installed. • The default Python version is incompatible with Ansible modules. • The target system does not have Python 3 installed as the default interpreter. • Virtual environments are used.

Default Behavior

By default, Ansible attempts to locate and use /usr/bin/python3. If it’s unavailable, it falls back to /usr/bin/python, and in some cases, it might try /bin/python or another available Python interpreter.

However, in Red Hat-based distributions (RHEL, CentOS, Fedora), the system may use /usr/libexec/platform-python, which is required for system utilities.

Setting ansible_python_interpreter

There are several ways to define the Python interpreter in Ansible:

1. Using Inventory Variables (Recommended)

Specify the Python interpreter in the inventory file:

[servers]
web ansible_host=192.168.1.10 ansible_python_interpreter=/usr/bin/python3
db ansible_host=192.168.1.20 ansible_python_interpreter=/usr/bin/python3.8

Or in a YAML inventory:

all:
  hosts:
    web:
      ansible_host: 192.168.1.10
      ansible_python_interpreter: /usr/bin/python3
    db:
      ansible_host: 192.168.1.20
      ansible_python_interpreter: /usr/bin/python3.8

2. Using an Ansible Playbook

You can define the Python interpreter dynamically within a playbook:

- hosts: all
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: Check Python version
      command: python3 --version
      register: python_version
    - debug:
        msg: "Python version is {{ python_version.stdout }}"

3. Using Extra Variables (-e) at Runtime

If you want to override the Python interpreter during execution, use:

ansible-playbook -i inventory.ini playbook.yml -e ansible_python_interpreter=/usr/bin/python3

4. Using Ansible Configuration File (ansible.cfg)

Set a global Python interpreter in ansible.cfg:

[defaults]
interpreter_python = /usr/bin/python3

See also: Ansible Troubleshooting: Fix Jinja2 Syntax & Inventory Errors

Why Set a Specific Python Interpreter?

Avoid Compatibility Issues: Some Ansible modules require Python 3.x to function correctly. • Ensure Stability: If multiple Python versions exist, defining an explicit interpreter ensures consistency. • Work with Virtual Environments: Specify the path to a virtual environment’s Python binary. • Support Legacy Systems: Some older distributions still use Python 2.7, requiring explicit interpreter definition.

Example: Checking Python Interpreter with Ansible

To verify the Python interpreter being used on a target host:

ansible all -m setup -a "filter=ansible_python_interpreter"

Example output:

"ansible_python_interpreter": "/usr/bin/python3"

This confirms the correct interpreter is in use.

See also: Ansible Troubleshooting: Fix Missing amazon.aws.ec2_ami_info

Conclusion

Setting the correct ansible_python_interpreter ensures a smooth automation experience by eliminating compatibility issues and maintaining consistency across managed hosts. Whether using inventory files, playbooks, or runtime options, defining the Python interpreter is a best practice that enhances reliability in Ansible automation.

If you found this guide helpful, consider subscribing for more Ansible automation tutorials!

Set Python Interpreter

Per host (inventory)

all:
  hosts:
    old-server:
      ansible_python_interpreter: /usr/bin/python3
    centos7:
      ansible_python_interpreter: /usr/bin/python2.7

Per group

# group_vars/ubuntu.yml
ansible_python_interpreter: /usr/bin/python3

# group_vars/centos7.yml ansible_python_interpreter: /usr/bin/python2.7

Globally in ansible.cfg

[defaults]
interpreter_python = auto_silent

Command line

ansible all -m ping -e "ansible_python_interpreter=/usr/bin/python3"

Auto-Detection Modes

| Setting | Behavior | |---------|----------| | auto | Auto-detect with deprecation warnings | | auto_silent | Auto-detect silently (recommended) | | auto_legacy | Prefer /usr/bin/python (old behavior) | | /usr/bin/python3 | Explicit path |

# ansible.cfg — recommended
[defaults]
interpreter_python = auto_silent

Common Errors and Fixes

"No module named 'distutils'"

# Install Python distutils on remote
- raw: apt install -y python3-distutils
  become: true

"/usr/bin/python: not found"

# Bootstrap Python on new hosts
- hosts: new_servers
  gather_facts: false
  tasks:
    - name: Install Python
      ansible.builtin.raw: |
        apt update && apt install -y python3 python3-apt
      become: true

- name: Gather facts now ansible.builtin.setup:

Virtual environment Python

- hosts: app_servers
  vars:
    ansible_python_interpreter: /opt/myapp/venv/bin/python
  tasks:
    - pip:
        name: requests
        # Installs into the venv

Platform Defaults

| OS | Default Python Path | |----|-------------------| | Ubuntu 20.04+ | /usr/bin/python3 | | RHEL 8+ | /usr/bin/python3 | | CentOS 7 | /usr/bin/python2.7 | | macOS | /usr/bin/python3 | | Alpine | /usr/bin/python3 |

Match Controller Python

# Use the same Python as the controller
ansible_python_interpreter: "{{ ansible_playbook_python }}"

FAQ

Why does Ansible need Python on remote hosts?

Ansible copies Python modules to remote hosts and executes them. Most modules require Python. Exception: raw and script modules work without Python.

Can I use pyenv or conda Python?

Yes — set the full path:

ansible_python_interpreter: /home/user/.pyenv/versions/3.11.0/bin/python

How do I check which Python Ansible is using?

ansible myhost -m setup -a "filter=ansible_python*"

This shows ansible_python_version and the interpreter path.

Windows doesn't use Python interpreter?

Correct — Windows modules use PowerShell. The ansible_python_interpreter setting is irrelevant for Windows hosts.

Set Python Interpreter

# inventory.yml
all:
  vars:
    ansible_python_interpreter: /usr/bin/python3

hosts: legacy: ansible_python_interpreter: /usr/bin/python2 modern: ansible_python_interpreter: /usr/bin/python3

# Let Ansible find Python automatically
ansible_python_interpreter: auto

# Silent mode (no warnings) ansible_python_interpreter: auto_silent

# Legacy mode (Python 2 preferred) ansible_python_interpreter: auto_legacy

In ansible.cfg

[defaults]
interpreter_python = auto_silent

Common Scenarios

No Python Installed

# Error: "module_stderr": "/bin/sh: python: not found"

# Fix on remote host ssh user@host sudo apt install python3 # Debian/Ubuntu sudo dnf install python3 # RHEL/Rocky

Wrong Python Version

# Error: module requires Python 3.8+
web1:
  ansible_python_interpreter: /usr/bin/python3.11

Virtual Environment

# Use virtualenv Python
web1:
  ansible_python_interpreter: /opt/myapp/venv/bin/python

Container/Minimal Images

# Alpine Linux
alpine_host:
  ansible_python_interpreter: /usr/bin/python3
  # May need: apk add python3

# Minimal images minimal_host: ansible_python_interpreter: /usr/bin/python3

Per-Task Override

- name: Use specific Python
  command: python3 --version
  vars:
    ansible_python_interpreter: /usr/local/bin/python3.12

Check Current Interpreter

- debug:
    msg: "Using Python: {{ ansible_python_interpreter | default('auto') }}"

- command: "{{ ansible_python_interpreter | default('/usr/bin/python3') }} --version" register: py_version changed_when: false

- debug: msg="{{ py_version.stdout }}"

Discovery Order (auto mode)

/usr/bin/python3 (most distros) /usr/bin/python /usr/bin/python3.X (versioned) Platform-specific paths

Group-Level Configuration

# group_vars/debian.yml
ansible_python_interpreter: /usr/bin/python3

# group_vars/rhel7.yml ansible_python_interpreter: /usr/bin/python2

# group_vars/containers.yml ansible_python_interpreter: /usr/bin/python3

Troubleshooting

| Error | Cause | Fix | |-------|-------|-----| | python: not found | No Python on remote | Install Python | | No module named 'json' | Wrong Python build | Use system Python | | interpreter discovery warning | Auto mode emits warning | Set auto_silent | | module needs Python 3 | Python 2 selected | Set explicit Python 3 path |

FAQ

auto vs auto_silent vs auto_legacy?

auto: Finds Python, warns if unexpected • auto_silent: Finds Python, no warnings (recommended) • auto_legacy: Prefers Python 2 (for old systems)

Do I need Python on the controller AND remote?

Yes — the controller needs Python for Ansible itself, and remote hosts need Python for module execution. Exception: raw module works without remote Python.

What about Windows?

Windows doesn't use Python interpreter — it uses PowerShell. ansible_python_interpreter is ignored for WinRM connections.

Set Python Interpreter

# inventory
[all:vars]
ansible_python_interpreter=/usr/bin/python3

# Per-host [webservers] web1 ansible_python_interpreter=/usr/bin/python3.12

In group_vars

# group_vars/all.yml
ansible_python_interpreter: /usr/bin/python3

# group_vars/legacy.yml ansible_python_interpreter: /usr/bin/python2.7

In Playbook

- hosts: all
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - ping:

Auto-Discovery (Default)

# ansible.cfg
[defaults]
interpreter_python = auto  # Default since Ansible 2.12
# auto_silent — suppress warnings
# auto_legacy — old behavior

Using Virtual Environments

# Point to venv Python
ansible_python_interpreter: /opt/myapp/venv/bin/python

# Or discover dynamically - command: which python3 register: python_path delegate_to: "{{ inventory_hostname }}"

- set_fact: ansible_python_interpreter: "{{ python_path.stdout }}"

Common Error Messages

# "module_stdout": "/usr/bin/python: not found"
# Fix: Set ansible_python_interpreter=/usr/bin/python3

# "ansible_python_interpreter" was not found # Fix: Install Python 3 on the remote host

# "No module named 'apt'" # Fix: Install python3-apt on Debian/Ubuntu

Troubleshoot Discovery

# Check what Ansible finds
ansible myhost -m setup -a "filter=ansible_python*"

# Verbose to see interpreter selection ansible myhost -m ping -vvv | grep python

Per-Platform Configuration

[ubuntu:vars]
ansible_python_interpreter=/usr/bin/python3

[centos7:vars] ansible_python_interpreter=/usr/bin/python2.7

[alpine:vars] ansible_python_interpreter=/usr/bin/python3

FAQ

Why does Ansible need Python on remote hosts?

Ansible modules execute as Python scripts on the remote host. The control node sends the module; the remote host runs it.

What about raw and script modules?

These don't require Python — they execute raw commands. Useful for bootstrapping Python on hosts.

Python 2 vs 3?

Python 2 is EOL. Always use Python 3. Set ansible_python_interpreter=/usr/bin/python3 globally.

Related Articles

organizing hosts with Ansible inventoryWindows users and groups via Ansible

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home