4.2 Ansible Playbooks - Templates
In this lab we start to use templates!
Task 1
- Rewrite your playbook
motd.yml
without using thecopy
module, but rather using thetemplate
module. - Use a Jinja2 template file called
motd.j2
which 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.j2
by adding the default IP address of the server to the template. - Add information about the installed operating system to the
motd
file as well.
Tip
Remember using thesetup
module to get a list of all facts!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.txt
with the content below by using thetemplate
module:<name_of_user> <food_for_user>
- On
node1
: There should be a entry in the file/etc/dinner.txt
for each user in the variableusers
. Use a for loop in the template. - On
node1
: If a user has no food specified, use “kebab”. Look forfilters
in the online docs. You should be familiar with searching the online docs by now. - On
node2
: The same playbookuserplay.yml
should 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 theusers
variable. 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/zsh
for all users.
Bonus 2
- On
node2
: If (and only if) the user is “santos”, disable login. Do this by setting santos’s 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 of 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 login as one of the users via SSH providing the password.
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 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 before and then set the hash in the task above and not create it in the task itself. Reason beeing 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 before.
Run the playbook, then check on node1
(as user root
) if everthing 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.txt
with a line like follows for each and every server in the inventory:
<hostname>: OS: <operating system> IP: <IP address> Virtualization Role: <hardware type>
Replace
hostname
,operating system
,IP address
andhardware type
with a reasonable fact.Run your playbook and check on all servers by using an
ansible
ad hoc command if the content of the file/root/serverinfo.txt
is 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 F.A.Q..
Possible solution 2:
|
|