# problem: date and time as variable in 2 steps

**URL:** <https://forum.ansible.com/t/problem-date-and-time-as-variable-in-2-steps/28444>\
**Category:** Ansible Project\
**Created:** [August 28, 2018, 1:55pm UTC](https://forum.ansible.com/t/problem-date-and-time-as-variable-in-2-steps/28444 "2018-08-28T13:55:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik\_Schuetze](https://avatars.discourse-cdn.com/v4/letter/h/e9bcb4/32.png) [@Henrik\_Schuetze](https://forum.ansible.com/u/Henrik_Schuetze)\
**Post date:** [August 28, 2018, 1:55pm UTC](https://forum.ansible.com/t/problem-date-and-time-as-variable-in-2-steps/28444/1 "2018-08-28T13:55:12Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![sdoran](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@sdoran](https://forum.ansible.com/u/sdoran)\
**Post date:** [August 28, 2018, 1:59pm UTC](https://forum.ansible.com/t/problem-date-and-time-as-variable-in-2-steps/28444/2 "2018-08-28T13:59:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 28, 2018, 2:12pm UTC](https://forum.ansible.com/t/problem-date-and-time-as-variable-in-2-steps/28444/3 "2018-08-28T14:12:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Henrik\_Schuetze](https://avatars.discourse-cdn.com/v4/letter/h/e9bcb4/32.png) [@Henrik\_Schuetze](https://forum.ansible.com/u/Henrik_Schuetze)\
**Post date:** [August 28, 2018, 2:26pm UTC](https://forum.ansible.com/t/problem-date-and-time-as-variable-in-2-steps/28444/4 "2018-08-28T14:26:44Z")

</div>

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
