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

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

Scripts run on the control node against both managed nodes.
Server NameIP AddressOSRolesCPURAMHDD
ANS-CTL01192.168.0.36Ubuntu 26.04 LTSAnsible Control Node2 Core3 GB50 GB
ANS-A01192.168.0.37Ubuntu 26.04 LTSManaged Node (group: web)2 Core3 GB50 GB
ANS-B01192.168.0.38Ubuntu 26.04 LTSManaged Node (group: db)2 Core3 GB50 GB

Before you start

  1. The configuration this guide assumes

  2. The exit codes Ansible actually returns

  3. Playbook exit codes

  4. A script that wraps an ad-hoc command

  5. A script that runs a playbook with arguments

  6. Making the output machine-readable

Official sources