Lab 8 - Templates¶
Mandatory¶
Replace copy with template¶
- Change your
ntprole - Create the
templatesdirectory in your ntp role/home/ansible/training/roles/ntp/templates - Create the template file
templates/ntp.conf.j2(copy the ntp.conf file fromfiles/ntp.conf) - In your tasks file, replace the
copymodule with thetemplatemodule - Add a new task to start and enable the ntp service (see Service Module)
- Run the playbook
- Verify the configuration file
- If everything works, delete the file
files/ntp.conf
Hint
Don’t forget the .j2 file extension.
Use facts and ansible_header¶
- Add a comment in the
ntp.conf.j2file with the factansible_os_family - Add a
ansible_managedheader in thentp.conf.j2file - Run the playbook and verify the configuration file on the managed host
Loop¶
- Replace the
pool serverentries in your template filentp.conf.j2 - Loop over your
ntp_serverslist in the templatentp.conf.j2 - Run the playbook with diff, to see the changes
- Verify the configuration file
When Conditionals¶
- Make sure the
ntprole works onDebianandRedHat - Create the variable file
vars/RedHat.ymland add the variablentp_servicewith the valuentpd - Create a new task to include the variable file
RedHat.yml(see Include_Vars Module) -> Put this task in first place, to ensure it run before all other tasks - Ensure, the
include_varstask is running only on theansible_os_familyRedHat (see Chapter Variables) - Define a role default in
defaults/main.ymlfor the variablentp_servicewith the valuentp - Use the new variable
ntp_servicein yourhandlers/main.ymlandtasks/main.yml - Edit your installation task and use the
packagemodule instead of theaptmodule - Edit your playbook to run the ntp role on the
webhostgroup - Run the playbook
Overwrite role default with group_vars¶
- Create a group_vars file for your
dbhostgroup - Add the list variable
ntp_serverswith the itemsntp1.lab,ntp2.labandntp3.lab - Edit your playbook to run the ntp role
allhosts - Run the playbook
- Verify your
dbhost has other ntp servers
Commit your changes¶
- add all files to your git repo and commit it