From Playbook to Production: Projects, Templates, Jobs, and Workflows in Practice
Scheduling Jobs (the Right and Wrong Ways)
Previously, we created a constructed inventory that groups hosts based on Ansible facts and host variables.
Reminder: this is the Source vars of the constructed inventory:
---
plugin: constructed
strict: true
compose:
bios_year: ansible_bios_date | default('01/01/1970') | regex_search('[0-9]{4}$')
groups:
bios_pre_2000: bios_year | int < 2000
bios_2000_or_later: bios_year | int >= 2000
To use facts in a constructed inventory, you need to populate the fact cache first. AWX won't get the facts by itself.
You need to follow these steps if you're using facts that were not gathered yet by AWX:
- Enable fact caching globally in AWX settings.
- Create and run a fact-gather job template against the source inventory with
gather_facts: trueand Use Fact Storage enabled on the template. - Sync the constructed inventory so it reads facts from the cache and builds the groups.
For production, either chain the fact-gather job and the inventory sync in a workflow template, or schedule a fact-gather job to run every N minutes, where N is shorter than the fact cache timeout so the cache never goes stale.
This setup is a stepping stone to teach scheduling, not the cleanest way to drive a constructed inventory from facts (a workflow chaining the fact-gather job and the inventory sync is tighter). The fact cache TTL is configurable under Settings > Jobs > Per-Host Ansible Fact Cache Timeout, in seconds. Set your gather interval below that value.
Create a new project under $PROJECTS_PATH called GatherFacts and add a new playbook called gather_facts.yml with the following content:
# Create the project directory
mkdir -p $PROJECTS_PATH/GatherFacts
# Create the playbook
cat << 'EOF' > $PROJECTS_PATH/GatherFacts/gather_facts.yml
---
- hosts: all
gather_facts:AWX in Action
Ansible Orchestration at ScaleEnroll now to unlock all content and receive all future updates for free.
