Ansible is an open-source configuration management and provisioning system that automates the deployment of applications, services or OSes across multiple hosts. The Templates module in Ansible allows you to save a template for your desired customization which can then be applied quickly by using this new saved file as input like creating a custom machine image on AWS. This article will show how to create templates with various configurations such as app servers, load balancers, etc., saving you time when applying these machines to different environments in development and staging deployments.
Ansible is a configuration management tool that allows users to automate the creation of systems. To save time, it is important to create templates for common configurations. This will allow users to create new systems with ease.
Using Ansible allows you to manage the settings of different servers and environments. But what if the configuration files differ from one server to the next? You might look at Ansible templates rather than creating a distinct setup for each server or environment.
You’ll learn what Ansible templates are, how they function, and how to utilize the Ansible template module to save time in this course.
This article will be a step-by-step guide. Make sure you have an Ansible controller host if you want to follow along. Ansible v2.9.18 will be used in this lesson.
What is a template in Ansible?
Text files must sometimes be sent to distant sites. Those text files are almost often configuration files. If you’re dealing with a single server, you may need to build an app.conf configuration file that certain services need.
That configuration file might include information particular to that server, such as the hostname, IP address, and so on. Because you’re dealing with a single server, you may generate the file on the Ansible controller and then transfer it to the server using the copy module in a playbook.
But what if you have many web servers, each of which requires the same configuration file but with different values? You can’t just transfer the configuration file to other computers; it’s only designed for a single server with a unique hostname, IP address, and other details. An Ansible template is required.
Ansible templates enable you to create text files containing variables rather than static values, which you can then alter during playbook execution.
What is the appearance of an Ansible template?
An Ansible template is a text file with the j2 file extension that was created using the Jinja2 templating language. A Jinja2 template resembles the text file you want to upload to a remote server. The only distinction is that the file includes variables rather than static values.
For example, on all of your webservers, you may need to create a configuration file named app.conf that includes references to each server’s IP address, the Ansible host, and the Ansible user. The app.conf file on a single server may look like this.
my ip = “192.168.0.1” “ANSBILECONTROL” is my host. “ansible user” is my user.
Because each item will be unique based on the remote host’s IP, the Ansible controller’s hostname, and the Ansible user, you can’t replicate this file to each website.
An Ansible template enables you to create variables that are evaluated at runtime and substituted on the remote host, rather than statically defining each of these values.
An example of the app.conf.j2 template file may be seen below. Each static value has now been replaced with a variable denoted on each side by double curly brackets. These variables are derived from Ansible facts in this case.
Template files are always J2 files with the same name as the files they produce on the destination host.
my host = “ansible host” my user = “ansible user” my ip = “ansible default ipv4[“address”]”
On distant hosts, how are templated files created?
After you’ve developed a template, you’ll need to have it transported to the remote computer and “transformed” into the real text file that it should look like. To do so, you’ll need to use a playbook to reference the template file.
Most Ansible administrators use the copy module to transport files to remote hosts, however this isn’t possible with templates, as previously stated.
A simple example reference from a playbook that moves the app.conf file to the /opt directory on all of the playbook’s target hosts can be found below.
– name: transfer a file from a local computer to a distant computer copy: # Copy Module src: “app.conf” # Declaring Copy Module # Source Location dest: “/opt/app.conf” # Remote Host Destination Location
Let’s pretend you’ve “templatized” the app.conf configuration file into an app.conf.j2 template file on your Ansible controller, as described in the previous section. You must now confirm that app.conf is still sent to the /opt directory, but that the variables have been changed with genuine values.
Simply modify the copy reference to template as shown below to inform the playbook to generate the app.conf file in the /opt directory. After that, Ansible calls the template module to transfer the template and replace the variables with static values.
– name: remote host template file template: src: “app.conf.j2” # Ansible template module # This is the source location dest: “/opt/app.conf” # This is the template src. # Remote host’s final destination
Ansible will transfer app.conf.j2 to the remote host’s /opt directory, replace all variables with static values, and rename the file to app.conf after the aforementioned operation in the playbook completes.
When you provide the template src with a directory path, Ansible looks for templates in the /<ansible_installation_directory>/files/ directory. If you simply provide the file name, Ansible will look for the template in the /<ansible_installation_directory>/templates/ directory instead.
A Template for Rendering a Configuration File
Let’s have a look at how to create an Ansible template and utilize the Ansible template module to construct a configuration file dynamically. You’re generating a file named app.conf in the /etc directory on a server called SRV1 in this example.
This section’s instructions will work with any kind of text file. As an example, the lesson will utilize a configuration file.
1. Log in to your Ansible controller host as the user you usually use to handle Ansible.
2. Change the working directory to a folder in your home directory that will house the demo files for this lesson.
/ansible template demo/mkdir cd /ansible template demo/ansible template demo
3. In the directory, create a template file named app.conf.j2 that looks like this.
ansible default ipv4[“address”] my ip = my host is the same as ansible host. if my user = ansible user
In your template, you may also utilize variables related to the Ansible template module.
4. In the same directory, create a basic playbook named my playbook.yml. The app.conf file is created in the /etc directory by this playbook.
template name: Ansible remote user: ubuntu # sample hosts: myserver Ubuntu tasks using a remote host: Create the app.conf configuration file template with the following name: dest: “/etc/app.conf” src: “/ansible template demo/app.conf.j2” become: real
5. Run the Ansible playbook with the SRV1 remote host as the target.
—inventory SRV1 ansible-playbook my playbook.yml
The playbook should then be executed by Ansible.
6. Verify that the /etc/app.conf configuration file is there and has the desired settings.
verify the configuration file /etc/app.conf
Using the Template Module to Update File Permissions
Let’s go a little more sophisticated now that you’ve seen the fundamentals of utilizing the template module. You’ll construct the identical app.conf file as before for this demonstration. This time, though, you’ll change the file’s owner and permissions.
User and File Permissions: A Windows Guy in a Linux World
You must use three options within the playbook to alter permissions on the file that the template module creates:
- owner — The owner of the file
- group – The group to which the file should belong.
- permissions – the mode This string may be rendered as either symbols or octal integers.
In symbolic mode, the letters u, g, and o stand for “user,” “group,” and “other.”
Open the my playbook.yml playbook and change the contents with those shown below, assuming you still have the /ansible template demo folder established from the previous step. Ansible will utilize connection variables to assign the owner and group to the Ansible user in this case. The file permissions are then set to 0644, which means:
- The owner has read/write access.
- Everyone else, including members of the group, has read permission.
—- – remote user: ubuntu tasks: Ansible file permission example Create the app.conf configuration file and assign rights using the following template: owner: “ansible user” group: “ansible user” src: “/ansible template demo/app.conf.j2” dest: “/etc/app.conf” mode: 0644 ## OR u=rw, g=w, and o=r become: true
The Ansible template module documentation lists all of the possible template module parameters.
Execute the playbook once again, as indicated below.
—inventory SRV1 ansible-playbook my playbook.yml
The app is now visible. The required file permissions have been set to conf.
Using Loops to Create Multiple File Templates
You may need to add many files on a remote host if a single file isn’t enough. You may utilize the loops with the template module in such instance. Using the loop argument to define a loop enables you to add many text files from a directory.
If you still have the /ansible template demo folder from the previous phase, the app.conf.j2 file should already be there.
1. In the /ansible template demo folder, create a second template file named app2.conf.j2.
template path = “template path” template fullpath = “template fullpath” template run date = “template run date” template uid = “template uid” template path = “template path” template fullpath = “template fullpath”
2. Replace all of the contents of the my playbook.yml book with the YAML below. The item variable in this playbook represents each template file handled in the loop. The loop argument then specifies which template files should be processed by the loop.
—- – remote user: ubuntu tasks: Ansible file permission example Create the app.conf configuration file and assign rights using the following template: src: “item.j2/ansible template demo/ “# Iterates through two templates. “/etc/item” as dest “ansible user” is the owner. “ansible user” is a group. mode: 0644 ## OR u=rw, g=w, o=r mode become: genuine loop: # Instruct the template module to locate and process each of these templates. – application1.conf – application2.conf
3. Now run the playbook again. —inventory SRV1 ansible-playbook my playbook.yml
—inventory SRV1 ansible-playbook my playbook.yml
Now you can see how Ansible recognizes and processes each template file.
Templates and the template module in Ansible may help you save time and produce dynamic text files on all of your remote servers. The copy module has comparable capabilities, but the template module is your best friend if you ever need to generate dynamic text files.
Ansible is a simple and powerful automation tool that allows you to easily configure systems with YAML files. Ansible templates can save time by creating configuration files for your servers. Reference: ansible template jinja2.
- ansible render template locally
- ansible template variables
- ansible templates example
- ansible template for loop
- ansible role templates