Hands-on Lab·Ansible
Running Ansible from Shell Scripts
"Script administration tasks" is an EX294 objective in its own right: shell scripts that run ad hoc commands or playbooks. The scripting is straightforward; the interesting part is the exit codes, because one of the most dangerous outcomes in Ansible - a pattern that matched no hosts - exits zero.
Scale and Debugging Guide 40 of 45 Intermediate
- OSUbuntu 26.04 LTS
- ansible-core2.20.1
- Python3.14.4
- TimeAbout 16 min
- Reviewed23 August 2026
Written against the versions above. `ansible-playbook` exit codes: **0** success, **1** an error such as a missing playbook, **2** one or more hosts failed, **3** all hosts failed, **4** all hosts unreachable, **99** interrupted. The one to be careful about is that a run which matched no hosts at all is a **success**.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| ANS-CTL01 | 192.168.0.36 | Ubuntu 26.04 LTS | Ansible Control Node | 2 Core | 3 GB | 50 GB |
| ANS-A01 | 192.168.0.37 | Ubuntu 26.04 LTS | Managed Node (group: web) | 2 Core | 3 GB | 50 GB |
| ANS-B01 | 192.168.0.38 | Ubuntu 26.04 LTS | Managed Node (group: db) | 2 Core | 3 GB | 50 GB |
Before you start
- A control node configured as shown in the first step.
- The session writes two scripts to the home directory, runs them, and removes them.
-
The configuration this guide assumes
-
The exit codes Ansible actually returns
-
Playbook exit codes
-
A script that wraps an ad-hoc command
-
A script that runs a playbook with arguments
-
Making the output machine-readable