Bookmark this page

Lab: Deploying Files to Managed Hosts

In this lab, you run a playbook that creates a customized file on your managed hosts by using a Jinja2 template.

Outcomes

  • Build a template file.

  • Use the template file in a playbook.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available.

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

Note

All files used in this exercise are available on workstation in the /home/student/file-review/files directory.

Procedure 5.3. Instructions

  1. Review the inventory file in the /home/student/file-review directory. This inventory file defines the servers group, which has the serverb.lab.example.com managed host associated with it.

    1. On workstation, change to the /home/student/file-review directory.

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

      [servers]
      serverb.lab.example.com
  2. Identify the facts on serverb.lab.example.com that show its total amount of system memory, and the number of processors it has.

    Create a playbook called serverb_facts.yml in the /home/student/file-review directory. Edit it to contain a play that uses the ansible.builtin.debug module to display a list of all the facts for the serverb.lab.example.com managed host. The ansible_facts['processor_count'] and ansible_facts['memtotal_mb'] facts provide information about the resource limits of the managed host.

    [student@workstation file-review]$ cat serverb_facts.yml
    ---
    - name: Display ansible_facts
      hosts: serverb.lab.example.com
      tasks:
        - name: Display facts
          debug:
            var: ansible_facts
    
    [student@workstation file-review]$ ansible-navigator run \
    > -m stdout serverb_facts.yml
    
    PLAY [Display ansible_facts] ***********************************************
    
    TASK [Gathering Facts] *****************************************************
    ok: [serverb.lab.example.com]
    
    TASK [Display facts] *******************************************************
    ok: [serverb.lab.example.com] => {
        "ansible_facts": {
    ...output omitted...
            "memtotal_mb": 960,
    ...output omitted...
            "processor_count": 1,
    ...output omitted...
        }
    }
    
    PLAY RECAP *****************************************************************
    serverb.lab.example.com    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  3. In the /home/student/file-review/templates directory, create a Jinja2 template file for the Message of the Day (/etc/motd), named motd.j2.

    After this template has been deployed to serverb.lab.example.com, when the devops user logs in, a message should display on their terminal that shows the system's total memory and processor count. Use the ansible_facts['memtotal_mb'] and ansible_facts['processor_count'] facts in the template to provide the system resource information for the message.

    System total memory: {{ ansible_facts['memtotal_mb'] }} MiB.
    System processor count: {{ ansible_facts['processor_count'] }}
  4. In the /home/student/file-review directory, create a new playbook file called motd.yml that contains a new play that runs on all hosts in the inventory. It must log in using the devops user on the remote host, and use become to enable privilege escalation for the whole play.

    The play must have a task that uses the ansible.builtin.template module to deploy the motd.j2 Jinja2 template file to the file /etc/motd on the managed hosts. The resulting file must have the root user as its owning user and group, and its permissions must be 0644.

    Add an additional task that uses the ansible.builtin.stat module to verify that /etc/motd exists on the managed hosts and registers its results in a variable. That task must be followed by a task that uses ansible.builtin.debug to display the information in that registered variable.

    Add a task that uses the ansible.builtin.copy module to place files/issue into the /etc/ directory on the managed host, use the same ownership and permissions as /etc/motd.

    Finally, add a task that uses the ansible.builtin.file module to ensure that /etc/issue.net is a symbolic link to /etc/issue on the managed host.

    1. Create the playbook and configure the remote_user and become directives.

      ---
      - name: Configure system
        hosts: all
        remote_user: devops
        become: true
        tasks:
    2. Create a task using the ansible.builtin.template module to deploy the motd.j2 Jinja2 template file to the file /etc/motd

          - name: Configure a custom /etc/motd
            ansible.builtin.template:
              src: templates/motd.j2
              dest: /etc/motd
              owner: root
              group: root
              mode: 0644
    3. Create a task that uses the ansible.builtin.stat module to verify that /etc/motd exists, and register the results. Add another task using the ansible.builtin.debug module to display the registered variable.

          - name: Check file exists
            ansible.builtin.stat:
              path: /etc/motd
            register: motd
      
          - name: Display stat results
            ansible.builtin.debug:
              var: motd
    4. Add a task that uses the ansible.builtin.copy module to place files/issue into the /etc/ directory.

          - name: Copy custom /etc/issue file
            ansible.builtin.copy:
              src: files/issue
              dest: /etc/issue
              owner: root
              group: root
              mode: 0644
    5. Add a task that uses the ansible.builtin.file module to ensure that /etc/issue.net is a symbolic link to /etc/issue.

          - name: Ensure /etc/issue.net is a symlink to /etc/issue
            ansible.builtin.file:
              src: /etc/issue
              dest: /etc/issue.net
              state: link
              owner: root
              group: root
              force: yes
    6. The completed playbook should look as follows.

      ---
      - name: Configure system
        hosts: all
        remote_user: devops
        become: true
        tasks:
          - name: Configure a custom /etc/motd
            ansible.builtin.template:
              src: templates/motd.j2
              dest: /etc/motd
              owner: root
              group: root
              mode: 0644
      
          - name: Check file exists
            ansible.builtin.stat:
              path: /etc/motd
            register: motd
      
          - name: Display stat results
            ansible.builtin.debug:
              var: motd
      
          - name: Copy custom /etc/issue file
            ansible.builtin.copy:
              src: files/issue
              dest: /etc/issue
              owner: root
              group: root
              mode: 0644
      
          - name: Ensure /etc/issue.net is a symlink to /etc/issue
            ansible.builtin.file:
              src: /etc/issue
              dest: /etc/issue.net
              state: link
              owner: root
              group: root
              force: yes
  5. Verify that your playbook contains no syntax errors.

    Before you run the playbook, use the ansible-navigator run --syntax-check command to validate its 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-review]$ ansible-navigator run \
    > -m stdout motd.yml --syntax-check
    playbook: /home/student/file-review/motd.yml
  6. Run the motd.yml Ansible Playbook.

    [student@workstation file-review]$ ansible-navigator run \
    > -m stdout motd.yml
    
    PLAY [Configure system] ****************************************************
    
    TASK [Gathering Facts] *****************************************************
    ok: [serverb.lab.example.com]
    
    TASK [Configure a custom /etc/motd] ****************************************
    changed: [serverb.lab.example.com]
    
    TASK [Check file exists] ***************************************************
    ok: [serverb.lab.example.com]
    
    TASK [Display stat results] ************************************************
    ok: [serverb.lab.example.com] => {
        "motd": {
            "changed": false,
            "failed": false,
    ...output omitted...
    
    TASK [Copy custom /etc/issue file] *****************************************
    changed: [serverb.lab.example.com]
    
    TASK [Ensure /etc/issue.net is a symlink to /etc/issue] ********************
    changed: [serverb.lab.example.com]
    
    PLAY RECAP *****************************************************************
    serverb.lab.example.com    : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  7. Confirm that the motd.yml playbook has run correctly.

    Use ssh to log in to serverb.lab.example.com as the devops user, and verify that the /etc/motd and /etc/issue contents are displayed when you log in. Log off when you have finished.

    [student@workstation file-review]$ ssh devops@serverb.lab.example.com
    ------------------------------- PRIVATE SYSTEM -----------------------------
    *   Access to this computer system is restricted to authorized users only.   *
    *                                                                            *
    *      Customer information is confidential and must not be disclosed.       *
    ----------------------------------------------------------------------------
    System total memory: 960 MiB.
    System processor count: 1
    
    Register this system with Red Hat Insights: insights-client --register
    Create an account or view all your systems at https://red.ht/insights-dashboard
    Last login: Thu Jul  7 14:34:04 2022 from 172.25.250.9
    [devops@serverb ~]$ logout

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab grade file-review

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

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

This concludes the section.

Revision: rh294-9.0-c95c7de