Bookmark this page

Guided Exercise: Deploying Custom Files with Jinja2 Templates

In this exercise, you will create a simple template file that your playbook will use to install a customized Message of the Day file on each managed host.

Outcomes

You should be able to:

  • Build a template file.

  • Use the template file in a playbook.

Log in to workstation as student using student as the password.

On workstation, run the lab file-template start command. This script ensures that Ansible is installed on workstation, creates the /home/student/file-template directory, and downloads the ansible.cfg file into that directory.

[student@workstation ~]$ lab file-template start

Note

All the files used during this exercise are available for reference on workstation in the /home/student/file-template/files directory.

Procedure 5.2. Instructions

  1. On workstation, navigate to the /home/student/file-template working directory. Review the inventory file in the current working directory. This file configures two groups: webservers and workstations. The servera.lab.example.com system is in the webservers group, and the workstation.lab.example.com system is in the workstations group.

    1. Navigate to the /home/student/file-template working directory.

      [student@workstation ~]$ cd ~/file-template
      [student@workstation file-template]$
    2. Display the content of the inventory file.

      [webservers]
      servera.lab.example.com
      
      [workstations]
      workstation.lab.example.com
  2. Create a template for the Message of the Day and include it in the motd.j2 file in the current working directory. Include the following variables and facts in the template:

    • ansible_facts['fqdn'], to insert the FQDN of the managed host.

    • ansible_facts['distribution'] and ansible_facts['distribution_version'], to provide distribution information.

    • system_owner, for the system owner's email. This variable needs to be defined with an appropriate value in the vars section of the playbook template.

    This is the system {{ ansible_facts['fqdn'] }}.
    This is a {{ ansible_facts['distribution'] }} version {{ ansible_facts['distribution_version'] }} system.
    Only use this system with permission.
    Please report issues to: {{ system_owner }}.
  3. Create a playbook file named motd.yml in the current working directory. Define the system_owner variable in the vars section, and include a task for the template module that maps the motd.j2 Jinja2 template to the remote file /etc/motd on the managed hosts. Set the owner and group to root, and the mode to 0644.

    ---
    - name: configure SOE
      hosts: all
      remote_user: devops
      become: true
      vars:
        - system_owner: clyde@example.com
      tasks:
        - name: configure /etc/motd
          template:
            src: motd.j2
            dest: /etc/motd
            owner: root
            group: root
            mode: 0644
  4. Before running the playbook, use the ansible-playbook --syntax-check command to verify the syntax. If it reports any errors, correct them before moving to the next step. You should see output similar to the following:

    [student@workstation file-template]$ ansible-playbook --syntax-check motd.yml
    
    playbook: motd.yml
  5. Run the motd.yml playbook.

    [student@workstation file-template]$ ansible-playbook motd.yml
    PLAY [all] ******************************************************************
    
    TASK [Gathering Facts] ******************************************************
    ok: [servera.lab.example.com]
    ok: [workstation.lab.example.com]
    
    TASK [template] *************************************************************
    changed: [servera.lab.example.com]
    changed: [workstation.lab.example.com]
    
    PLAY RECAP ******************************************************************
    servera.lab.example.com      : ok=2    changed=1    unreachable=0    failed=0
    workstation.lab.example.com  : ok=2    changed=1    unreachable=0    failed=0
  6. Log in to servera.lab.example.com as the devops user to verify that the MOTD is correctly displayed when logged in. Log off when you have finished.

    [student@workstation file-template]$ ssh devops@servera.lab.example.com
    This is the system servera.lab.example.com.
    This is a RedHat version 8.4 system.
    Only use this system with permission.
    Please report issues to: clyde@example.com.
    ...output omitted...
    [devops@servera ~]# exit
    Connection to servera.lab.example.com closed.

Finish

Run the lab file-template finish command to clean up after the exercise.

[student@workstation ~]$ lab file-template finish

This concludes the guided exercise.

Revision: rh294-8.4-9cb53f0