How to print the present date when the Ansible Jinja2 Template is run?
My Jinja2 template is
Today date is date +%Y-%m-%d
When I run the task it is simply copy pasting the same line, but I want todays date to be printed in the destination file
How to print the present date when the Ansible Jinja2 Template is run?
My Jinja2 template is
Today date is date +%Y-%m-%d
When I run the task it is simply copy pasting the same line, but I want todays date to be printed in the destination file
i dunno if i understand well your problem
but you just trap the date in a var and use the var in your template with "{{ date }}":
tasks:
- name: template {{ date }}
template: your j2 file and destfile with {{date}} inside your j2
vars:
date: "{{ lookup('pipe', 'date +%Y-%m-%d') }}"
A built-in way of getting a date into a Jinja template is to use the template_run_date
variable that's automatically available when using the template module.
For example a Jinja template file my.conf.j2
# My configuration file
# Updated on {{ template_run_date }}
Will render the date/time stamp of when the template was rendered.
If facts are gathered it is also possible to use {{ ansible_date_time.date }}
.
Thanks to