Skip to main content

Template

Continuing from lesson 3, we're going to use the setup module that brings Ansible facts and use the collected values inside the Nginx index HTML.

The Template module does nothing more than perform interpolation using Ansible facts into any file.

In playbook 3 we used the copy module to copy the index into the system, but we could replace the copy module with template and do variable interpolation at runtime.

To do this interpolation, Ansible uses jinja so the file must have a .j2 extension.

Configure the playbook4

~/projects/ansible/study-ansible/Aula 4 - Template main !5 ?1                                                                                                                      1.1.7 23:48:09
❯ ansible-playbook my_playbook4.yml

PLAY [Install nginx] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************
ok: [robot-1]
ok: [robot-2]

TASK [Installing Nginx] *************************************************************************************************************************************************************************
ok: [robot-2]
ok: [robot-1]

TASK [Enabling nginx service] ******************************************************************************************************************************************************************
ok: [robot-2]
ok: [robot-1]

TASK [Starting nginx service] ********************************************************************************************************************************************************************
ok: [robot-1]
ok: [robot-2]

TASK [Copying the index] ***************************************************************************************************************************************************************************
changed: [robot-1]
changed: [robot-2]

TASK [Copying nginx.conf] **********************************************************************************************************************************************************************
ok: [robot-1]
ok: [robot-2]

PLAY RECAP ****************************************************************************************************************************************************************************************
robot-1 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
robot-2 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

~/projects/ansible/study-ansible/Aula 4 - Template main !5 ?1 1.1.7 23:49:16
curl http://robot-1:
<html>
<head>
<title>Ansible Study</title>
<body>
<h1>DEVOPS GITOPS CLOUDOPS INFRAOPS ops.... ops... ops.. ops.!</h1>
<h2>THIS MACHINE'S IP IS: 172.31.8.74</h2>
</body>
</head>
</html>%

~/projects/ansible/study-ansible/Aula 4 - Template main !5 ?1 1.1.7 23:49:27
❯ curl http://robot-2:
<html>
<head>
<title>Ansible Study</title>
<body>
<h1>DEVOPS GITOPS CLOUDOPS INFRAOPS ops.... ops... ops.. ops.!</h1>
<h2>THIS MACHINE'S IP IS: 172.31.9.91</h2>
</body>
</head>
</html>%