Feedback

Chat Icon

AWX in Action

Ansible Orchestration at Scale

From Playbook to Production: Projects, Templates, Jobs, and Workflows in Practice
57%

Workflow Templates: Chaining Jobs Into a DAG

This is what we have so far:

Two static inventories, SolaraInventory and NebulaInventory, each pointing at real hosts. One constructed inventory, AllInventory, that pulls from both source inventories and groups hosts by BIOS year using facts from the cache:

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

We'll use the above code in Source vars of the AllInventory inventory. Update the inventory if needed.

We also have 2 job templates:

  • GatherFacts: runs against SolaraInventory and NebulaInventory, has gather_facts: true and Use Fact Storage enabled. Its only job is to populate the AWX fact cache.
  • My AWX job template: the real work. We want to run it against AllInventory. The constructed inventory plugin reads ansible_bios_date from the fact cache when AWX auto-syncs the inventory on job launch, builds the bios_pre_2000 and bios_2000_or_later groups, and the playbook targets them.

My AWX job template only works if GatherFacts ran recently enough that the cache is populated and the constructed inventory has the right groups. So far we've solved that with a schedule that fires GatherFacts every 5 minutes.

The scheduled fact-gather job works, but it's wasteful. Facts get gathered every 5 minutes whether or not a job is queued to consume them, which means continuous SSH traffic to every host for cache updates nobody is reading most of the time. A workflow template is the cleaner pattern. We can create a workflow template that does the following:

  • Runs the GatherFacts job template first (against the source inventories).
  • Runs My AWX job template against the constructed inventory.
  • The second job auto-syncs the constructed inventory on launch, reads the cache that was just populated, and operates on correct groups. Facts get gathered exactly when needed, no sooner.

We will build 2 nodes, chained on success:

[GatherFacts] ---on success---> [My AWX job template]

Start by deleting or disabling the every-5-minutes schedule on GatherFacts. Navigate to Templates > GatherFacts > Schedules and disable the schedule.

Now, go to the Templates menu, click on Add Workflow Template, and provide the following information:

  • Name: My AWX Job Workflow Template
  • Description: A workflow template that first runs the GatherFacts job template and then executes the My AWX Job Template job template.
  • Organization: Leave this field blank.
  • Inventory: Select the AllInventory inventory.
  • Labels: Add the demo label.
  • Source Control Branch: Use main as the branch or leave it empty.
  • Limit, Variables, Job Tags, Skip Tags: Leave these fields blank.

The Source Control Branch field is relevant for all job template nodes that request a branch. If this field is left blank, the branch will be prompted for each job template node that requires one.

Save to create the new workflow template. You'll be redirected to the workflow template details page where you'll be prompted to add a new node.

A node represents a job template, a step in the workflow. You can add multiple nodes to the workflow template and define the sequence in which they should execute.

A workflow node can be one of the following types:

AWX in Action

Ansible Orchestration at Scale

Enroll now to unlock all content and receive all future updates for free.