Ansible templates:
- Templates will copy the source file to the target host destination.
- While the template module gets executed it will read the template file and change all the variables to its value and copy the file to the target host.
- Template file ends with .j2 extension which stands for Jinja2 templates.
Example-1:
—–
—–
Create a directory.
#mkdir templates
Sample template file.
#vi templates/file.j2
<html>
<h1> Hello! Venkat </h1>
<h1> GoodDay {{username}} </h1>
</html>
Ansible Templates in Playbooks:
——————————————-
——————————————-
#vi playbooks/pb1.yml
- hosts: dev
vars:
- username: Venkat
- file_path: /var/www/html
tasks:
- name: Copy site files
template:
src= templates/file.j2
dest= {{file_path}}/index.html mode=0777
–> Use template module to copy the template file and the variables used in the template.
Ex-2: To change Tomcat port
#vi templates/demo.j2
.......
<Connector port= "{{myport}}"
protocol= "HTTP/1.1"
.......
(server.xml content)
After changing the Port edit the playbook.
#vi playbooks/pb2.yml
---
- hosts: web
become: true
vars:
- myport: 9090
- file_path: /etc/tomcat7/
tasks:
- name: to update packages
apt: update_cache=yes
- name: to install java
apt:
name=openjdk-7-jdk
state: latest
- name: to install tomcat
apt:
name: tomcat7
state: latest
- name: to change tomcat port
template:
src: templates/demo.j2
dest: {{file_path}}/server.xml
notify:
- restart tomcat
handlers:
- name: restart tomcat
service:
name: tomcat7
state: restarted
good explanation , it need more information
ReplyDelete