Junior — Middle
Explain what a template in Ansible is and how it is used in configuration automation.
sobes.tech AI
Answer from AI
In Ansible, a template is a file with dynamic content, usually written using Jinja2. It allows creating configuration files or scripts by substituting variables and expressions at playbook runtime. This is convenient for generating configurations that depend on host parameters or environment.
Example of using a template:
Template file nginx.conf.j2:
server {
listen 80;
server_name {{ domain_name }};
root {{ document_root }};
}
In the playbook, you can use the template module to generate the final file:
- name: Deploy nginx configuration
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
vars:
domain_name: example.com
document_root: /var/www/html
Thus, templates allow flexible management of configurations during automation.