Workshop Exercise - Writing Your First Playbook

Read this in other languages:
uk English, japan日本語, brazil Portugues do Brasil, france Française,Español Español.

Table of Contents

Objective

In this exercise, you’ll use Ansible to conduct basic system setup tasks on a Red Hat Enterprise Linux server. You will become familiar with fundamental Ansible modules like package and user, and learn how to create and run playbooks.

Guide

Playbooks in Ansible are essentially scripts written in YAML format. They are used to define the tasks and configurations that Ansible will apply to your servers.

Step 1 - Playbook Basics

First, create a text file in YAML format for your playbook. Remember:

Key Concepts:

NOTE: An Ansible playbook is designed to be idempotent, meaning if you run it multiple times on the same hosts, it ensures the desired state without making redundant changes.

Step 2 - Creating Your Playbook

Before creating your first playbook, ensure you are in the correct directory by changing to ~/lab_inventory:

cd ~/lab_inventory

Now create a playbook named system_setup.yml to perform basic system setup:

The basic structure looks as follows:

---
- name: Basic System Setup
  hosts: node1
  become: true
  tasks:
    - name: Update all security-related packages
      ansible.builtin.package:
        name: '*'
        state: latest
        security: true
   
    - name: Create a new user
      ansible.builtin.user:
        name: myuser
        state: present
        create_home: true

NOTE: Updating the packages may take a few minutes prior to the Ansible playbook completing.

Step 3 - Running the Playbook

Execute your playbook using the ansible-navigator command:

[student@ansible-1 lab_inventory]$ ansible-navigator run system_setup.yml -m stdout

Review the output to ensure each task is completed successfully.


PLAY [Basic System Setup] ******************************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [Update all security-related packages] ************************************
changed: [node1]

TASK [Create a new user] *******************************************************
changed: [node1]

PLAY RECAP *********************************************************************
node1                      : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Step 4 - Checking the Playbook

Now, let’s create a second playbook for post-configuration checks, named system_checks.yml:

---
- name: System Configuration Checks
  hosts: node1
  become: true
  tasks:
    - name: Check user existence
      ansible.builtin.command:
        cmd: id myuser
      register: user_check
 
    - name: Report user status
      ansible.builtin.debug:
        msg: "User 'myuser' exists."
      when: user_check.rc == 0

Run the checks playbook:

[student@ansible-1 lab_inventory]$ ansible-navigator run system_checks.yml -m stdout

Review the output to ensure the user creation was successful.


PLAY [System Configuration Checks] *********************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [Check user existence] ****************************************************
changed: [node1]

TASK [Report user status] ******************************************************
ok: [node1] => {
    "msg": "User 'myuser' exists."
}

PLAY RECAP *********************************************************************
node1                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

Navigation

Previous Exercise - Next Exercise




Click here to return to the Ansible for Red Hat Enterprise Linux Workshop