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 package
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.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.
About the package
module: This modules manages packages on a target without specifying a package manager module
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.
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
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