# Store Timestamp As A Constant Value

**URL:** <https://forum.ansible.com/t/store-timestamp-as-a-constant-value/35914>\
**Category:** Ansible Project\
**Created:** [October 25, 2021, 4:59am UTC](https://forum.ansible.com/t/store-timestamp-as-a-constant-value/35914 "2021-10-25T04:59:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dhiwakar\_Ravikumar](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Dhiwakar\_Ravikumar](https://forum.ansible.com/u/Dhiwakar_Ravikumar)\
**Post date:** [October 25, 2021, 4:59am UTC](https://forum.ansible.com/t/store-timestamp-as-a-constant-value/35914/1 "2021-10-25T04:59:53Z")

</div>

I’m trying to save the timestamp corresponding to when a playbook runs in a variable.  
I plan to use this variable across the playbook but I’m facing issues particularly since

the lookup plugin runs and generates a new value each time.

module\_defaults:  
group/ns.col.session:  
session\_id: “{{ lookup(‘pipe’,‘date "+%Y-%m-%d-%H%M"’) }}”

The value is looked up at the time that it is needed

I could use set\_fact: but it only works inside of the tasks: block and I’d like to set it to some value before any task can run i.e. right after hosts.

- hosts:
- localhost

module\_defaults:  
group/ns.col.session:  
session\_id: …

How do I achieve this WITHOUT using set\_fact OR without using the lookup() ?  
In other words, how to save or copy the value of a lookup to some variable ?

Thanks  
Dhiwakar

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 25, 2021, 7:40am UTC](https://forum.ansible.com/t/store-timestamp-as-a-constant-value/35914/2 "2021-10-25T07:40:58Z")

</div>

If you must do this before anything else you could provide the value as a command line extra variable to ansible-playbook

---

<div class="post-metadata">

**Author:** ![Dhiwakar\_Ravikumar](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Dhiwakar\_Ravikumar](https://forum.ansible.com/u/Dhiwakar_Ravikumar)\
**Post date:** [October 25, 2021, 8:02am UTC](https://forum.ansible.com/t/store-timestamp-as-a-constant-value/35914/3 "2021-10-25T08:02:07Z")

</div>

Thank you, so from that reply, I’m assuming there isn’t any viable alternative ?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 25, 2021, 8:09am UTC](https://forum.ansible.com/t/store-timestamp-as-a-constant-value/35914/4 "2021-10-25T08:09:28Z")

</div>

Use set\_fact as the first task in the playbook.  
You disqualified that, but tbh I don’t understand the reasoning behind that?  
Can you elaborate?

---

<div class="post-metadata">

**Author:** ![Dhiwakar\_Ravikumar](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Dhiwakar\_Ravikumar](https://forum.ansible.com/u/Dhiwakar_Ravikumar)\
**Post date:** [October 25, 2021, 8:26am UTC](https://forum.ansible.com/t/store-timestamp-as-a-constant-value/35914/5 "2021-10-25T08:26:34Z")

</div>

So, I disqualified it because in Ansible 2.12, there is an ability to define defaults for module collections, more on that [here](https://docs.ansible.com/ansible-core/devel/user_guide/playbooks_module_defaults.html#module-defaults-groups).

_In ansible-core 2.12, collections can define their own groups in the meta/runtime.yml file. module\_defaults does not take the collections keyword into account, so the fully qualified group name must be used for new groups in module\_defaults._

This means my playbook requires a value to begin with.

_module\_defaults:_  
_group/ns.col.session:_  
_session\_id:_

Now, if I’m going to use set\_fact, this is what my playbook looks like.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [October 27, 2021, 6:23pm UTC](https://forum.ansible.com/t/store-timestamp-as-a-constant-value/35914/6 "2021-10-27T18:23:01Z")

</div>

I’ve done it like this. Perhaps this will be useful to you.

```auto
- hosts: all
tasks:
- name: Snarf the playbook start time on the first host.
# We'll use this time stamp on backup files in the next play.
set_fact:
ansible_playbook_starttime: "{{ ansible_date_time.date + ' at ' + ansible_date_time.time }}"
when: ansible_play_hosts_all | first == ansible_fqdn
run_once: yes

- hosts: all
vars:
ansible_playbook_starttime: "{{ hostvars[ansible_play_hosts_all | first]['ansible_playbook_starttime'] }}"
tasks:
[...]

```
