Concept and mechanism
A role organizes related tasks, handlers, variables, files, and other components. Reusing that structure is useful when its interface is understandable: which values consumers must supply, which have defaults, and which constraints apply. Defaults provide options consumers can override within precedence rules.
Do not hide a configurable decision in a layer preventing its intended override. Role argument validation can declare types and requirements, detecting an incorrect contract before dependent work executes. A missing parameter should produce an understandable message rather than a remote failure difficult to relate to the original input.
Guided application
Collections distribute content with a namespace and identity. Using namespace.collection.module clarifies the module, but the collection still needs installation in the correct environment. When obtaining a role from a repository, record an approved stable revision instead of depending on a moving branch.
Pinning helps reproduce the artifact without proving it is safe or suitable. In a fictional middleware project, the platform team publishes a role and APS supplies environment values. Before promotion, rehearse valid and invalid inputs, confirm EE dependencies, and review interface changes.
Also document how to update the dependency so reproducibility does not become permanent obsolescence.
Trace the value actually used
The lab creates a batch_contract role for a fictional batch process. Its window default is 90, but worker inventory supplies 75. The acceptance file contains 75.
Meanwhile, a policy in vars/main.yml overrides the inventory policy. In a later run, extra vars supply window 60 and another policy, replacing both earlier values. Before blaming the template or host, build a table of possible sources and confirm the resolved value in the environment used.
Do not treat the name policy as proof of enforcement: it remains a variable subject to the demonstrated precedence. A production change record should identify inventory, arguments and role revision without exposing sensitive data. A correct file on a laptop does not establish that RUN will resolve the same inputs.
Make the interface a useful contract
The exercise interface allows integers 60, 75 or 90. Value 5 is a valid integer but violates the permitted choices. With validation enabled, the role fails before creating accepted.txt.
With rolespec_validate:false, the same value reaches the file. This comparison shows why a green job can stop demonstrating compliance when the check defining acceptance is removed. Correct the input or formally revise the requirement; removing rejection does not establish approval.
Another run declares default:42 only in argument_specs. Its body observes MISSING because it received no runtime variable holding that value. Keep the effective value in defaults/main.yml and align interface documentation.
Exercise permitted, absent and invalid values instead of testing only the successful example.
Inspect what happened before failure
The guarded role depends on prerequisite. The dependency creates prerequisite.txt; only afterward does parent validation reject approval=pending. parent.txt is never created, but the dependency file remains. An operational report should state both facts: the main body was blocked and an earlier effect occurred.
A real preparation task might create a directory, change permissions or reserve a resource. Recovery depends on that concrete effect rather than only the overall exit code. Before retrying, identify whether preparation is idempotent, repetition is authorized and partial state needs reconciliation.
This lab uses only synthetic local files; its marker is not a real approval mechanism. That boundary lets learners study ordering without representing production authorization.
Bound scope, entry point and repetition
With public:false, a default can be used inside a role while remaining unavailable to the following task. With public:true, the exercise observes MISSING before dynamic inclusion and from-role afterward. The option does not retroactively change an earlier task.
If a prerequisite needs the value, provide an explicit input or revise ordering. Selecting tasks_from:inspect also changes the body executed: inspection.txt appears without setup.txt because main does not implicitly run first. Finally, two identical static counted entries produce one line; two dynamic inclusions with default options produce two.
Do not transfer duplicate-execution assumptions between invocation forms. Decide whether repetition is needed and make the body safe for it. These results were observed on core 2.16.14; options introduced in later versions need their own verification.
Package and discover the intended collection
The exercise builds dr_lab.contracts 1.0.0 from original code, installs its archive offline into package-store and runs dr_lab.contracts.stamp. The marker confirms that role in the local environment. Removing package-store from the active path makes resolution fail.
After copying the same collection into collections/ansible_collections beside the playbook, execution succeeds again even with an empty configured path. This comparison helps when a laptop appears to work without a dependency missing from the pipeline: inspect project-adjacent content too.
An FQCN identifies namespace, collection and content; it neither installs dependencies nor pins a version by itself. Retain the manifest, installed version and actual location. A local archive without signature verification must not be represented as distributor-issued authenticity evidence.
Prepare a repeatable RUN handover
A version range can admit different artifacts in builds sharing the same application commit. To reproduce an approved change, retain resolved versions and rehearse installation in the environment that will be used. If requirements.yml mixes roles and collections, confirm the pipeline installs both: the collection subcommand does not install standalone roles.
For a window without registry access, prepare dependencies too and check the set before connectivity is unavailable. The guided exercise is complete when you can explain each created file, expected absence and deliberate failure. Then add a plan to validate a real role with RUN access, functional criteria and recovery from partial effects.
The local lab does not establish RHEL services, reboot, SSH, AAP or a complete EX294 mock; those require evidence from the relevant environment.
OBSERVED RESULTS
Default 90 + inventory 75 -> 75
Role vars: role-policy; extra var -> extra-policy
Choices 60/75/90 + input 5 -> rejection
rolespec_validate:false + input 5 -> file containing 5
Default only in argument_specs -> MISSING
Parent failure -> dependency effect remains
tasks_from:inspect -> inspection.txt without setup.txt
Duplicate static role: 1; two dynamic includes: 2
Adjacent collection -> discovered even with empty configured pathThe role accepts service_port as an integer; an incompatible input should fail with a contract indication.
Common pitfalls
Reuse without interface; FQCN without installation; moving branch as fixed version; pinning as a security guarantee.
Related topics: State and repeatability · Inventories, variables, and configuration · Access and execution environment
Identify the actual contract and contents, observe partial effects, and give RUN an execution it can repeat.
References
- Role organization and variables · Current primary reference inspected 2026-09-30
- include_role: dynamic execution and scope · Current upstream guide; exercised semantics bounded to ansible-core 2.16.14
- Installing and locating collections · Current upstream guide; exercised semantics bounded to ansible-core 2.16.14