Using Templates with Ansible

15 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Templating is a powerful tool that we can add to our Ansible arsenal, enabling us to set up some interesting configurations quickly. This lab will help us practice with that tool.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Write a Template to Replace the Document Root in httpd.conf and Any Other Configurations Required

Using the provided httpd.template, replace all occurences of /var/www/ with
{{ variable_name}} – similar to the following. This will let us update the directory with the use of a variable:

<Directory />
   AllowOverride none
   Require all denied
</Directory>

DocumentRoot "{{ webdir }}html"

<Directory "{{ webdir }}">
   AllowOverride None
   Require all granted
</Directory>

Note that there are more places /var/www needs to be changed to {{ variable name }}, these are just two.

Write a Playbook to Deploy the Template to the webserver Group

Your playbook should look similar to this:

---
- name: Template playbook
  hosts: webservers
  become: yes
  vars:
   webdir: '/opt/'

  tasks:
   - name: Deploy the web template
     template:
      src: /root/httpd.template
      dest: /etc/httpd/conf/httpd.conf

Additional Resources

Notice: Ansible is installed as the root user, so please work on all tasks after elevating to the root user.

Our organization's standards are changing, regarding where Apache's document root will be stored. Rather than /var/www/html, the document root will be /opt/html. We need to make sure all instances of /var/www/ are changed to /opt/.

We must write a template and playbook to deploy a new httpd.conf on all web servers. Use the provided httpd.template file to get started.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!