Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
    • Security
Google Cloud Platform icon
Labs

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.*

Google Cloud Platform icon
Lab platform
Lab Info
Level
Advanced
Last updated
Sep 22, 2025
Duration
1h 30m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. 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
  2. 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
      
  3. 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
      
  4. 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
      
  5. 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
      
  6. 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
    
  7. Challenge

    Run Your Playbook Using the Default Inventory

    Run ansible-playbook /home/ansible/resources/web.yml.

About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight