# How can I create a path variable containing a timestamp?

**URL:** <https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553>\
**Category:** Ansible Project\
**Created:** [March 21, 2013, 5:33pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553 "2013-03-21T17:33:29Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clifford\_Sanders](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Clifford\_Sanders](https://forum.ansible.com/u/Clifford_Sanders)\
**Post date:** [March 21, 2013, 5:33pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/1 "2013-03-21T17:33:29Z")

</div>

Hi,

I’m trying to create something like:

vars:  
path\_to\_somewhere: /home/somewhere/${timestamp}/foo

the timestamp format should be like the output of ‘date +%Y%m%d%H%M%S’ → 20130321164525

Is this possible?

Thank you

Clifford

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 21, 2013, 6:57pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/2 "2013-03-21T18:57:24Z")

</div>

Currently you can use "register" to store a variable, and then access it.

If you want all the timestamps to be the same, you could use a  
particular host for the value and pull it out of hostvars.

See the documentation on "register" for some starter content.

---

<div class="post-metadata">

**Author:** ![Clifford\_Sanders](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Clifford\_Sanders](https://forum.ansible.com/u/Clifford_Sanders)\
**Post date:** [March 21, 2013, 7:58pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/3 "2013-03-21T19:58:04Z")

</div>

Hi Michael,

thanks for answering.

As I undestand when using “register” the variable is created during task execution.  
But I also need the variable in var files that are included.

– app-playbook.yml –

vars:  
path\_to\_somewhere: /home/somewhere/${timestamp}/foo

…  
vars\_files:  
…/vars/common\_vars.yml

– common\_vars.yml –  
path\_1: ${path\_to\_somewhere}/bar  
path\_2: ${path\_1}/blah

path\_1 and path\_2 are then used in the task file

I also thought about getting the timestamp out of the hostvars but couldn’t find the right value yet.

Clifford

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 22, 2013, 12:25pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/4 "2013-03-22T12:25:30Z")

</div>

> As I undestand when using "register" the variable is created during task execution.

But I also need the variable in var files that are included.

You could into the $PIPE lookup plugin

$PIPE(some command here)

The only downside is it will be evaluated a \*lot\*, work to make this  
be evaluated outside of the multiprocessing loop can be done to make  
it convert to variables earlier, but I must say including a variable  
file based on the time and date seems  
a little complicated to me.

Can I ask what the underlying use case is?

---

<div class="post-metadata">

**Author:** ![Clifford\_Sanders](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@Clifford\_Sanders](https://forum.ansible.com/u/Clifford_Sanders)\
**Post date:** [March 22, 2013, 7:50pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/5 "2013-03-22T19:50:25Z")

</div>

Hi Michael,

> You could into the $PIPE lookup plugin
> 
> $PIPE(some command here)

so

vars:  
timestamp: $PIPE(date +%Y%m%d%H%M%S)  
path\_1: /app/releases/${timestamp}-v1.0/ROOT

path\_2: /app/config/${timestamp}-v2.3.1  
path\_3: ${path\_2}/properties

would work? That would be great!

> The only downside is it will be evaluated a _lot_, work to make this  
> be evaluated outside of the multiprocessing loop can be done to make  
> it convert to variables earlier, but I must say including a variable  
> file based on the time and date seems  
> a little complicated to me.

The documentation says:

“… and if it is used in a variable definition, it will be executed each time the variable is evaluated”

Does this mean that it would be called twice per server in the example above and there would be the possiblity of different values for ‘timestamp’?  
This would be ok for my use case I think. It is not essential that those timestamps are equal.  
I just need them to sort the folder names.

> Can I ask what the underlying use case is?

Sure. I’m deploying using Ansible to deploy webapps and webservices to our tomcat instances.

The main steps of the workflow are:

1. download artifact (zip containing application war and configuration jar) from Nexus into tmp folder /app/tmp and unpack
2. unpack war to /app/releases/20130322201903-v1.0/ROOT
3. create/change symlink /app/current\_app to point to /app/releases/20130322201903-v1.0
4. unpack jar to /app/config/20130322201904-v2.3.1
5. create/change symlink /app/current\_config to /app/config/20130322201904-v2.3.1
6. delete /app/tmp
7. restart tomcat

In case of an error i would do a rollback by just changing the symlinks to point to the folders to which they were linked before by reverse sorting the folder names.  
As I understand capistrano uses a similar mechanism like this to have a fast and secure rollback.

Maybe I’m getting it all wrong. So please feel free to make suggestions

Regards,

Clifford

Regards,

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 23, 2013, 3:40pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/6 "2013-03-23T15:40:22Z")

