Read this in other languages:
English, 日本語, Portugues do Brasil, Française, Español.
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.
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.
First, create a text file in YAML format for your playbook. Remember:
---
).Key Concepts:
hosts
: Specifies the target servers or devices for your playbook to run against.tasks
: The actions Ansible will perform.become
: Allows privilege escalation (running tasks with elevated privileges).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.
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.
About the dnf
module: This module is used for package management with DNF (Dandified YUM) on RHEL and other Fedora-based systems.
About the user
module: This module is used to manage user accounts.
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.
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