Lab 8 - Templates

Mandatory

Replace copy with template

  • Change your ntp role
  • Create the templates directory in your ntp role /home/ansible/training/roles/ntp/templates
  • Create the template file templates/ntp.conf.j2 (copy the ntp.conf file from files/ntp.conf)
  • In your tasks file, replace the copy module with the template module
  • 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.j2 file with the fact ansible_os_family
  • Add a ansible_managed header in the ntp.conf.j2 file
  • Run the playbook and verify the configuration file on the managed host

Loop

  • Replace the pool server entries in your template file ntp.conf.j2
  • Loop over your ntp_servers list in the template ntp.conf.j2
  • Run the playbook with diff, to see the changes
  • Verify the configuration file

When Conditionals

  • Make sure the ntp role works on Debian and RedHat
  • Create the variable file vars/RedHat.yml and add the variable ntp_service with the value ntpd
  • 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_vars task is running only on the ansible_os_family RedHat (see Chapter Variables)
  • Define a role default in defaults/main.yml for the variable ntp_service with the value ntp
  • Use the new variable ntp_service in your handlers/main.yml and tasks/main.yml
  • Edit your installation task and use the package module instead of the apt module
  • Edit your playbook to run the ntp role on the web hostgroup
  • Run the playbook

Overwrite role default with group_vars

  • Create a group_vars file for your db hostgroup
  • Add the list variable ntp_servers with the items ntp1.lab, ntp2.lab and ntp3.lab
  • Edit your playbook to run the ntp role all hosts
  • Run the playbook
  • Verify your db host has other ntp servers

Commit your changes

  • add all files to your git repo and commit it