Skip to main content

Ad-Hoc Ansible Shell Scripts

Hands-On Lab

 

Length

00:30:00

Difficulty

Intermediate

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.<p> 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.<p> In this lab we will create a bash script and use it to perform Ansible Ad-hoc tasks.<p> Note: Ansible has been setup and configured for use.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Ad-Hoc Ansible Shell Scripts

Introduction

One of the keys to success with Ansible is being able to run ad-hoc commands. 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 ad-hoc Ansible tasks.

Note: Ansible has been setup and configured for use.

Instructions

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.

Get Logged In

Login credentials are all on the lab overview page. Once we're logged into the control1 server, become the ansible user (su - ansible) and we can get going.

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

Let's look at our hosts file:

cat /etc/hosts

We should see node1 and node2 in there, each with a private IP address. And we should be able to log in (with SSH) to either of them without a password.

Back in control1 as ansible, 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.

Here is the script:

#!/bin/bash

# Command line variable is $1

if [ -n &quot;$1&quot; ]; then
  echo &quot;Package to install is $1&quot;
else
  echo &quot;Package to install not supplied.&quot;
  exit
fi

ansible all -b -m yum -a &quot;name=$1 state=present&quot;

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. Let's make it executable (chmod +x install-package.sh), the run it (./install-package.sh). Since we didn't supply any package names, it should just dump us back out to the Bash prompt.

Use the Script to Install ELinks on the node1 and node2 Servers

Now let's run it again, with a package name:

./install-package.sh elinks

We should see ELinks get installed on both nodes. Let's check to be sure though...

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. Test on each node by running elinks http://google.com. It should work on both of them.

Conclusion

Well, we've reached the end of the lab. We have a working script now that installs software on nodes, using ad-hoc Ansible commands on a control node. Congratulations!