# Utilizing timestamps within filenames

**URL:** <https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232>\
**Category:** Ansible Project\
**Created:** [April 21, 2016, 3:45pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232 "2016-04-21T15:45:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Louthan](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Joe\_Louthan](https://forum.ansible.com/u/Joe_Louthan)\
**Post date:** [April 21, 2016, 3:45pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/1 "2016-04-21T15:45:53Z")

</div>

Hello,

In my deployments, I tend to do db backups and dump them into filenames containing a timestamp like so:

` mysqldump dbname | gzip -9c > ~/dbname.`date +%Y-%m-%d.%H%M`.sql.gz ; ls -la ~/ | grep `date +%Y-%m-%d`

`

As you can see, when the dump is done, I want to see the resulting file just to eyeball it and make everything looks right.

(To answer future questions: we date time stamp our backup files so that we can keep them for an extended period of time.)

I would like to write something like this:

`

- hosts: dbserver  
remote\_user: jlouthan  
tasks:

- name: Backup Prod Database  
shell: mysqldump dbdump | gzip -9c \> ~jlouthan/dbdump.`date +%Y-%m-%d`.sql.gz  
become: yes  
become\_method: sudo

- name: Check to see if the dbdump has been successfully created  
wait\_for: path=~jlouthan/dbdump.`date +%Y-%m-%d`.sql.gz

`

Is there any chance that Ansible would be able to pick up on backticks or is there a better way?

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [April 21, 2016, 9:41pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/2 "2016-04-21T21:41:29Z")

</div>

Hi Joe,

you could add a task

- name: Get current timestamp  
&nbsp;&nbsp;local\_action: command bash -c "date +%Y-%m-%d.%H%M"  
&nbsp;&nbsp;register: now  
&nbsp;&nbsp;run\_once: true

(or something similar adapted to your local environment) before your  
dump task and then use {{ now }} in the next tasks to always get the  
same timestamp.

Best,  
Felix

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 22, 2016, 2:21am UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/3 "2016-04-22T02:21:14Z")

</div>

You can use a lookup or create a var based on it:

​datestring: ​"{{lookup(‘pipe’, “date +%Y-%m-%d.%H%M”)}}"

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [April 22, 2016, 6:42pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/4 "2016-04-22T18:42:58Z")

</div>

Apart form the other answers, I avoid using backticks and use the  
$(date ...) syntax instead (a leading dollar sign and brackets instead  
of backticks). Have you tried this?

Johannes

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 22, 2016, 6:58pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/5 "2016-04-22T18:58:21Z")

</div>

$() depends on shell, I prefer it as it is stackable and cleaner than the backticks, but not always available.

---

<div class="post-metadata">

**Author:** ![Joe\_Louthan](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Joe\_Louthan](https://forum.ansible.com/u/Joe_Louthan)\
**Post date:** [April 22, 2016, 8:11pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/6 "2016-04-22T20:11:50Z")

</div>

I ran this:

`

- hosts: dbserver  
remote\_user: jlouthan  
tasks:

- name: Get current timestamp  
local\_action: command bash -c “date +%Y-%m-%d.%H%M”  
register: timestamp  
run\_once: true

- debug: var=timestamp.stdout

- name: Get current datestamp  
local\_action: command bash -c “date +%Y-%m-%d”  
register: datestamp  
run\_once: true

- debug: var=datestamp.stdout

- name: Backup Stag Database  
mysql\_db: state=dump name=dbname target=~jlouthan/dbname.{{ datestamp }}.sql  
become: yes  
become\_method: sudo

- name: See the the Stag DB backup  
shell: ls -la ~jlouthan/ | grep {{ timestamp }}  
register: dumpstag

- debug: var=dumpstag.stdout\_lines

- name: Make sure that the Stag DB Backups look good before you continue  
pause:

`

And got this:

`

[jlouthan@ansible-server Playbooks]$ ansible-playbook test-backup-Client-staging-db.yml  
SUDO password:

PLAY [dbserver] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

GATHERING FACTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [dbserver]

TASK: [Get current timestamp] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [dbserver → 127.0.0.1]

TASK: [debug var=timestamp.stdout] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [dbserver] =\> {  
“var”: {  
“timestamp.stdout”: “2016-04-22.1507”  
}  
}

TASK: [Get current datestamp] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [dbserver → 127.0.0.1]

TASK: [debug var=datestamp.stdout] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [dbserver] =\> {  
“var”: {  
“datestamp.stdout”: “2016-04-22”  
}  
}

TASK: [Backup Stag Database] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [dbserver] =\> {“failed”: true}  
msg: this module requires key=value arguments ([‘state=dump’, ‘name=dbname’, ‘target=~jlouthan/dbname.{changed:’, ‘True,’, ‘end:’, ‘2016-04-22 15:07:30.816204,’, ‘stdout:’, ‘2016-04-22,’, ‘cmd:’, ‘[bash,’, ‘-c,’, ‘date +%Y-%m-%d],’, ‘rc:’, ‘0,’, ‘start:’, ‘2016-04-22 15:07:30.810278,’, ‘stderr:’, ‘,’, ‘delta:’, ‘0:00:00.005926,’, ‘invocation:’, ‘{module\_name:’, ‘ucommand,’, ‘module\_complex\_args:’, ‘{},’, ‘module\_args:’, ‘ubash -c “date +%Y-%m-%d”},’, ‘stdout\_lines:’, ‘[2016-04-22],’, ‘warnings:’, ‘}.sql’])

FATAL: all hosts have already failed – aborting

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
to retry, use: --limit @/home/jlouthan/test-backup-Client-staging-db.retry

dbserver : ok=5 changed=2 unreachable=0 failed=1

`

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 22, 2016, 8:20pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/7 "2016-04-22T20:20:38Z")

