Ad-Hoc Ansible Shell Scripts

30 minutes
  • 5 Learning Objectives

About this Hands-on Lab

One of the keys to success with Ansible is being able to run `ad-hoc` commands and in this particular exercise we will make use of scripting and ad-hoc commands to perform tasks.

The value of `ad-hoc` commands in scripts is underscored by the fact that it is an objective of the *Red Hat Certified Ansible Specialist* exam.
In this lab we will create a bash script and use it to perform Ansible Ad-hoc tasks.

Note: Ansible has been setup and configured for use.

*This course is not approved or sponsored by Red Hat.*

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Sign onto the Ansible Control Node Server as cloud_user and Change to the ansible User, Then Test Ansible Has Been Set Up

Sign into the server called Ansible Control Node as cloud_user and change to the ansible user via the su - ansible command.

Test that Ansible is working, with an ad-hoc command. One possible command to use is:

ansible all -m ping
Create a Bash Script Called install-package.sh That Accepts a Parameter and Uses an Ad-Hoc Ansible Command to Install Software

Using Bash, create a script that accepts a variable on the command line and uses an ad-hoc Ansible command to install software on the node1 and node2 servers.

The script should exit if no program to install has been typed in the command line.

Test the Script to Ensure It Will Exit If a Command Line Argument Has Not Been Entered

If no command line argument has been entered, the script should exit.

Make Sure the Script is Executable and Use it to Install Elinks on the node1 and node2 Servers

Ensure the script is executable, and run it to install ELinks on the nodes.

Log into node1 and node2 and Ensure the ELinks Package Has Been Installed

As the ansible user, log into the node1 and node2 servers, and ensure that ELinks has been installed.

Additional Resources

We have been tasked with creating a Bash script that will make use of an ad-hoc Ansible command to install software on our Ansible servers.

The purpose of the script is so that a user who has no experience with Ansible can just run it and install software as needed.

To summarize, we must do the following:

  1. Create a Bash script to use an ad-hoc Ansible command to install software.
  2. Make sure the script accepts a command line variable with the software to install.
  3. If no software to install is included on the command line, the script must exit and alert the user.

Important notes:

  • Ansible is already on the control node. If we connect to the server by clicking on the Public IP address in a web browser, we need to make sure we change to the ansible user, with the su - ansible command.
  • The user ansible is present on all servers with appropriate shared keys for access to managed servers from the control node. We need to make sure we use this user to complete the commands.
  • The ansible user has the same password as cloud_user.
  • The default Ansible inventory has already been configured with the appropriate hosts and groups.
  • /etc/hosts entries are present on the control1 host for the managed servers.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?