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

bash
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:

bash
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:

bash
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:

ini
[webservers]
webserver ansible_python_interpreter=/usr/bin/python3

Or 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:

ini
[defaults]
interpreter_python = auto_silent

The 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:

bash
ssh user@webserver "ls -la ~/.ansible/tmp"

Fix permissions:

yaml
- hosts: all
  tasks:
    - name: Ensure temp directory exists
      file:
        path: ~/.ansible/tmp
        state: directory
        mode: '0700'

Or specify a different temp directory:

ini
[defaults]
remote_tmp = /tmp/.ansible-${USER}/tmp

sudo/privilege Issues

Fact gathering may need elevated privileges for some information.

Disable fact gathering for specific plays:

yaml
- hosts: all
  gather_facts: false
  tasks:
    - name: Do something that doesn't need facts
      ping:

Gather facts with become:

yaml
- 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:

bash
ssh user@webserver "getenforce"

Temporarily disable for troubleshooting:

bash
ssh user@webserver "sudo setenforce 0"

Install required SELinux Python bindings:

bash
# On the target
sudo yum install libselinux-python3

Fact Gathering Timeout

On slow systems or networks, fact gathering can timeout.

Increase timeout:

yaml
- hosts: all
  vars:
    ansible_timeout: 60
  tasks:
    # ...

Or in ansible.cfg:

ini
[defaults]
timeout = 60

Gather minimal facts to speed up:

yaml
- hosts: all
  gather_subset:
    - min
    - network
  tasks:
    - name: Show network facts
      debug:
        var: ansible_default_ipv4

Disk 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:

yaml
- 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. 1.Python installed? ssh host "python3 --version"
  2. 2.Python path correct? Set ansible_python_interpreter
  3. 3.Temp directory writable? Check ~/.ansible/tmp
  4. 4.Timeout issues? Increase timeout values
  5. 5.Privilege issues? Add become: yes
  6. 6.SELinux blocking? Install libselinux-python3
  • [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>