Store Timestamp As A Constant Value

179 views
Skip to first unread message

Dhiwakar Ravikumar

unread,
Oct 25, 2021, 12:59:53 AM10/25/21
to Ansible Project
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

Dick Visser

unread,
Oct 25, 2021, 3:41:24 AM10/25/21
to ansible...@googlegroups.com
If you must do this before anything else you could provide the value as a command line extra variable to ansible-playbook

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2a0329e7-541b-4f68-aa3e-5185b42d04efn%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Dhiwakar Ravikumar

unread,
Oct 25, 2021, 4:02:07 AM10/25/21
to Ansible Project
Thank you, so from that reply, I'm assuming there isn't any viable alternative ? 

Dick Visser

unread,
Oct 25, 2021, 4:09:54 AM10/25/21
to ansible...@googlegroups.com
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?



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Dhiwakar Ravikumar

unread,
Oct 25, 2021, 4:26:34 AM10/25/21
to Ansible Project
So, I disqualified it because in Ansible 2.12, there is an ability to define defaults for module collections, more on that here.

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: <VALUE>

Now, if I'm going to use set_fact, this is what my playbook looks like.

---
- hosts:
   - localhost

  vars:
    session_fact:

  module_defaults:
    group/ns.col.session:
      session_id: "{{ session_fact }}"

  tasks:
    - name: "SETTING VALUE FOR FACT session_fact"
      set_fact:
        session_fact: "MYSESSIONVALUE"
      delegate_to: localhost
      register: RESULT

    - name: "MY MODULE 1"
       ns.col.mymodule1 :
        param1: "val1"

As you can see,  I have to create a variable called session_fact , assign it to the module default, at this point, its value is Null, then in the first set_fact task, I assign it a proper value.
Now, it does work but I'm not sure if this is the cleanest approach, naming a variable a fact.

Thanks
Dhiwakar

Todd Lewis

unread,
Oct 27, 2021, 2:23:01 PM10/27/21
to Ansible Project
I've done it like this. Perhaps this will be useful to you.
```
- 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:
   [...]
```
Reply all
Reply to author
Forward
0 new messages