How to use template in Ansible?
##### Step 1 – Create your inventory file with #####
[code]$ vi inventory
35.154.85.120 ansible_user=root ansible_ssh_private_key_file=remote.pem
35.154.85.120 ansible_user=root ansible_ssh_private_key_file=remote.pem[/code]
##### Step 2 – Create templates directory #####
[code]$ mkdir templates
$ vi templates/index.j2
<!DOCTYPE html>
<html>
<body>
<h1> Welcome to DevOpsSchool.com Ansible Training</h1>
<h3> This is Deployed in OS Family – {{ ansible_os_family }} </h3>
<h3> This is Deployed in OS Family – {{ ansible_hostname }} </h3>
<h3> Company Name – {{ companyname }} </h3>
companyname
</body>
</html>[/code]
<strong>##### Step 3 – Create a playbook.yaml#####</strong>
[code]—
– name: This sets up an httpd webserver
hosts: all
remote_user: ec2-user
become: yes
vars:
ansible_ssh_private_key_file: remote.pem
companyname: DevOpsSchool.com
pack: httpd
tasks:
– name: Install the httpd apps
yum: name={{ pack }}
– name: Deploy configuration File
template: src=templates/index.j2 dest=/var/www/html/index.html
– name: start the httpd service
service: name={{ pack }} state=started[/code]
##### Step 4 – Last comamds to execute #####
[code]$ ansible-playbook -i inventory httpd.yaml[/code]

















I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND