Exercise 1.0 - Running Ad hoc commands

Return to Workshop

Exercise Description

For our first exercise, we are going to run some ad hoc commands to help you get a feel for how Red Hat Ansible Tower works. Ansible ad hoc commands enable you to perform repeatable tasks on remote nodes, without having to write a playbook. They are very useful when you simply need to do one or two tasks quickly and often, to many remote nodes.

Set Up Dependencies:

Define your inventory. Inventories are crucial to Ansible, as they define remote machines on which you will run commands or your playbook(s). Use vi or vim to create a file called hosts. Then, add the appropriate definitions for the node that will function as a web node.

The workshopname, in the example below, will be provided to you by your instructor. The # should be replaced by your student number.

For example, a recent workshop student used: example.tower.0.redhatgov.io

hosts (example)
[web]
example.node.0.redhatgov.io

Step 1: Test webhost response

Let’s start with something basic - pinging a host. The ping module tests the responsiveness of our web host.

ansible web -m ping

Step 2: Run a Linux command

Now let’s see how we can run a Linux command and format the output, using the command module.

ansible web -m command -a "uptime" -o

Step 3: Review the setup module

Take a look at your web node’s configuration. The setup module displays Ansible facts (and a lot of them) about an endpoint.

ansible web -m setup

Step 4: Run an old version of a package

Let’s purposely install an out-of-date version of subversion, a revision control tool. First, find the most recent out-of-date version of the package that is available:

sudo yum list available subversion.x86_64 --showduplicates

You should get a number of lines of output, that look something like this (though the versions will vary):

Loaded plugins: amazon-id, rhui-lb, search-disabled-repos
Available Packages
subversion.x86_64       1.7.14-6.el7            rhui-REGION-rhel-server-releases
subversion.x86_64       1.7.14-7.el7_0          rhui-REGION-rhel-server-releases
subversion.x86_64       1.7.14-7.el7_1.1        rhui-REGION-rhel-server-releases
subversion.x86_64       1.7.14-10.el7           rhui-REGION-rhel-server-releases
subversion.x86_64       1.7.14-11.el7_4         rhui-REGION-rhel-server-releases
subversion.x86_64       1.7.14-14.el7           rhui-REGION-rhel-server-releases

Now, let’s install the version mentioned in the second-to-last line:

ansible web -m package -a "name=subversion-1.7.14-11.el7_4 state=present" -b
You can use that method, referring to a package with <package name>-<version> to install any version of any package.

Since that’s done, let’s now update Subversion to the current version:

ansible web -m package -a "name=subversion state=latest" -b
Package Updating Methods

There are various methods that you can use to update packages. You saw a simple case, already, but you can also update all packages to the current version with:

ansible web -m package -a "name=* state=latest" -b

Or, you can install updates only to those packages with security implications. Red Hat identifies an update as 'security-relevant', when the update’s package changes, affect system integrity. To use this mode, use the following command:

ansible web -m yum -a "name=* security=yes state=latest" -b

Step 5: Install Maven and Git

Now, let’s install Maven & Git, using the package module.

ansible web -m package -a "name=rh-maven35 state=present" -b
ansible web -m package -a "name=git state=present" -b

Step 6: Install a group of packages

It is even possible to install groups of packages (aka yum groupinstall); such as Red Hat JBoss Enterprise Application Platform (EAP).

ansible web -m package -a "name=@jboss-eap7 state=present" -b

Step 7: Start JBoss EAP

JBoss EAP is installed now, so let’s start it up using the service module.

ansible web -m service -a "name=eap7-standalone state=started" -b

Step 8: Deploy an application

Next, we will deploy an application.

For simplicity sake, we will use an existing quickstart. To get started, we checkout some code …​

ansible web -m git -a "repo=https://github.com/jboss-developer/jboss-eap-quickstarts.git dest=/tmp/checkout"

…​and deploy the code via Maven, provided by Red Hat Software Collections.

ansible web -m shell -a "scl enable rh-maven35 'mvn clean install wildfly:deploy -Dmaven.test.skip=true' chdir=/tmp/checkout/helloworld" -b

Demo site (unit test):

ansible web -m uri -a "url=http://localhost:8080/helloworld/ return_content=yes"

Demo site (open this in your web browser):

http://example.node.0.redhatgov.io:8080/helloworld/

Step 9: Cleanup

Finally, let’s clean up after ourselves. First, stop the Red Hat JBoss EAP service, using the following command.

ansible web -m service -a "name=eap7-standalone state=stopped" -b

Step 10: Remove packages

Next, remove some packages - as follows.

ansible web -m package -a "name=@jboss-eap7 state=absent" -b
ansible web -m package -a "name=eap7-* state=absent" -b
ansible web -m package -a "name=rh-maven35 state=absent" -b
ansible web -m package -a "name=git state=absent" -b
Like many Linux commands, Ansible allows for long-form options, as well as short-form. For example:
ansible web --module-name ping

The command above is the same as running the command below.

ansible web -m ping

We are going to be using the short-form options throughout this workshop


Workshop Details

Domain
Workshop
Student ID

Return to Workshop