Skip to main content

Using Templates with Ansible

Hands-On Lab

 

Photo of Rob Marti

Rob Marti

Linux Training Architect I in Content

Length

00:15:00

Difficulty

Beginner

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Templates with Ansible

Introduction

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.

The Scenario

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. There's a sample httpd.conf in /root/ on the Ansible Host.

Logging In

Use the credentials provided on the hands-on lab page to get into Server1 to begin with. Since we need root privileges, let's just run sudo -i right off and become root.

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

Using the provided httpd.template, replace all occurrences of /var/www/ with {{ variable_name }} - similar to the following. Use whatever editor is most comfortable:

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

DocumentRoot "{{ webdir }}html"

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

There are more farther down the file, those are just a couple of the ones that need to be fixed.

Write a Playbook to Deploy the Template to the webserver Group

Once the template is fixed, we need to write a playbook. Call it template.yml, and it 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

Examine Current httpd.conf File

Log into one of the web servers:

ssh ansible@webserver1

Once we're in, we can look at the existing httpd.conf for any instance of opt:

sudo grep opt /etc/httpd/conf/httpd.conf

We'll see just one spot, and opt is part of the word option. Type exit to get out of that web server, so that we're back on the Ansible server as root.

Run the Playbook

Run the playbook with rhis:

ansible-playbook template.yml

Examine the httpd.conf File Again

Once the playbook has finished running, repeat the procedure we did a couple of steps ago. Log into one of the web servers:

ssh ansible@webserver1

Then look at the existing httpd.conf again for any instance of opt:

sudo grep opt /etc/httpd/conf/httpd.conf

Now we should see opt all over the place, in the same spots where we changed things in /root/httpd.template on the Ansible server.

Conclusion

We did it. We've got an Ansible playbook now that will go through all of our web servers and change Apache's document root. If it needs to move again, we can just make a few changes in the playbook and make it happen with another ansible-playbook template.yml run. Congratulations!