- Lab
- A Cloud Guru
Working with Ansible Roles
Working with Ansible roles is a key concept covered on the Red Hat Certified Ansible Specialist Exam (EX407). This should not be a surprise, considering how much functionality roles provide. This exercise covers how to create a role and how to use roles within a playbook. In order to complete this exercise, you will need to have basic proficiency with several common Ansible modules and using Ansible playbooks. After completing this learning activity, you will better understand how to work with Ansible roles. *This course is not approved or sponsored by Red Hat.*
Path Info
Table of Contents
-
Challenge
Create a Role Called baseline in /etc/ansible/roles
Run the following commands to create the structure needed for the role:
sudo mkdir /etc/ansible/roles/baseline && sudo chown ansible.ansible /etc/ansible/roles/baseline
mkdir /etc/ansible/roles/baseline/{templates,tasks,files}
echo "---" > /etc/ansible/roles/baseline/tasks/main.yml
-
Challenge
Configure the Role to Deploy the /etc/motd Template
-
cp /home/ansible/resources/motd.j2 /etc/ansible/roles/baseline/templates
-
Create a file called
/etc/ansible/roles/baseline/tasks/deploy_motd.yml
with the following content:--- - template: src: motd.j2 dest: /etc/motd
-
Edit
/etc/ansible/roles/baseline/tasks/main.yml
to include the following lines at the bottom of the file:- name: configure motd import_tasks: deploy_motd.yml
-
-
Challenge
Configure the Role to Install the Latest Nagios Client
-
Create a file called
/etc/ansible/roles/baseline/tasks/deploy_nagios.yml
with the following content:--- - yum: name=nrpe state=latest
-
Edit
/etc/ansible/roles/baseline/tasks/main.yml
to include the following lines at the bottom of the file (take care with the formatting.):- name: deploy nagios client import_tasks: deploy_nagios.yml
-
-
Challenge
Configure the Role to Add an Entry to /etc/hosts for the Nagios Server
-
Create a file called /etc/ansible/roles/baseline/tasks/edit_hosts.yml with the following content, substituting <<PROVIDED>PROVIDED> with the IP specified in /home/ansible/resources/nagios_info.txt:
--- - lineinfile: line: "<<PROVIDED>PROVIDED> nagios.example.com" path: /etc/hosts
-
Edit /etc/ansible/roles/baseline/tasks/main.yml to include the following lines at the bottom of the file:
- name: edit hosts file import_tasks: edit_hosts.yml
-
-
Challenge
Configure the Role to Create the noc User and Deploy the Provided Public Key for the noc User on Target Systems
-
Copy the file
/home/ansible/resources/authorized_keys
to/etc/ansible/roles/baseline/files/
. -
Create a file called
/etc/ansible/roles/baseline/tasks/deploy_noc_user.yml
with the following content:--- - user: name=noc - file: state: directory path: /home/noc/.ssh mode: 0600 owner: noc group: noc - copy: src: authorized_keys dest: /home/noc/.ssh/authorized_keys mode: 0600 owner: noc group: noc
-
Edit
/etc/ansible/roles/baseline/tasks/main.yml
to include the following lines at the bottom of the file:- name: set up noc user and key import_tasks: deploy_noc_user.yml
-
-
Challenge
Edit web.yml to Deploy the baseline Role
Edit /home/ansible/resources/web.yml to the following:
--- - hosts: webservers become: yes roles: - baseline tasks: - name: install httpd yum: name=httpd state=latest - name: start and enable httpd service: name=httpd state=started enabled=yes
-
Challenge
Run Your Playbook Using the Default Inventory
Run
ansible-playbook /home/ansible/resources/web.yml
.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.