As this year draws to an end i.e. today is the 31st of December as I’m writing this article. We are sure that your email and phone inboxes would be filled with wishes from your loved ones and friends. We wanted to take this opportunity to share our best wishes to you in an interesting Ansible way.
In this article, we will share an Ansible playbook which prints the line “HAPPY NEW YEAR TO ALL” in a very colorful manner. We’ll show you how to execute the playbook properly but we will not do a deep dive into the plugins that are responsible for generating the output.
To get started we need the GitHub repository ansible-cowmas. You can download the repository to your local system from the URL or you can clone it directly if you are using git on your local system.
I’ve downloaded the repository from the URL and copied it to my local system.
[root@linuxnix seasons_greetings]# ls ansible-cowmas-master.zip
Let’s extract this zip file now.
[root@linuxnix seasons_greetings]# unzip ansible-cowmas-master.zip Archive: ansible-cowmas-master.zip 25dda48a4be957b306dd785ee512835ee9df5711 creating: ansible-cowmas-master/ inflating: ansible-cowmas-master/.gitignore inflating: ansible-cowmas-master/LICENSE inflating: ansible-cowmas-master/README.md inflating: ansible-cowmas-master/ansible.cfg creating: ansible-cowmas-master/callback_plugins/ inflating: ansible-cowmas-master/callback_plugins/cowmas.py inflating: ansible-cowmas-master/hello_world.yml [root@linuxnix seasons_greetings]# ls ansible-cowmas-master ansible-cowmas-master.zip [root@linuxnix seasons_greetings]#
Now let’s take a peek at the contents of the directory.
[root@linuxnix seasons_greetings]# cd ansible-cowmas-master/ [root@linuxnix ansible-cowmas-master]# ls ansible.cfg callback_plugins hello_world.yml LICENSE README.md [root@linuxnix ansible-cowmas-master]#
The callback_plugins directory contains a python script which is responsible for generating all the wonderful colorful display of text that our playbook execution will print.
[root@linuxnix ansible-cowmas-master]# cd callback_plugins/ [root@linuxnix callback_plugins]# ls cowmas.py
The ansible.cfg file contains the below content:
[root@linuxnix ansible-cowmas-master]# cat ansible.cfg [defaults] callback_whitelist = cowmas stdout_callback = cowmas
The two statements are related to callback plugins. Callback plugins enable adding new behaviors to Ansible when responding to events.
By default, callback plugins control most of the output you see when running the command line programs, but can also be used to add as an additional output, integrate with other tools and marshall the events to a storage backend. So, within our ansible.cfg file, we are allowing the output from the execution of the cowmas.py script to print to the screen during playbook execution.
The hello_world.yml file is our playbook and it simply uses the debug module to print the message Hello World!.
Given below is the content of the playbook.
[root@linuxnix ansible-cowmas-master]# cat hello_world.yml - name: Hello World Sample hosts: all vars: failed: false changed: false tasks: - name: Hello Message debug: msg: "Hello World!" failed_when: failed changed_when: changed [root@linuxnix ansible-cowmas-master]#
To execute the playbook we need to type the following command:
ansible-playbook -i sahil, -c local hello_world.yml
I’ve specified my own name as the hostname. you may specify a name of your choice as long as you conform to the syntax of the command. The hostname doesn’t really matter much because I’ve used the -c local option to ensure that the playbook executes on the localhost only.
When you execute the playbook, you’ll see the colorful output as shown in the screenshot below:
The cowmas.py script that was in the callback_plugins directory was originally supposed to say “HAPPY AUTOMATING TO ALL” but I changed it to “HAPPY NEW YEAR TO ALL”.
You may update it accordingly to your liking by editing the python script using an editor of your choice. I used vim here. The text to be printed is on line number 95 in the script.
[root@linuxnix callback_plugins]# grep -n HAPPY cowmas.py 95: print colorize("HAPPY NEW YEAR TO ALL!\n") [root@linuxnix callback_plugins]#
In this article, we showed you to download the season’s greetings playbook from GitHub on to your local system and execute it to see a nice greeting message.
We encourage you to download and share this greeting with your cherished ones and from us at the Linux Juggernaut, we wish you a very Happy New Year.
Latest posts by Sahil Suri (see all)
- Docker networking basics explained - September 20, 2019
- How to push an image to Docker hub - September 18, 2019
- Ansible: insert a line after a string using lineinfile module - September 17, 2019
- Common Docker image and container management commands - September 16, 2019
- Docker Container life cycle explained - September 9, 2019