The goal of this task will be configuring a welcome message per host group, each message will be different.
- Ansible installed, version 2.7 is preferred
- One ‘ansible’ user configured, with sudo privileges and passwordless access to all managed hosts
- 1 ansible-control node
- 3 managed-nodes
Our inventory file should look something like this:
[group1] managed-node1 [group2] managed-node2 [group3] managed-node3
In the same directory where our inventory is, let’s create our main playbook:
touch setmotd.yml vim setmotd.yml
The content of the main playbook will look something similar to this:
--- - hosts: all become: true tasks: - name: Setting motd file for group 1 copy: dest: /etc/motd content: "Welcome to Group 1\n" when: "'group1' in group_names" - name: Setting motd file for group 2 copy: dest: /etc/motd content: "Welcome to Group 2\n" when: "'group2' in group_names" - name: Setting motd file for group 3 copy: dest: /etc/motd content: "Welcome to Group 3\n" when: "'group3' in group_names"
As you can see in the code above, this playbook runs in all hosts in our inventory with become true enabled. Then it’ll use the copy module to create a file in /etc/motd with the desired content, this file will only be created when our when statement is met.
“If group1 is defined in the group names, then place this line of text”. Run your playbook with the following command:
Once the playbook is done running, ssh to one of the 3 managed-nodes and you should see your welcome message set.
[[email protected] ~]$ ssh managed-node2 Last login: Tue Mar 24 19:22:53 2020 from xxx.xxx.xxx.xxx Welcome to Group 2
There you have it. That was the last bit of configuration needed. In the upcoming days I’ll be posting more practice exercises for EX407, so stay tuned.