problem: date and time as variable in 2 steps

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 ?

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.

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.

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