What are Facts?
Facts are Python classes that gather information about a host by executing commands and processing the output. They provide a consistent interface for reading system state.Fact Categories
Pyinfra includes facts organized by category:System Facts
- Server - System information (hostname, OS, architecture, users, groups)
- Hardware - CPU, memory, disk information
File System Facts
- Files - File and directory information
- File - Single file metadata
- Directory - Directory contents
- FindInFile - Search within files
Package Manager Facts
- Packages - Installed packages across package managers
- Apt - Debian/Ubuntu packages
- Yum/Dnf - Red Hat/CentOS packages
- Apk - Alpine packages
- Brew - macOS packages
- Pip - Python packages
- Npm - Node.js packages
- Gem - Ruby packages
Service Facts
- Systemd - Systemd services
- Upstart - Upstart services
- Launchd - macOS services
- Sysvinit - SysV init services
Application Facts
- Git - Git repositories
- Docker - Docker containers and images
- MySQL - MySQL databases
- PostgreSQL - PostgreSQL databases
Using Facts
In Operations
Use facts to make operations conditional:Check File Exists
Check Package Installed
Fact API
FactBase Class
All facts inherit fromFactBase:
Creating Custom Facts
Create custom facts for your specific needs:Fact Caching
Facts are cached per host to avoid redundant execution:Fact Execution
Facts can use global arguments:CLI Usage
Query facts from the command line:Best Practices
1. Cache Facts in Variables
2. Handle Missing Facts
3. Use Specific Facts
4. Reuse Facts
Create reusable fact-based checks:Fact Types
Simple Facts
Return a single value:Dict Facts
Return structured data:List Facts
Return multiple items:Parameterized Facts
Accept arguments:Error Handling
Facts can fail if commands error:None for missing items:
Source Reference
Location:src/pyinfra/api/facts.py
Key Classes
FactBase- Base class for all facts (line 53)ShortFactBase- Shorthand fact wrapper (line 98)
Key Functions
get_fact()- Get fact value (line in facts.py)
