4.2 Ansible Playbooks - Templates
In this lab we start to use templates!
Task 1
- Rewrite your playbook
motd.ymlwithout using theansible.builtin.copymodule, but rather using theansbile.builtin.templatemodule. - Use a Jinja2 template file called
motd.j2which uses the variablemotd_content.
Solution Task 1
Create the file motd.j2 with the following one-liner:
| |
Edit your motd.yml playbook to something like this:
| |
Run the playbook again.
| |
Task 2
- Improve the template
motd.j2by adding the default IP address of the server to the template. - Add information about the installed operating system to the
motdfile as well.
Tip
Remember using theansible.builtin.setup module to get a list of all facts! Ensure, that gather_facts was set to true.Solution Task 2
Add IP and OS to motd.j2:
| |
Rerun the playbook and check if the text has been changed accordingly:
| |
Task 3 (Advanced)
Create a variable users like the following:
| |
Put the variable in an appropriate place of your choice.
Create a playbook userplay.yml doing the following and running on node1 and node2:
- On
node1: Create a file/etc/dinner.txtwith the content below by using theansible.builtin.templatemodule:<name_of_user> <food_for_user> - On
node1: There should be an entry in the file/etc/dinner.txtfor each user in the variableusers. Use a for loop in the template. - On
node1: If a user has no food specified, use “kebab”. Look forfiltersin the online docs. You should be familiar with searching the online docs by now. - On
node2: The same playbookuserplay.ymlshould create a (Linux) group for every different food specified in the variableusers. If a user has no food defined, create the group “kebab” instead. - On
node2: Create a user for every entry in theusersvariable. Ensure that this user is also in the group with the same name as his food. Again, if no food is defined for this user, add group “kebab”.
Bonus 1
- On
node2: Set the login shell to/bin/zshfor all users.
Bonus 2
- On
node2: If (and only if) the user is “santos”, disable login. Do this by setting santos’ login shell to/usr/sbin/nologin. Use an if/else statement in the template for that purpose.
Bonus 3
- All on
node2:- Set the default password for all the newly created users to “
N0t_5o_s3cur3” - Once the password has been set, your playbook should not set it again. Not even when it got changed.
- Hash the password using the sha512 algorithm.
- Don’t define a salt for the password.
- Verify that you are able to log in as one of the users via SSH providing the password.
- Set the default password for all the newly created users to “
Warning
Be aware that it is NOT a good idea to set passwords in clear text. We will learn in the lab aboutansible-vault how to handle this in a better way.
Never ever do this in a productive environment.Solution Task 3
Note
Be aware that there are multiple possible solutions.
Documentation about filters
| |
Tip
See the ansible.builtin.user module for how to set the password and search for a link to additional documentation
about how to set passwords in Ansible.
Note, that it would be even better to create a hash of the password beforehand
and then set the hash in the task above and not create it in the task itself.
Reason being the above would result in a state changed everytime it runs and is therefore not idempotent.
You can find in the documentation mentioned how to get the hash in advance.
Run the playbook, then check on node1 (as user root) if everything is as expected:
| |
Check as well on node2 (as user root):
| |
Login to node2 as user jim, providing the password via SSH prompt:
| |
Task 4 (Maester)
Create a playbook serverinfo.yml that does the following:
- On all nodes: Place a file
/root/serverinfo.txtwith a line like the following repeated for each and every server in the inventory:
<hostname>: OS: <operating system> IP: <IP address> Virtualization Role: <hardware type>
Replace
hostname,operating system,IP addressandhardware typewith a reasonable fact.Run your playbook and check on all servers by using an
ansiblead hoc command if the content of the file/root/serverinfo.txtis as expected.Are you an Ansible Maester already? Solve the solution once by using a template and once without using a template!
Solution Task 4
Possible solution 1:
| |
Note
Have a good look at where to set quotes and where not!
hostvars[host] without the quotes around host is not really intuitive.
More about that in the FAQ.
Possible solution 2:
| |