Server date in Ansible/J2?

34 views
Skip to first unread message

j...@jpsdomain.org

unread,
Jul 10, 2016, 3:02:02 PM7/10/16
to Ansible Project
I have what seems to be a simple question that I can't find on the web.  How do I get the current date from the Ansible server into a J2 variable?  I know how to do it with {{ ansible_date_time.date }} but that's from the remote node at the time Ansible collected facts (which could be cached), NOT "now" on the server I'm running on...right?

Use case (this works, but it's remote node "setup" (get facts) time, not local server "now" time):
 - name: Update image_release file
   lineinfile:
     dest=..../image_release
     state=present
     insertbefore=BOF
     line='{{ ansible_date_time.date }} {{ image_release }}'

How do I get "now" on the server I'm running on?

Kai Stian Olstad

unread,
Jul 10, 2016, 3:41:55 PM7/10/16
to ansible...@googlegroups.com
On 09. juli 2016 04:03, j...@jpsdomain.org wrote:
> I have what seems to be a simple question that I can't find on the web. How do I get the current date from the Ansible server into a J2 variable? I know how to do it with {{ ansible_date_time.date }} but that's from the remote node at the time Ansible collected facts (which could be cached), NOT "now" on the server I'm running on...right?

Check out lookups
https://docs.ansible.com/ansible/playbooks_lookups.html

{{ lookup('pipe','date') }}

--
Kai Stian Olstad

j...@jpsdomain.org

unread,
Jul 11, 2016, 11:15:19 AM7/11/16
to Ansible Project, ansible-pr...@olstad.com
Thanks.  I should have mentioned I considered that, but it seems crazy and inefficient to have to shell out and run the `date` command when Python, Ansible and J2 already know what date and time it is...I just need a way to make one of them tell me in a playbook.  Is there really no way to do something like {{ now.strftime('%Y-%m-%d') }}?
Reply all
Reply to author
Forward
0 new messages