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 dnf 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.dnf:
        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.

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.


Navigation

Previous Exercise - Next Exercise




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