Skip to main content

Deploying Services Using Ansible

Hands-On Lab

 

Photo of Stosh Oldham

Stosh Oldham

Course Development Director in Content

Length

01:30:00

Difficulty

Advanced

Ansible is frequently used to manage software configuration and deployment. This exercise allows students to deploy and configure an NFS server using key features of Ansible.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Deploying Services Using Ansible

Introduction

Ansible is frequently used to manage software configuration and deployment. This exercise allows students to deploy and configure an NFS server using key features of Ansible.

The team has been working on automating the builds for common services using Ansible. You have been tasked with creating the build for a common NFS server. Write a playbook that satisfies the following requirements:

On nfs:

  • Make sure nfs-utils is installed.

  • Configure /etc/exports via an Ansible template stored in /home/ansible/exports.j2. Deploy the template so that /mnt/nfsroot is exported with read and write to all hosts.

    • Note: Your template file should contain the following with the variable {{ share_path }} being defined within your playbook:

      {{ share_path }} *(rw)
    • Note: The file /etc/exports on nfs should have the following content once deployed:

      /mnt/nfsroot *(rw)
  • Create a handler that runs the command exportfs -a if the file /etc/exports is modified in a playbook task.

  • You can assume all necessary firewall rules have been deployed.

On remote:

  • Configure /etc/hosts from a template file stored on control at /home/ansible/etc.hosts.j2 with the following entries:

    127.0.0.1 localhost, {{ ansible_hostname }}
    {{ nfs_ip }}  {{ nfs_hostname }}
    • Note: You should populate the variables {{ nfs_ip }} and {{ nfs_hostname }} using magic variables in your playbook.
  • Create users from file stored on control at /home/ansible/user-list.txt only if the remote host has the file /opt/user-agreement.txt.

The Ansible control node has been configured for you and all other servers have already been configured for use with Ansible. The default inventory has been configured to include the group remote and server nfs.

Solution

Begin in by logging in to the Ansible control node using the credentials on the hands-on lab page:

ssh cloud_user@IP_ADDRESS

Become the ansible user:

sudo -i -u ansible

Create the necessary template files on the Ansible control node

  1. Create the files exports.j2 in /home/ansible with the noted content:

    vim /home/ansible/exports.j2

    Provide the following content for this file:

    {{ share_path }} *(rw)
  2. Create the file etc.hosts.j2 in /home/ansible with the noted content:

    vim /home/ansible/etc.hosts.j2

    Provide the following content for this file:

    127.0.0.1   localhost {{ ansible_hostname}}
    {{ nfs_ip }}    {{ nfs_hostname }}

Create a playbook for the server 'nfs' in the Ansible inventory

Create a playbook in /home/ansible/nfs.yml that meets the provided specification for the server nfs in the ansible inventory.

  1. Create a playbook in /home/ansible/nfs.yml with the following content:

    vim /home/ansible/nfs.yml
    - hosts: nfs
      become: yes
      vars:
        share_path: /mnt/nfsroot
      tasks:
        - name: install nfs
          yum:
            name: nfs-utils
            state: latest
        - name: start and enable nfs-server
          service:
            name: nfs-server
            state: started
            enabled: yes
        - name: configure exports
          template:
            src: /home/ansible/exports.j2
            dest: /etc/exports
          notify: update nfs
      handlers:
        - name: update nfs exports
          command: exportfs -a
          listen: update nfs

Add a play for the remote host group

Edit /home/ansible/nfs.yml such that changes are executed on the remote host group to meet the provided requirements.

  1. Edit /home/ansible/nfs.yml to contain the following content after the nfs play:

    vim /home/ansible/nfs.yml
      - hosts: remote
        become: yes
        vars:
          nfs_ip: "{{ hostvars['nfs']['ansible_default_ipv4']['address'] }}"
          nfs_hostname: "{{ hostvars['nfs']['ansible_hostname'] }}"
        vars_files:
          - /home/ansible/user-list.txt
        tasks:
          - name: configure hostsfile
            template:
              src: /home/ansible/etc.hosts.j2
              dest: /etc/hosts.nfslab
          - name: get file status
            stat:
              path: /opt/user-agreement.txt
            register: filestat
          - name: debug info
            debug:
              var: filestat
          - name: create users
            user:
              name: "{{ item }}"
            when:  filestat.stat.exists
            loop: "{{ users }}"

Execute playbook to verify your playbook works correctly

Execute playbook /home/ansible/nfs.yml to verify your playbook works correctly.

  1. Run ansible-playbook /home/ansible/nfs.yml from the control node.

Conclusion

Congratulations, you've completed this hands-on lab!