>_ IzzyLab

Hands-on task

This task is step 11 of the course “Terraform and Ansible”

Ansible: config from a template

medium~15 min

Write a playbook /root/tpl.yml and a Jinja2 template that generate /etc/myapp.conf from the variable port with the value 9090. The finished file should contain the line listen 9090. The playbook must create /etc/myapp.conf where the file doesn't exist yet, and a second run must change nothing.

What you'll practise:
  • ansible
  • template module
  • jinja2
  • vars variables
  • ansible-playbook

Topic breakdown

The template module renders a target file from a Jinja2 template (.j2): it substitutes variable values into the spots with double curly braces and puts the result at the dest path. This is a basic config-management technique — one template serves different environments, only the set of variables changes, and those are convenient to declare in a vars section. You give the module src (the template) and dest (where to put the finished file).

What's checked

  1. the playbook renders the config from scratch
  2. /etc/myapp.conf has the line listen 9090
  3. the config is rendered with the template module
  4. a second run changes nothing (changed=0)

What it looks like