The module lifecycle stage: Experimental
The module has requirements for installation
This page holds ready-made scenarios of working with the ansible module. The scenarios do not depend on each other, and each of them carries a manifest and a description of what appears in the status of the run after it is executed. Every field of a run is covered in the user guide.
Before you start
Every example expects the dvp-examples namespace, a virtual machine labeled role: example and a Secret with SSH credentials. Prepare those objects before running the examples.
Connectivity check with an inline playbook
The simplest scenario consists of one machine selected by a label and a playbook with a single task. It is recommended to start with this scenario, because it exercises the whole path from the creation of a run to the recording of the result in its status.
Playbook from a ConfigMap
A playbook longer than a few dozen lines, or one used by more than a single run, should be stored in a ConfigMap resource. In that case the run points at a key of such a resource instead of carrying the playbook text.
Selecting several targets
The targets of a run are given either by a label expression or by a list of names. The two ways cannot be combined, and a manifest with an empty selector is refused by the API server. A run is executed once, so its targets have to be stated explicitly.
Debugging a run
The runner block changes how the ansible-playbook utility is executed and does not affect which tasks run. The diff parameter shows what exactly changed in a file, the verbosity parameter sets how much the log holds, and the dryRun parameter performs a check run without changes on the hosts.
Groups and host variables in annotations
Ansible groups and facts about a machine are set on the machine itself, in two annotations that the controller lays out into the inventory file. Thanks to that, a playbook gets the web group and an app_role variable independently of a particular run.
Installing a package with privilege escalation
Tasks that change the system need the become: true parameter. The password for privilege escalation is taken by the controller from the become-password key of the connection Secret. The package manager is chosen from the facts Ansible gathers.
Idempotent configuration of a service
Configuring a service consists of a config template and a handler that restarts the service only when the file has changed. Executing such a run again changes no configuration and restarts nothing.
Collecting facts about machines
An inventory or a capacity review uses a playbook that changes nothing and only gathers facts and prints them. The result stays in the logs of the runner Pod, so it is worth collecting into a log store.
Skipped machines in the status
A machine that matched the target but cannot be configured when the run starts does not fail the run. Such a machine lands in the status.skippedHosts list with a reason, and the playbook is executed on the remaining machines.
Encrypted values in Ansible Vault
A project with files encrypted by Ansible Vault is executed unchanged. The password is stored in the connection Secret under the ansible-vault-password key, and the run hands it to the ansible-playbook utility as a password file.
Playbook project from a public Git repository
A real Ansible project is a tree of directories with roles/, group_vars/ and templates. The Git source fetches the repository whole, so roles and files next to the playbook are executed unchanged, and the declared Galaxy dependencies are installed before the playbook starts.
Private Git repository
The credentials of a repository are stored in a Secret of their own and are mounted only for the fetch step, so the tasks of the playbook never see them. For addresses of the ssh:// form such a Secret is mandatory, and host keys are checked strictly.
Run in an additional network
When the SSH server in a guest OS listens only in an additional network of the sdn module, the address of the machine alone is not enough, because there is no route from the pod network into another L2 domain. The connection.network field puts the run itself into that network.
Troubleshooting a failed run
A failed run is of one of two kinds, and they are investigated differently. The PlaybookFailed phase means the playbook ran and its tasks failed, while the Error phase means the tasks were never reached. Errors the playbook handled itself leave the run successful.
Scheduled run
A schedule creates runs by cron the way a CronJob resource creates Job objects. A run is named after its schedule plus the time of the tick.
Concurrency and history of a schedule
Four fields of a schedule decide what happens when a run does not finish before the next tick, how late a missed tick still makes sense, and how many runs to keep in the namespace.
Hosts named by address
The addresses of some hosts are unknown to the platform. Such hosts are machines whose address is configured inside a guest OS by hand or handed out by an external DHCP server, as well as physical servers and network appliances. Those hosts are named by address in the manifest of the run.
Targets of type Hosts are available in commercial editions only. In the Community Edition a run works with virtual machines of the platform, and a manifest with targets of type Hosts is refused by the cluster when it is created.
Variables of a run
Variables hand values to a playbook and make changes to the playbook unnecessary, so one project serves several environments. A value written in the manifest can be a string, a number, a list or a mapping.
Values from a Secret and a ConfigMap
A password or a ready-made set of values does not have to be copied into the manifest of a run. The valueFrom field takes the value from a key of a Secret or a ConfigMap, and the varsFiles field attaches a whole YAML document with the types of the values intact.
Variable precedence
One variable name can be set in four places: an annotation on the machine, the playbook, a varsFiles document and the vars field of a run. The variables of a run take precedence over every value the playbook and its project set.
Refused variable names
Some variable names a run does not accept. Those are the names the module sets itself and the Ansible magic variables. The former already have their own fields in the API, and the values of the latter Ansible fills in itself.