Skip to main content

Understand Core Components of Ansible – Playbooks

Hands-On Lab

 

Photo of Rob Marti

Rob Marti

Linux Training Architect I in Content

Length

00:30:00

Difficulty

Intermediate

Ansible is all about automation and making an administrator's life easier. So rather than chaining ad hoc commands together, you can set up a Playbook to run multiple commands at once. This hands-on lab will help enhance your understanding of building a playbook.

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 - Playbooks

Introduction

Ansible is all about automation and making an administrator's life easier. So rather than chaining ad hoc commands together, you can set up a Playbook to run multiple commands at once. This hands-on lab will help enhance your understanding of building a playbook.

The Scenario

We're in the midst of a proof of concept project. Now, we've been tasked with writing a playbook that will set our machine up as a web server, with the normal software that our company's web servers typically have running on them. The playbook must install the following software:

httpd
git
tcpdump
php

Then we need to set up a second playbook that creates the following users:

security
devs
admins

All of those users should be members of the web group.

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 and Execute the Playbook to Install Software

Using whatever text editor is most comfortable, create software.yml in the /root directory (we'll use vim here):

vim software.yml

The playbook should look something like this when we're done:

---
# Software playbook
#

- name: Install required software
  hosts: all

  tasks:
   - yum:
      name: "{{ packages }}"
      state: present
     vars:
       packages:
        - httpd
        - git
        - tcpdump
        - php

   - service:
      name: httpd
      state: started
      enabled: yes

Now we can run the playbook with ansible-playbook software.yml --become. Alternatively, we could have stuck a become: yes line below the hosts line, and skipped having to put it in the command. If we do that though, we need to remember this is YAML, and very finicky about spacing. The become line needs to have the exact indentation that the hosts line does.

After a quick authenticity prompt, the playbook will run. We can see all of the software install and fire up. If we run the command again, we'll just get a bunch of ok: messages, because there's nothing to do this time around.

Write and Execute the Playbook to Add and Configure the Required Users

Now let's make a second playbook, the one that will create the users. Create users.yml in the /root directory ( again we'll use vim here):

vim users.yml

Your playbook should look something like this:

---
# Create required users

- name: Create required users
  hosts: all
  vars:
   usernames:  
    - devs
    - security
    - admins

  tasks:
   - group:
      name: web
      state: present

   - user:
      name: "{{ item }}"
      state: present
      groups: web
      append: yes
     with_items: "{{ usernames }}"

Just like with the last playbook, we'll run it with an ansible-playbook command:

ansible-playbook users.yml --become

Our output should show things that happened. We can check with id admins (which will print out a list of all the groups that admins is a member of). We can check the other users the same way.

Conclusion

We've successfully created a couple of playbooks. One installs software, and the other creates some users and adds them to groups. We are done. Congratulations!