Skip to main content

Understand Core Components of Ansible – Variables

Hands-On Lab

 

Photo of Rob Marti

Rob Marti

Linux Training Architect I in Content

Length

00:15:00

Difficulty

Intermediate

Variables are another thing that makes Ansible powerful. By being able to write generic commands, with decision trees based on variables, we can automate almost anything. This lab will walk through a couple of things we can do with variables to help burn in the concept.

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.

Understand Core Components of Ansible - Variables

Introduction

Variables are another thing that makes Ansible powerful. By being able to write generic commands, with decision trees based on variables, we can automate almost anything. This lab will walk through a couple of things we can do with variables to help burn in the concept.

The Scenario

We're in the midst of a proof of concept project. Now we've got to make sure, on our existing setup, that tcpdump is only installed on servers with admin in their hostnames. And we want Git installed, but only on servers running RedHat. We can do it using Ansible variables.

Logging In

Use the credentials provided on the hands-on lab page to get into Server1 to begin with. Since we need root privileges, let's just run sudo -i right off and become root.

Write a Playbook That Removes tcpdump When a Server's Name Does Not Contain admin

Let's verify whether or not tcpdump is installed:

rpm -q tcpdump

Looks like it is. So now let's create the playbook (we'll use vim here, but any text editor is fine):

vim variables.yml

When we're done, the playbook should look something like this:

---
# Variables playbook

- name: This playbook will remove tcpdump (if installed) from servers without admin in their hostnames
  hosts: all
  become: yes
  tasks:
   - name: Remove tcpdump from all but admin servers
     yum:
      name: tcpdump
      state: absent
     when: "'admin' not in inventory_hostname"

Now we can run this playbook with:

ansible-playbook variables.yml

After an initial authenticity prompt, things should start moving. In the output, we'll see tcpdump get removed, because the hostname of the server (as it sits in the inventory) we're running it against doesn't contain admin.

Modify That Playbook to Install Git on RedHat Servers

Now let's get back into our playbook (vim variables.yml) and add the code that will install Git on our RedHat-based servers. Your additional lines should look something like this:

   - name: Make sure git is installed only on Red Hat servers
     yum:
      name: git
      state: present
     when: ansible_facts['os_family'] == 'RedHat'

Before we run the playbook though, let's see if Git is installed:

rpm -q git

It is installed. Let's get rid of it, then run the playbook and see what happens:

yum remove git -y
ansible-playbook variables.yml

In the output, we'll see that something changed. Let's run rpm -q git again, and we'll see that the playbook installed the software correctly.

Conclusion

We were tasked with removing tcpdump from any server whose name did not include admin, and installing Git on any server that was running RedHat. We have a playbook that does both. We're finished. Congratulations!