How to import Python modules for user in Jinja2 template

1.5k Views Asked by At

I want to create a file name by formatting datetime.now(). It is simple enough in Python, but the datetime module needs to be imported. How can I import it for use in a Jinja2 template?

For example, in this Ansible task:

  - name: myTask
    shell: echo "123" > /tmp/{{datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}}.zaz
1

There are 1 best solutions below

1
On BEST ANSWER

Ansible already knows about the date/time.

 - name: myTask
   shell: echo "123" > /tmp/{{ ansible_date_time.date }}_{{ ansible_date_time.hour }}-{{ ansible_date_time.minute }}-{{ ansible_date_time.second }}.zaz

See this page for a list of default systems facts.