</div>

"Does this mean that it would be called twice per server in the  
example above and there would be the possiblity of different values  
for 'timestamp'?"

Yes.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 23, 2013, 3:41pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/7 "2013-03-23T15:41:28Z")

</div>

BTW, the super easiest way to do this would be assigning the timestamp  
in a bash wrapper script and then using

--extra-vars="timestamp=foo"

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 23, 2013, 3:42pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/8 "2013-03-23T15:42:25Z")

</div>

I'm also not against making some more statistics based times available  
as magic variables, like $ansible\_play\_start\_time or something like  
that.

Patches would be considered.

---

<div class="post-metadata">

**Author:** ![Sankalp\_Khare](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Sankalp\_Khare](https://forum.ansible.com/u/Sankalp_Khare)\
**Post date:** [August 26, 2014, 9:58am UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/9 "2014-08-26T09:58:33Z")

</div>

Doesn’t seem to work for me:

failed: [[ec2-23-20-215-207.compute-1.amazonaws.com](http://ec2-23-20-215-207.compute-1.amazonaws.com)] =\> {“changed”: true, “cmd”: “tar -cf $PIPE(date +%Y%m%d-%H%M%S).tar file1”, “delta”: “0:00:00.007156”, “end”: “2014-08-26 08:46:56.146550”, “rc”: 2, “start”: “2014-08-26 08:46:56.139394”, “warnings”: [“Consider using unarchive module rather than running tar”]}  
stderr: /bin/sh: 1: Syntax error: “(” unexpected

I’ve got this in the playbook :

- name: Create timestamped backup  
hosts: all  
sudo: no  
gather\_facts: False  
vars:  
timestamp: $PIPE(date +%Y%m%d%H%M%S)  
tasks:
- name: backup existing files

shell: tar -cf {{ timestamp }}.tar file1  
args:  
executable: /bin/bash  
register: backup  
ignore\_errors: true

Ansible is putting the verbatim contents of the timestamp var into the tar command. I would’ve hoped those contents got evaluated first, into a timestamp.

Ps: why is the latest ansible checkout showing me cowsays instead of regular play/task/… titles? 😃

e.g.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 26, 2014, 12:45pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/10 "2014-08-26T12:45:57Z")

</div>

Yeah that’s an easy one, fortunately!

That above thread that is well over a year and a half old, using some syntax in Ansible that is quite out of date, and legacy syntaxes like that have been pruned from Ansible in that time (after deprecating over the course of several releases with notes about how to upgrade)

change the $PIPE(…) to:

{{ lookup(‘pipe’, ‘date +%Y%m%d%H%M%S’ }}

Should be good to go!

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [August 26, 2014, 4:17pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/11 "2014-08-26T16:17:37Z")

</div>

Wouldn’t the value change in the course of a play?

Michael DeHaan [michael@ansible.com](mailto:michael@ansible.com) napisał:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 26, 2014, 10:56pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/12 "2014-08-26T22:56:09Z")

</div>

Yep.

The trick would be to set it via set\_fact…

set\_fact:  
var: {{ lookup(‘pipe’, ‘foo’) }}

And then refer to it consistently from one host via hostvars, or not care that it was different.

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [August 27, 2014, 9:39am UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/13 "2014-08-27T09:39:26Z")

</div>

> Ps: why is the latest ansible checkout showing me cowsays instead of regular play/task/… titles? 😃

hmm… I always have the nocows=1 in my ansible.cfg  
otherwise, if ansible finds the cowsay package, it will use it by default.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 27, 2014, 12:18pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/14 "2014-08-27T12:18:26Z")

</div>

That would be a question for a new thread, but this topic hasn’t changed.

Sorry: big list, do need to play thread-cop.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 27, 2014, 12:18pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/15 "2014-08-27T12:18:51Z")

</div>

this topic → this behavior.

Probably your config file has it in the wrong section.

---

<div class="post-metadata">

**Author:** ![Sankalp\_Khare](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@Sankalp\_Khare](https://forum.ansible.com/u/Sankalp_Khare)\
**Post date:** [September 7, 2014, 3:53pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/16 "2014-09-07T15:53:23Z")

</div>

Just saw the responses. Thanks, Michael, for the workaround (syntax update). The cowsay thing was just an offhand question, as I didn’t, for the life of me, know why Ansible would start doing that all of a sudden 😛

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 7, 2014, 5:07pm UTC](https://forum.ansible.com/t/how-can-i-create-a-path-variable-containing-a-timestamp/12553/17 "2014-09-07T17:07:19Z")

</div>

It’s because cows make people happy! 🙂
