Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
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
Labs

Path Info

Level
Clock icon Advanced
Duration
Clock icon 1h 30m
Published
Clock icon Jul 11, 2018

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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

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.

Start learning by doing today

View Plans