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 Automation works. Ansible ad hoc commands enable you to perform repeatable tasks on local or 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.

Quick example:

Let’s deploy an application on your local node that can assist you with the workshop. Using ad hoc commands, we will download a configuration file from a remote git server (gitOps), set up a system service, and start the service.

The service will be a Visual Studio Code container, defined in a Kubernetes POD YAML, launched via podman by systemd.

The systemd configuration file exists in SCM; so, we need to pull it to the local host. Since the file is available via a URL, we will use the Ansible uri module to retrieve the file.

The ansible ad hoc command will start with ansible, then the target, declaring the uri module via the --module switch, the arguments --args to the module, followed by the --become switch telling Ansible to become a superuser.

We have configured 'sudo' for you; however, Ansible can use lots of other methods to become (-b) the superuser.
ansible localhost --module-name uri --args "url=https://raw.githubusercontent.com/RedHatGov/redhatgov.workshops/master/ansible_tower_aws/files/code-server.service return_content=yes dest=/etc/systemd/system" --become

Now in the /etc/systemd/system directory lives the code-server.service file you downloaded from the SCM system. In order to apply it, we would typically utilize the systemctl command; however, this is an Ansible workshop. With that being said, we will apply the configuration file with a module.

ansible localhost --module-name service --args "name=code-server state=started enabled=yes" --become

It is possible now to see your service running by doing the following commands:

systemctl status code-server.service

Additionally, you can see more details about the pod deployment:

podman pod list

Connect to the web based editor in a browser window, using the following URL:

https://example.tower.0.redhatgov.io:8443

Managing remote hosts

First, in order to manage remote systems - we need to set up our 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)
vim hosts

Put the following content into the file:

[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 --module-name 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 --module-name command --args "uptime" --one-line

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 --module-name setup
Like many Linux commands, ansible allows for long-form and short-form command-line arguments. For example:
ansible web --module-name command --args "uptime" --one-line --become

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

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

We are going to be using the short-form options, going forward. Please ask your instructor if any of them are unclear!

Step 4: Install Node.js

Now, let’s install Node.js, using the dnf module.

ansible web -m dnf -a "name=nodejs state=present" -b

Step 5: Let’s copy a file to the managed node.

ansible web -m copy -a "src=hello.js dest=hello.js"

Step 6: Copy configuration file to priviledged location

ansible web -m copy -a "src=hello.service dest=/etc/systemd/system/hello.service" -b

Step 6: Deploy an application

Next, we will deploy an application. We first enable the application service, then open the port in the firewall, and last activate the change to the firewall:

ansible web -m service -a "name=hello state=started enabled=yes" -b
ansible web -m firewalld -a "port=8080/tcp permanent=yes state=enabled" -b
ansible web -m systemd -a "name=firewalld state=reloaded" -b

Demo site (unit test):

ansible web -m uri -a "url=http://localhost:8080 return_content=yes"
ansible localhost -m uri -a "url=http://example.node.0.redhatgov.io:8080 return_content=yes"

Demo site (open this in your web browser):

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

Step 7: Cleanup

Finally, let’s clean up after ourselves. First, stop the node service, using the following command.

ansible web -m service -a "name=hello state=stopped enabled=no" -b

Step 8: Remove package

Next, remove the nodejs package, as shown, below:

ansible web -m dnf -a "name=nodejs state=absent" -b

Step 9: Clean files

Last, let’s remove the web content and service definition:

ansible web -m file -a "path=/home/ec2-user/hello.js state=absent"
ansible web -m file -a "path=/etc/systemd/system/hello.service state=absent" -b

Workshop Details

Domain Red Hat Logo
Workshop
Student ID

Return to Workshop