In this exercise, you create a simple template file that your playbook uses to install a customized Message of the Day file on each managed host.
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-template
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
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.
Navigate to the /home/student/file-template working directory.
[student@workstation ~]$ cd ~/file-template
[student@workstation file-template]$Display the contents of the inventory file.
[webservers] servera.lab.example.com [workstations] workstation.lab.example.com
Create a template for the Message of the Day file (/etc/motd) and save it as 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 Linux distribution information.
system_owner, for the system owner's email.
This variable is going to be set by the play in the motd.yml playbook, which you edit in the next step.
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 }}.Create a playbook file named motd.yml in the current working directory.
Create a play in that file that defines the system_owner variable in its vars section.
The play must have a task that uses the ansible.builtin.template module to deploy the motd.j2 Jinja2 template to the remote file /etc/motd on the managed hosts.
It must set the owner and group of /etc/motd 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
ansible.builtin.template:
src: motd.j2
dest: /etc/motd
owner: root
group: root
mode: 0644Before running the playbook, use the ansible-navigator run --syntax-check command to verify 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-template]$ansible-navigator run \>-m stdout motd.yml --syntax-checkplaybook: /home/student/file-template/motd.yml
Run the motd.yml playbook.
[student@workstation file-template]$ansible-navigator run \>-m stdout motd.ymlPLAY [Configure SOE] *********************************************************** TASK [Gathering Facts] ********************************************************* ok: [servera.lab.example.com] ok: [workstation.lab.example.com] TASK [Configure /etc/motd] ***************************************************** changed: [servera.lab.example.com] changed: [workstation.lab.example.com] PLAY RECAP ********************************************************************* servera.lab.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 workstation.lab.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
From workstation, use ssh to log in to servera.lab.example.com as the devops user to verify that the Message of the Day is correctly displayed on your terminal when you log in.
Log off when you have finished.
[student@workstation file-template]$ssh devops@servera.lab.example.comThis is the system servera.lab.example.com. This is a RedHat version 9.0 system. Only use this system with permission. Please report issues to: clyde@example.com. ...output omitted... [devops@servera ~]$exitlogout Connection to servera.lab.example.com closed.
This concludes the section.