Your playbook fails immediately at the start with a fact gathering error. The setup module that collects facts about target systems is one of the first things Ansible runs, and when it fails, everything downstream fails. This error has several common causes, from Python interpreter issues to permission problems.
Introduction
This article covers troubleshooting steps and solutions for Fix Ansible Fact Gathering Failed Error. The error typically occurs in production environments and can cause service disruptions if not addressed promptly.
Symptoms
TASK [Gathering Facts] *********************************************************
fatal: [webserver]: FAILED! => {
"msg": "failed to transfer file to /home/user/.ansible/tmp/ansible-tmp-1640000000.0-12345-123456789/setup.py:\n\nfatal: [webserver]: UNREACHABLE!: Failed to connect to the host via ssh: Shared connection to 192.168.1.50 closed."
}Or:
fatal: [webserver]: FAILED! => {
"ansible_facts": {},
"changed": false,
"failed": true,
"msg": "The module failed to execute correctly, you probably need to install the python interpreter on the target host."
}Or the Python interpreter error:
fatal: [webserver]: FAILED! => {
"msg": "failed to resolve remote temporary directory from ansible_ssh_user: ansible was unable to find a python interpreter on the target host"
}Common Causes
- Configuration misconfiguration
- Missing or incorrect credentials
- Network connectivity issues
- Version compatibility problems
- Resource exhaustion or limits
- Permission or access denied
Step-by-Step Fix
Test basic connectivity and Python:
```bash # Test SSH connection ssh user@webserver "echo connected"
# Test Python availability ssh user@webserver "python3 --version"
# Test Python execution ansible webserver -m raw -a "python3 -c 'print(1+1)'"
# Test with minimal gathering ansible webserver -m setup -a "gather_subset=minimal" ```
Common Causes and Fixes
Python Interpreter Not Found
Ansible needs Python on the target host. If Python is missing or in a non-standard location:
Specify Python interpreter in inventory:
[webservers]
webserver ansible_python_interpreter=/usr/bin/python3Or discover and set automatically:
```yaml - hosts: all gather_facts: false tasks: - name: Find Python raw: which python3 || which python register: python_path changed_when: false
- name: Set Python interpreter
- set_fact:
- ansible_python_interpreter: "{{ python_path.stdout.strip() }}"
- name: Now gather facts
- setup:
`
Or in ansible.cfg for all hosts:
[defaults]
interpreter_python = auto_silentThe auto_silent option automatically discovers Python without warnings.
Bootstrap Python on Target
If Python is not installed at all:
```yaml - hosts: all gather_facts: false become: yes tasks: - name: Check for Python raw: test -e /usr/bin/python3 register: python_check failed_when: false changed_when: false
- name: Install Python (Debian/Ubuntu)
- raw: apt-get update && apt-get install -y python3
- when:
- - python_check.rc != 0
- - ansible_os_family is defined or 'Debian' in ansible_distribution
- name: Install Python (RHEL/CentOS)
- raw: yum install -y python3
- when:
- - python_check.rc != 0
- - ansible_os_family is defined or 'RedHat' in ansible_distribution
- name: Gather facts now that Python is installed
- setup:
`
Permission Denied on Temp Directory
The remote user may not have write access to the temp directory.
Check temp directory:
ssh user@webserver "ls -la ~/.ansible/tmp"Fix permissions:
- hosts: all
tasks:
- name: Ensure temp directory exists
file:
path: ~/.ansible/tmp
state: directory
mode: '0700'Or specify a different temp directory:
[defaults]
remote_tmp = /tmp/.ansible-${USER}/tmpsudo/privilege Issues
Fact gathering may need elevated privileges for some information.
Disable fact gathering for specific plays:
- hosts: all
gather_facts: false
tasks:
- name: Do something that doesn't need facts
ping:Gather facts with become:
- hosts: all
become: yes
tasks:
- name: Facts will be gathered with sudo
# ...SELinux/AppArmor Blocking
On systems with mandatory access control, fact gathering might be blocked.
Check SELinux:
ssh user@webserver "getenforce"Temporarily disable for troubleshooting:
ssh user@webserver "sudo setenforce 0"Install required SELinux Python bindings:
# On the target
sudo yum install libselinux-python3Fact Gathering Timeout
On slow systems or networks, fact gathering can timeout.
Increase timeout:
- hosts: all
vars:
ansible_timeout: 60
tasks:
# ...Or in ansible.cfg:
[defaults]
timeout = 60Gather minimal facts to speed up:
- hosts: all
gather_subset:
- min
- network
tasks:
- name: Show network facts
debug:
var: ansible_default_ipv4Disk Space Issues
No space to write temp files.
Check disk space:
```yaml - hosts: all gather_facts: false tasks: - name: Check disk space raw: df -h / register: disk_space
- name: Show disk space
- debug:
- msg: "{{ disk_space.stdout_lines }}"
`
Alternative: Skip Fact Gathering
If you don't need facts, skip them entirely:
- hosts: all
gather_facts: false
tasks:
- name: Simple task
command: echo "No facts needed"Then selectively gather what you need:
```yaml - hosts: all gather_facts: false tasks: - name: Gather only network facts setup: gather_subset: - network filter: ansible_eth*
- name: Use network facts
- debug:
- msg: "IP is {{ ansible_eth0.ipv4.address }}"
`
Verification
After fixing, verify fact gathering works:
```bash # Full fact gathering ansible webserver -m setup
# Specific facts ansible webserver -m setup -a "filter=ansible_memory_mb"
# Minimal facts ansible webserver -m setup -a "gather_subset=minimal"
# Pretty print specific fact ansible webserver -m setup | grep ansible_distribution ```
Quick Fix Checklist
- 1.Python installed?
ssh host "python3 --version" - 2.Python path correct? Set
ansible_python_interpreter - 3.Temp directory writable? Check
~/.ansible/tmp - 4.Timeout issues? Increase timeout values
- 5.Privilege issues? Add
become: yes - 6.SELinux blocking? Install
libselinux-python3
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": "Fix Ansible Fact Gathering Failed Error", "description": "Learn how to fix Ansible fact gathering failures with Python interpreter fixes, permission solutions, and alternative fact gathering methods.", "url": "https://www.fixwikihub.com/fix-ansible-fact-gathering-failed", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2025-11-11T20:56:14.705Z", "dateModified": "2025-11-11T20:56:14.705Z" } </script>