Skip to content
CloudOps
All prompts
AI for Infrastructure as Code Difficulty: Intermediate ClaudeChatGPT

Ansible Facts & Caching Prompt

Tune Ansible fact gathering — gather_subset, custom facts, local facts, cache backends (jsonfile, Redis), staleness.

Target user
Ansible engineers optimizing fact gathering
Difficulty
Intermediate
Tools
Claude, ChatGPT

The prompt

You are a senior Ansible engineer who has tuned fact gathering for fleets of thousands.

I will provide:
- Fact usage pattern
- Current config
- Symptom (slow gather, stale facts, missing custom fact)

Your job:

1. **Fact gathering**:
   - Default at start of each play
   - All facts about target host
   - Slow on many hosts
2. **For `gather_subset`**:
   - Limit to subset (network, hardware, etc.)
   - `gather_subset: !all,min` for minimal
3. **For custom facts**:
   - `ansible.builtin.set_fact` — runtime
   - `meta: set_facts` — persistent
   - Local facts in `/etc/ansible/facts.d/*.fact` — JSON/INI
4. **For fact caching**:
   - **jsonfile** — disk cache
   - **redis** — shared cache for multiple controllers
   - **memcached** — fast in-memory
   - `fact_caching_timeout` — TTL
5. **For smart gathering**:
   - `gathering = smart`
   - Only gather if not cached
   - Best of both
6. **For skipping**:
   - `gather_facts: false`
   - When facts not needed
7. **For local facts (target-side)**:
   - JSON file at `/etc/ansible/facts.d/myapp.fact`
   - Available as `ansible_local.myapp.<key>`
   - Set by external script
8. **For staleness**:
   - Cache TTL must match change rate
   - For changing facts (IP, package versions), short TTL

Mark DESTRUCTIVE: long cache TTL with rapidly changing facts (wrong actions), removing fact gathering when playbook depends, custom fact with sensitive data in /etc.

---

Fact usage: [DESCRIBE]
Current config: [DESCRIBE]
Symptom: [DESCRIBE]

Why this prompt works

Facts are foundational. This prompt walks tuning.

How to use it

  1. Profile fact gather time.
  2. Use subset if you don’t need all.
  3. Cache for repeat runs.
  4. Custom facts for app state.

Useful commands

# Show all facts for a host
ansible -i inventory <host> -m setup

# Show subset
ansible -i inventory <host> -m setup -a 'gather_subset=network'

# Show only specific
ansible -i inventory <host> -m setup -a 'filter=ansible_default_ipv4'

# Local facts test
sudo touch /etc/ansible/facts.d/myapp.fact
echo '{"version":"1.2.3","status":"running"}' > /etc/ansible/facts.d/myapp.fact
ansible -i inventory <host> -m setup -a 'filter=ansible_local'

Patterns

Subset gathering

- hosts: web
  gather_facts: true
  gather_subset:
    - '!all'
    - '!min'
    - network
    - distribution
  tasks: ...

Smart cache (ansible.cfg)

[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
fact_caching_timeout = 3600

Redis cache

[defaults]
gathering = smart
fact_caching = redis
fact_caching_connection = redis.example.com:6379:0  # host:port:db
fact_caching_timeout = 7200

Custom local fact (script-driven)

# /etc/ansible/facts.d/myapp.fact
#!/bin/bash
# Output JSON
cat <<EOF
{
  "version": "$(/usr/local/bin/myapp --version | awk '{print $NF}')",
  "running": $(systemctl is-active myapp >/dev/null && echo true || echo false),
  "last_deploy": "$(stat -c %y /opt/myapp/release.txt)"
}
EOF
chmod +x /etc/ansible/facts.d/myapp.fact

In playbook:

- name: Use custom fact
  debug:
    msg: "MyApp version: {{ ansible_local.myapp.version }}, running: {{ ansible_local.myapp.running }}"

Skip gathering in subsequent plays

# First play gathers + caches
- hosts: all
  gather_facts: true

# Second play uses cache
- hosts: web
  gather_facts: false           # cached from first play
  tasks:
  - debug: var=ansible_distribution

Set_fact for derived

- name: Compute and cache derived value
  set_fact:
    app_install_path: "{{ '/opt/' + ansible_distribution_release + '/myapp' }}"
    cacheable: true             # persists in fact cache

Common findings this catches

  • Slow runs → cache + smart gathering.
  • Custom fact not visible → permissions on /etc/ansible/facts.d.
  • Cache stale → reduce TTL or flush.
  • Missing fact in playbook → gather_subset too aggressive.
  • Redis cache failing → connection / auth.
  • Local fact script slow → optimize.
  • Memory cache growth → fact size.

When to escalate

  • Cache backend (Redis) scaling — engineering.
  • Custom fact strategy — coordinate.
  • Fact gather optimization — at scale.

Related prompts

Newsletter

Get weekly AI workflows for DevOps engineers

Practical prompts, automation ideas, and tool reviews for infrastructure engineers. One email per week. No spam.