problem: date and time as variable in 2 steps

35 views
Skip to first unread message

Henrik Schuetze

unread,
Aug 28, 2018, 9:55:12 AM8/28/18
to Ansible Project
i want to create an backup with Backup_<Date + Time> so i use:

defaults\main.yml
file_date: "{{ lookup('pipe','date +%Y-%m-%d-%H-%M-%S') }}"

and then i want to transfer the file.

But between backup and transfer the time has changed... so my transfer goes wrong

What can i do ?


Sam Doran

unread,
Aug 28, 2018, 9:59:57 AM8/28/18
to 'Daley Okuwa' via Ansible Project
You can do one of two things:

1) Use set_fact to store the value of the date string you get from the lookup, then use that in the file and backup tasks

2) Register the results of the file task and use the path of the file from there in your backup task.

---

Sam Doran

Brian Coca

unread,
Aug 28, 2018, 10:13:17 AM8/28/18
to Ansible Project
To clarify, the reason you see 2 diff dates is because vars are 'lazy
evaluated' and evaluated for each task.

The solutions proposed above work because set_fact gets around this by
forcing evaluation and assigning the value. The registration takes the
'backup_file' which is also a static value.



--
----------
Brian Coca

Henrik Schuetze

unread,
Aug 28, 2018, 10:26:44 AM8/28/18
to Ansible Project
yes i see two different dates because this.

my solution is:

in \tasks\main.yml put in line 2:

- include: set_facts.yml

in \tasks\set_facts.yml

- set_fact: 
   file_date: "{{ lookup('pipe','date +%Y-%m-%d-%H-%M-%S') }}"
   cacheable: true

that worked for me.

Thanx for your help
Reply all
Reply to author
Forward
0 new messages