Ansible: Writing ansible playbook to update “Motd” to display banner with Facts

Share At:

In continuation to my previous article titled “Ansible: Defining “Common Role” and Writing “Ansible playbook” to install Packages”, here we are going to see how to define “Motd role” and write an ansible playbook to display dynamic Motd on client servers.


  1. In this demonstration, we will be using centos-07. Please go through the Ansible documentation for supported OS platforms.
  2. We will be using 3 machines for our lab: 1 ansible workstation and 2 ansible clients with the below IP details:

Note: Ansible workstation would be our ansible server.

3. For all the servers — memory should be at least 2 GB and there should be at least 2 core cpu.

4. Ansible workstation and client servers should be able to communicate with each other over ssh (port 22).

5. We have already installed the ansible package on the ansible workstation and have created an inventory file — /etc/ansible/hosts.

The inventory file “/etc/ansible/hosts” should have entry like below:

Defining “Motd” Role and Writing Playbook:

Roles are, at the most basic level, groups of tasks organized by their purpose.

Here we will create “motd” role which displays the customized motd on the client servers. Once a user logs in to the client machine, the customized motd gets displayed.

Please perform the below steps:

  1. Create Directory structure something like below:

2. Now go to “/home/vagrant/repo/ansible-automation/roles/motd/tasks” and create a file called “main.yml”. Ansible is going to read main.yml at first.

The main.yml file should look like below:

4. Now go to the defaults directory and create again a main.yml file. This main.yml file will contain our banner:

# Default ASCII art shown at the beginning of the motd
motd_ascii_art: “ _ _ _ _\n / \\ _ __ ___(_) |__ | | ___\n / _ \\ | ‘_ \\/ __| | ‘_ \\| |/ _ \\\n / ___ \\| | | \\__ \\ | |_) | | __/\n /_/ \\_\\_| |_|___/_|_.__/|_|\\___|\n”
# # Default information to show under the ASCII art
— “ FQDN: “: “{{ ansible_fqdn }}”
— “ Distro: “: “{{ ansible_distribution }} {{ ansible_distribution_version }} {{ ansible_distribution_release }}”
— “ Virtual: “: “{{ ‘YES’ if ansible_virtualization_role == ‘guest’ else ‘NO’ }}\n”
— “ CPUs: “: “{{ ansible_processor_vcpus }}”
— “ RAM: “: “{{ (ansible_memtotal_mb / 1000) | round(1) }}GB”

You can download this file from the below github url:

5. Now go to the “templates” directory and create motd.j2 template. This motd.j2 template should look like below:

6. Now we are going to create a playbook called “webserver.yml” which will use the motd role we have just created.

For this, go to the “/home/vagrant/repo/ansible-automation” directory and create “webservers.yml” file. Update the webservers.yml file to look something like below:

7. Use ansible-playbook webservers.yml — check” to perform the dry runThis will not modify the banners (/etc/motd) on client servers.

8. Now run the “ansible-playbook webservers.yml” command to run the playbook and perform actual banner update client servers:

9. Now log out/log back in the client-server and validate if the motd have been updated. You should get something like below once you log in on the client machine.

That’s It. We are done.

Happy Learning !!!

Share At:
0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Back To Top

Contact Us