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
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 content of the inventory file.
[webservers] servera.lab.example.com [workstations] workstation.lab.example.com
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 }}.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: 0644Before 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.ymlplaybook: motd.yml
Run the motd.yml playbook.
[student@workstation file-template]$ansible-playbook motd.ymlPLAY [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
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.comThis 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 ~]#exitConnection to servera.lab.example.com closed.