Managing Jinja2 Templates Using Ansible

@francilio thanks for reporting this. Thanks is just a typo in error message but validation is checking the correct location, could you share your KKE username we can re-check your answer.

1 Like

My KKE email is [email protected]

Hi @Inderpreet

I am getting the same error kindly check on this

My KKE emai Id is [email protected]

1 Like

@francilio @Prabhu this is marked Success for you guys.

2 Likes

@Inderpreet Thank you!!

hello @Inderpreet
i have the same issue here !

@redops this is marked Success for you.

thank you for your usual efforts @Inderpreet

Step1:

Go to /home/thor/ansible and update inventory file

Comment which is not required
image
Step2:

Update hosts as all in playbook.yml
image

Step3:

Update var file as below
image
Step4:

Update index.html.j2 under /home/thor/ansible/role/httpd/templates/
image
Step5:

Update tasks main.yml as below

How to update hostname in templates/index.html.js ? Is there other thing I have to change?

I used ansible_facts but not working
[root@stapp03 html]# cat index.html
This file was created using Ansible on {{ansible_facts[‘hostname’]}}

Also it is mentioned to use inventory_hostname?
Also please make sure not to hard code the server name inside the template. Instead, use inventory_hostname variable to fetch the correct value.

@Tej-Singh-Rana @andrzej please help

Looks fine what error you are getting?
{{ inventory_hostname }}

@Dhanabalan

I tried using template module instead of copy module.

1 Like

Hi Lakshmi,

Thank you very much…I followed what you suggested. It worked. How come using template is replacing correct value for inventory_hostname?

@Inderpreet I have the same issue.

image

@Dhanabalan

Copy module will copy the file from ‘files’ directory.
Template module will copy the jinja2 template from templates directory on Ansible playbooks.

1 Like

@Dhanabalan You should have used j2 extension for you template instead of js

jin

1 Like

Thank you, will try it if question repeat again.

@Inderpreet @Tej-Singh-Rana. I did the task as per the details . I did not check the permissions…Can you please reassign the task once. It was a silly mistake in permissions.

Hello, @pratikshag
Can you please share in the “Review Section” ?

Hello. Why my task is failed? “- ‘/var/www/html/index.html’ file has incorrect user owner on App Server 3”
In task was said nothing about the user owner. It was only about changing the rights to index.html (0744).

There is my scrennshots:

There is my task/main.yml code:

  • name: Add index.html
    template:
    src: /home/thor/ansible/role/httpd/templates/index.html.j2
    dest: /var/www/html/index.html
    mode: ‘0744’