Course outline

Ansible / Intermediate: reuse, secrets, and scoped validation / Lesson 14

Review dynamic inventory and cache freshness

Compare dynamic inventory source, filters and cache evidence before selecting Ansible targets.

About 3 min readContent reviewed
On this page

Lesson goals

  • Explain the role of an inventory plugin and optional cache.
  • Identify stale or unexpected host selection.
  • Verify a target list before an authorized run.

Understand changing inventory

An inventory plugin can derive hosts and groups from an external source whose contents change over time. Some plugins support caching, but behavior and configuration depend on the plugin. A cached answer is an observation from an earlier time, not proof that a host is still an approved target. The records here are synthetic; no plugin, cloud API or host was contacted.

State the expected scope

The first record describes a fictional lab filter, enabled cache and two expected hosts. Record the actual plugin, configuration revision, account or source scope, filter and intended group before resolving an inventory. Access to the source is a separate authorization boundary. Do not include source credentials or sensitive host details in a shared investigation record.

yaml Synthetic inventory intent
inventory_source: synthetic-plugin
filter: environment=lab
cache_enabled: true
expected_hosts: [lab-a, lab-b]

The record states expectations and is not a plugin configuration.

Compare observed hosts

The second record includes a retired host and leaves cache age unresolved. Compare the actual host list with the approved target list and investigate source filters and cache freshness. An extra host must not be ignored merely because the playbook would probably skip a task. A missing host can also affect capacity or maintenance coverage. Stop modifying runs until the target set is understood.

yaml Synthetic observation
observed_hosts: [lab-a, lab-b, retired-lab-c]
source_revision: unknown
cache_age: review-required

The record describes a mismatch and does not claim that an inventory query ran.

Trace the cache and source

Check plugin documentation for cache support, timeout and refresh behavior. Compare an authorized fresh source observation with the cache without exposing credentials. A cache miss differs from a valid empty source. Do not clear a shared cache without reviewing its impact.

Verify a bounded correction

In an isolated lab, use synthetic inventory data to test a current host, a removed host and a cache refresh. Review the final host list after any approved filter or cache correction and verify that the retired host is excluded. Preserve timestamps and configuration identity. A matching list proves the observed selection at that time, not host reachability or task authorization.

Common errors and fixes

An old cached host is treated as current

Why it happens
The source changed after the cached observation.
How to fix it
Compare freshness and the authorized source scope.
Prevent it next time
Record cache policy and review target lists.

An empty inventory is treated as a successful filter

Why it happens
It may reflect a source error or wrong scope.
How to fix it
Inspect plugin diagnostics and source access.
Prevent it next time
Test known positive and negative hosts.

A correct host list is treated as permission to modify

Why it happens
Selection does not grant remote authorization.
How to fix it
Review identity and playbook actions separately.
Prevent it next time
Require explicit target and permission checks.

2 practice exercises

1. The cache contains a retired host but the source no longer does. What next?

Show worked solution

Stop modifying runs, verify the source and cache context, then use an approved bounded refresh and compare the final targets.

2. The host list matches but the remote user is unexpected. Is the run ready?

Show worked solution

No. Verify the actual identity and any privilege escalation before authorizing the specific operation.

Key takeaways

  • Dynamic inventory is a time-bound source observation.
  • Cache and filters affect the final target list.
  • Target selection and execution authorization are separate.

Official references