-2

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

U880D
  • 8,601
  • 6
  • 24
  • 40
vtr
  • 11
  • 6

3 Answers3

1

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') }}"
        
Frenchy
  • 16,386
  • 3
  • 16
  • 39
1

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.

seshadri_c
  • 6,906
  • 2
  • 10
  • 24
1

If facts are gathered it is also possible to use {{ ansible_date_time.date }}.

Thanks to

U880D
  • 8,601
  • 6
  • 24
  • 40