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:
[...]
```
On Monday, October 25, 2021 at 4:26:34 AM UTC-4 [email protected] wrote:
> 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: <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
> On Monday, October 25, 2021 at 1:39:54 PM UTC+5:30 [email protected]
> wrote:
>
>> 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?
>>
>> On Mon, 25 Oct 2021 at 10:02, Dhiwakar Ravikumar <[email protected]>
>> wrote:
>>
>>> Thank you, so from that reply, I'm assuming there isn't any viable
>>> alternative ?
>>>
>>> On Monday, October 25, 2021 at 1:11:24 PM UTC+5:30 [email protected]
>>> wrote:
>>>
>>>> If you must do this before anything else you could provide the value as
>>>> a command line extra variable to ansible-playbook
>>>>
>>>> On Mon, 25 Oct 2021 at 07:00, Dhiwakar Ravikumar <[email protected]>
>>>> wrote:
>>>>
>>>>> 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
>>>>>
>>>>> --
>>>>> 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 [email protected].
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/ansible-project/2a0329e7-541b-4f68-aa3e-5185b42d04efn%40googlegroups.com
>>>>>
>>>>> <https://groups.google.com/d/msgid/ansible-project/2a0329e7-541b-4f68-aa3e-5185b42d04efn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>>>> Sent from a mobile device - please excuse the brevity, spelling and
>>>> punctuation.
>>>>
>>> --
>>> 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 [email protected].
>>>
>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ansible-project/a1294e55-8e2b-4ac4-a628-e6331ac454d8n%40googlegroups.com
>>>
>>> <https://groups.google.com/d/msgid/ansible-project/a1294e55-8e2b-4ac4-a628-e6331ac454d8n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>>
>> --
>> Dick Visser
>> Trust & Identity Service Operations Manager
>> GÉANT
>>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/173c51e8-5c03-40af-ae56-061822c6cd13n%40googlegroups.com.