How to get current timestamp in Salt Pillars/States ?

4,019 views
Skip to first unread message

Vitaly Isaev

unread,
Mar 11, 2016, 12:15:50 PM3/11/16
to Salt-users
Dear all, 
I'm looking for an opportunity to use current timestamp in my *.sls files. I didn't managed to find this function neither in SaltStack execution modules, nor in Jinja docs.

This will not compile:
{% set tstamp = datetime | strftime("%Y.%M.%d_%H:%M:%S") %}

I can use this workaround, but I'm sure that's not a Salt way:
{% set tstamp = salt["cmd.run"]("date +%Y.%M.%d_%H:%M:%S") %}

Any help will be appreciated.

Seth House

unread,
Mar 11, 2016, 1:20:27 PM3/11/16
to salt users list
There's an example buried in Salt's Jinja renderer docs but it's a
little hard to dig out unless you've seen it already. :-P

https://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.jinja.html#filters

Note the linked 'timelib' dep if you want to do anything that Python's
stdlib can't do.
> --
> You received this message because you are subscribed to the Google Groups
> "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to salt-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Matt

unread,
Apr 1, 2016, 10:56:09 AM4/1/16
to Salt-users
Here's what's worked for me:

write_deployment_info:
  file.managed:
    - name: {{ application_directory }}/.deployment_info.yaml
    - contents: |
        datetime_stamp: {{ None|strftime("%A %B %d %Y %H:%M") }}

Maxime Diawara

unread,
Jan 17, 2020, 6:16:40 PM1/17/20
to Salt-users
Hello,
Another solution could be to use the Saltstack module : 

salt.modules.system


{% set mydate = salt["system.get_system_date"]() %}
{% set mytime = salt["system.get_system_time"]() %}
{% set mydatetime = salt["system.get_system_date_time"]() %}

test_system_date:
  cmd
.run:
   
- name: 'echo {{ mydate }}'

test_system_time:
  cmd
.run:
   
- name: 'echo {{ mytime }}'

test_system_date_time:
  cmd
.run:
   
- name: 'echo {{ mydatetime }}'

Reply all
Reply to author
Forward
0 new messages