</div>

that the module name in the error is ‘ucommand’ and the spacing you posted, lends me to believe you are not matching the error to the actual play.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [April 22, 2016, 9:00pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/8 "2016-04-22T21:00:35Z")

</div>

Hi Joe,

> I ran this:  
> - hosts: dbserver  
> &nbsp;&nbsp;remote\_user: jlouthan  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Get current timestamp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local\_action: command bash -c "date +%Y-%m-%d.%H%M"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: timestamp  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: var=timestamp.stdout

as you correctly note here, the variable you need is not timestamp, but  
timestamp.stdout.

(I forgot the .stdout in my original mail.)

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Backup Stag Database  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mysql\_db: state=dump name=dbname target=~jlouthan/dbname.{{  
> datestamp }}.sql

Here you have to write {{ datestamp.stdout }} instead of  
{{ datestamp }}; otherwise, instead of just the datestamp, the whole  
dict will be appended, which results in the error message you get below.

> TASK: [Backup Stag Database]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> failed: [dbserver] =\> {"failed": true}  
> msg: this module requires key=value arguments (['state=dump',  
> 'name=dbname', 'target=~jlouthan/dbname.{changed:', 'True,', 'end:',  
> '2016-04-22 15:07:30.816204,', 'stdout:', '2016-04-22,', 'cmd:',  
> '[bash,', '-c,', 'date +%Y-%m-%d],', 'rc:', '0,', 'start:',  
> '2016-04-22 15:07:30.810278,', 'stderr:', ',', 'delta:',  
> '0:00:00.005926,', 'invocation:', '{module\_name:', 'ucommand,',  
> 'module\_complex\_args:', '{},', 'module\_args:', 'ubash -c "date  
> +%Y-%m-%d"},', 'stdout\_lines:', '[2016-04-22],', 'warnings:',  
> '}.sql'])

Here you can see that {{ datestamp }} was replaced by:

{changed:', 'True,', 'end:', '2016-04-22 15:07:30.816204,', 'stdout:',  
'2016-04-22,', 'cmd:', '[bash,', '-c,', 'date +%Y-%m-%d],', 'rc:',  
'0,', 'start:', '2016-04-22 15:07:30.810278,', 'stderr:', ',',  
'delta:', '0:00:00.005926,', 'invocation:', '{module\_name:',  
'ucommand,', 'module\_complex\_args:', '{},', 'module\_args:', 'ubash -c  
"date +%Y-%m-%d"}

which is NOT what you want 🙂

Best,  
Felix

---

<div class="post-metadata">

**Author:** ![Joe\_Louthan](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@Joe\_Louthan](https://forum.ansible.com/u/Joe_Louthan)\
**Post date:** [April 25, 2016, 1:40pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/9 "2016-04-25T13:40:22Z")

</div>

That’s it! Thank you!

To avoid being “that guy”, here is the working playbook:

`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 25, 2016, 1:53pm UTC](https://forum.ansible.com/t/utilizing-timestamps-within-filenames/21232/10 "2016-04-25T13:53:27Z")

</div>

Also, just as an FYI, this can be achieved without calling the date command:
