Skip to content

Instantly share code, notes, and snippets.

@halberom
Created June 5, 2014 10:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save halberom/b452df40828839fecabf to your computer and use it in GitHub Desktop.
Save halberom/b452df40828839fecabf to your computer and use it in GitHub Desktop.
ansible - using lookup to get a date timestamp into a variable
...
TASK: [debug var=mydate] ******************************************************
ok: [localhost] => {
"mydate": "20140605101824"
}
...
---
- hosts: all
tasks:
- set_fact: mydate="{{lookup('pipe','date +%Y%m%d%H%M%S')}}"
- debug: var=mydate
@ReSearchITEng
Copy link

ReSearchITEng commented May 17, 2016

For anyone interested in nanoseconds aswell, this is an example of getting the first 5 digits ---

  • hosts: all
    tasks:
    • set_fact: mydate="{{lookup('pipe','date +%Y%m%d%H%M%S**.%5N**')}}"
    • debug: var=mydate

@ReSearchITEng
Copy link

This is the way to set variable. The example above is for setting fact, and it won't be on all hosts same value.
If you want something like a timestamp for the entire run, do this at the beginning of your playbook:

hosts: localhost
  tasks:
  - shell: 'date +%Y%m%d%H%M%S.%5N'
    register: current_run_timestamp

@ssbarnea
Copy link

Is my impression or the register approach will not be able to use it as expected, you will be forced to refer to the variable as current_run_timestamp.stdout which is quite ugly.

@jeanmarc
Copy link

jeanmarc commented Dec 29, 2016

@ssbarnea: true, but that can be amended by adding another step: set_fact: the_timestamp = " {{ current_run_timestamp.stdout }}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment