Introduction
Ansible requires Python on managed nodes to execute most modules. When Python is not installed, the wrong version is configured, or required Python packages are missing, tasks fail with "module not found" or "interpreter not found" errors. This issue commonly occurs on fresh minimal OS installations, container images, or when managing systems that have been customized without the standard Python environment.
The problem manifests as MODULE FAILURE errors during playbook execution, blocking all automation until Python is properly bootstrapped on the target systems.
Symptoms
Python interpreter not found:
$ ansible target-server -m ping
target-server | FAILED! => {
"msg": "Failed to find required interpreter python3 in expected search paths",
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"failed": true
}Python module not found:
$ ansible target-server -m dnf -a "name=nginx state=present"
target-server | FAILED! => {
"msg": "The Python 2 bindings for rpm are needed for this module. If you require Python 3 support use the `dnf` module instead. `dnf` module is not installed",
"failed": true
}Raw module failure:
$ ansible target-server -m setup
target-server | FAILED! => {
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 127,
"stdout": "",
"stderr": "/bin/sh: 1: /usr/bin/python: not found\r\n/bin/sh: 1: /usr/bin/python: not found"
}Version mismatch errors:
$ ansible target-server -m yum -a "name=httpd state=present"
target-server | FAILED! => {
"msg": "The yum module requires the yum python bindings. These are typically provided by the 'python2-dnf' or 'python3-dnf' packages.",
"failed": true
}Bootstrap scenario - minimal container:
$ ansible container-host -m apt -a "name=curl state=present"
container-host | FAILED! => {
"msg": "Failed to find required interpreter python in expected search paths:\n/usr/bin/python\n/usr/bin/python3\n/usr/bin/python3.9",
"failed": true
}Checking Python status on target:
```bash $ ansible target-server -m raw -a "which python python3" target-server | FAILED | rc=127 >> /bin/sh: 1: which: not found /bin/sh: 1: python: not found /bin/sh: 1: python3: not found
# Target has no Python installed! ```
Common Causes
1. Minimal OS Images Without Python
Container images (Alpine, minimal Debian) often exclude Python:
$ docker run --rm alpine:latest python3
docker: Error response from daemon: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"python3\": executable file not found in $PATH": unknown.2. Python Version Mismatch
Ansible defaults to Python 3, but target only has Python 2:
```bash $ ssh target-server "python --version" Python 2.7.17
$ ssh target-server "python3 --version" -bash: python3: command not found ```
3. Missing Python Packages
Module-specific packages not installed:
# dnf module requires python3-dnf
- name: Install package
dnf:
name: nginx
state: present
# Fails if python3-dnf not installed4. Incorrect Interpreter Configuration
ansible_python_interpreter set to wrong path:
# inventory
[target_servers]
server-01 ansible_python_interpreter=/usr/bin/python3.11
# But target only has python3.95. Virtual Environment Not Activated
Ansible not using the correct virtual environment:
$ which python
/usr/bin/python
# But modules are installed in /opt/venv/bin/python6. SELinux/AppArmor Restrictions
Security policies preventing Python execution:
$ ansible target -m ping
target | FAILED! => {
"msg": "MODULE FAILURE",
"stderr": "SELinux is preventing /usr/bin/python3 from execute access"
}Step-by-Step Fix
Step 1: Diagnose Python Availability on Target
Check what Python versions exist:
```bash # Use raw module (doesn't require Python) ansible target-servers -m raw -a "ls -la /usr/bin/python*"
# Check with shell fallback ansible target-servers -m raw -a "command -v python3 || command -v python || echo 'NO PYTHON'"
# Get Python version if exists ansible target-servers -m raw -a "python3 --version 2>/dev/null || python --version 2>/dev/null" ```
Check for module-specific dependencies:
```bash # Check for pip ansible target-servers -m raw -a "pip3 --version || pip --version || echo 'NO PIP'"
# Check for dnf bindings ansible target-servers -m raw -a "python3 -c 'import dnf' 2>/dev/null && echo 'dnf OK' || echo 'NO DNF'"
# Check for apt module support ansible target-servers -m raw -a "python3 -c 'import apt' 2>/dev/null && echo 'apt OK' || echo 'NO APT'" ```
Step 2: Set Correct Python Interpreter
Update inventory with correct interpreter path:
```ini # inventory.ini [target_servers] server-01 ansible_python_interpreter=/usr/bin/python3 server-02 ansible_python_interpreter=/usr/bin/python3.9 server-03 ansible_python_interpreter=/opt/venv/bin/python
[target_servers:vars] ansible_python_interpreter=/usr/bin/python3 ```
Auto-discovery in ansible.cfg:
[defaults]
interpreter_python = auto_silent
# Or explicitly:
# interpreter_python = /usr/bin/python3Step 3: Bootstrap Python on Minimal Systems
Install Python using raw module:
```yaml # bootstrap_python.yml - name: Bootstrap Python on target systems hosts: all gather_facts: false vars: ansible_python_interpreter: auto_silent
tasks: - name: Check if Python is installed raw: "command -v python3 || command -v python" register: python_check changed_when: false failed_when: false
- name: Install Python (Debian/Ubuntu)
- raw: |
- apt-get update && \
- apt-get install -y python3 python3-pip
- when:
- - python_check.rc != 0
- - ansible_os_family | default('Debian') == 'Debian'
- become: true
- name: Install Python (RHEL/CentOS)
- raw: |
- yum install -y python3 python3-pip || \
- dnf install -y python3 python3-pip
- when:
- - python_check.rc != 0
- - ansible_os_family | default('RedHat') == 'RedHat'
- become: true
- name: Install Python (Alpine)
- raw: apk add --no-cache python3 py3-pip
- when:
- - python_check.rc != 0
- - ansible_os_family | default('Alpine') == 'Alpine'
- become: true
- name: Verify Python is now available
- raw: "python3 --version"
- changed_when: false
- name: Gather facts now that Python is available
- setup:
`
Run bootstrap playbook:
ansible-playbook bootstrap_python.yml -i inventory.iniStep 4: Install Required Python Packages
Install module dependencies:
```yaml # install_python_deps.yml - name: Install Python dependencies for Ansible modules hosts: all become: true
tasks: - name: Install dnf Python bindings (RHEL/CentOS 8+) dnf: name: - python3-dnf - python3-firewall state: present when: ansible_os_family == 'RedHat' and ansible_distribution_major_version | int >= 8
- name: Install yum Python bindings (RHEL/CentOS 7)
- yum:
- name:
- - python-yum
- - python-firewall
- state: present
- when: ansible_os_family == 'RedHat' and ansible_distribution_major_version | int == 7
- name: Install apt Python module (Debian/Ubuntu)
- apt:
- name:
- - python3-apt
- - python3-distutils
- state: present
- when: ansible_os_family == 'Debian'
- name: Install SELinux Python bindings
- package:
- name:
- - python3-libselinux
- - python3-policycoreutils
- state: present
- when: ansible_selinux.status == 'enabled'
`
Step 5: Create a Bootstrap Script for Quick Setup
Create a standalone bootstrap script:
```bash #!/bin/bash # bootstrap_ansible_target.sh
set -e
# Detect OS if [ -f /etc/debian_version ]; then OS_FAMILY="Debian" PKG_MANAGER="apt-get" PKGS="python3 python3-pip python3-apt" elif [ -f /etc/redhat-release ]; then if command -v dnf &> /dev/null; then OS_FAMILY="RedHat" PKG_MANAGER="dnf" PKGS="python3 python3-pip python3-dnf" else OS_FAMILY="RedHat" PKG_MANAGER="yum" PKGS="python python-pip" fi elif [ -f /etc/alpine-release ]; then OS_FAMILY="Alpine" PKG_MANAGER="apk" PKGS="python3 py3-pip" else echo "Unsupported OS" exit 1 fi
echo "Detected OS family: $OS_FAMILY" echo "Installing packages: $PKGS"
case $OS_FAMILY in Debian) $PKG_MANAGER update $PKG_MANAGER install -y $PKGS ;; RedHat) $PKG_MANAGER install -y $PKGS ;; Alpine) $PKG_MANAGER add $PKGS ;; esac
# Verify installation python3 --version pip3 --version
echo "Bootstrap complete. Ansible can now manage this host." ```
Deploy and run bootstrap script:
```yaml - name: Deploy and run bootstrap script hosts: all gather_facts: false vars: bootstrap_script: bootstrap_ansible_target.sh
tasks: - name: Copy bootstrap script raw: "cat > /tmp/{{ bootstrap_script }} << 'EOF'\n{{ lookup('file', bootstrap_script) }}\nEOF" changed_when: true
- name: Run bootstrap script
- raw: "chmod +x /tmp/{{ bootstrap_script }} && /tmp/{{ bootstrap_script }}"
- changed_when: true
- name: Clean up bootstrap script
- raw: "rm -f /tmp/{{ bootstrap_script }}"
- changed_when: true
- name: Verify Python is available
- raw: "python3 --version"
- changed_when: false
`
Step 6: Configure for Specific Environments
For container targets:
```yaml - name: Bootstrap Python in containers hosts: containers gather_facts: false vars: ansible_python_interpreter: /usr/local/bin/python3
tasks: - name: Install Python in Alpine container raw: apk add --no-cache python3 py3-pip when: ansible_distribution == 'Alpine'
- name: Install Python in Debian container
- raw: apt-get update && apt-get install -y python3 python3-pip python3-apt
- when: ansible_distribution == 'Debian'
`
For systems with virtual environments:
```yaml - name: Configure virtual environment hosts: app_servers vars: venv_path: /opt/venv
tasks: - name: Create virtual environment command: python3 -m venv {{ venv_path }} args: creates: "{{ venv_path }}/bin/python"
- name: Install required packages in venv
- pip:
- name:
- - setuptools
- - wheel
- - pyyaml
- virtualenv: "{{ venv_path }}"
- name: Set interpreter to venv Python
- set_fact:
- ansible_python_interpreter: "{{ venv_path }}/bin/python"
`
Verification
Test Python availability:
```bash # Test ping (requires Python) ansible all -m ping
# Test with specific interpreter ansible all -m ping -e "ansible_python_interpreter=/usr/bin/python3"
# Test gather facts ansible all -m setup -a "filter=ansible_python*"
# Check Python version on all hosts ansible all -m command -a "python3 --version" ```
Verify module-specific functionality:
```bash # Test dnf module ansible rhel_hosts -m dnf -a "name=curl state=present" -b
# Test apt module ansible debian_hosts -m apt -a "name=curl state=present" -b
# Test service module ansible all -m service -a "name=nginx state=started" -b ```
Check Python packages installed:
```bash # List pip packages ansible all -m pip -a "list=true virtualenv=/opt/venv"
# Check specific module import ansible all -m command -a "python3 -c 'import dnf; print(dnf.__version__)'" ```
Related Issues
- [ansible-bootstrap-python](/articles/ansible-bootstrap-python)
- [ansible-container-management](/articles/ansible-container-management)
- [ansible-virtual-environment-configuration](/articles/ansible-virtual-environment-configuration)
Related Articles
- [WordPress troubleshooting: Ansible Artifact Download Uses an Old Mi](ansible-artifact-download-uses-an-old-mirror-after-proxy-change)
- [WordPress troubleshooting: Ansible Audit Trail Misses Events Under ](ansible-audit-trail-misses-events-under-burst-load)
- [WordPress troubleshooting: Ansible Background Worker Gets Stuck in ](ansible-background-worker-stuck-in-a-retry-loop)
- [WordPress troubleshooting: Ansible Backup Completes but Restore Fai](ansible-backup-completes-but-restore-fails-checksum-validation)
- [WordPress troubleshooting: Ansible Batch Importer Duplicates Rows A](ansible-batch-importer-duplicates-rows-after-a-retry)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "WordPress troubleshooting: Ansible Module Not Found - Python Depend", "description": "Learn how to fix Ansible Module Not Found - Python Dependency Missing. Professional WordPress troubleshooting solutions with step-by-step guidance. WP error fix, WordPress optimization, WP security, WordPress performance.", "url": "https://www.fixwikihub.com/ansible-module-not-found-python-dependency", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2025-12-15T20:47:26.288Z", "dateModified": "2025-12-15T20:47:26.288Z" } </script>