How to use template in Ansible?

By | September 6, 2018

How to use template in Ansible?

##### Step 1 – Create your inventory file with #####

$ 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

##### Step 2 – Create templates directory #####

$ 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>

<strong>##### Step 3 – Create a playbook.yaml#####</strong>

---
- 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

##### Step 4 – Last comamds to execute #####

$ ansible-playbook -i inventory httpd.yaml

Leave a Reply