If you can use a file (defaults.yml or whatever) for only this variable, 
it's easy to check for the file's existence, write to it if it doesn't 
exist, and then just use it again and again later on. It will persist until 
removed.
# make a variable with the current date
- name: preflight
hosts: localhost
tasks:
- name: check for existing date_saved yaml file
stat:
path: ./defaults.yml
register: date_saved

- name: write current date to date.saved file
when: not date_saved.stat.exists
copy:
content: "data_dir: \"/file/to/dir_{{ now('True','%F_%H%M%S') }}\""
dest: ./defaults.yml

- name: main
hosts: localhost
vars_files:
- ./defaults.yml

tasks:
- name: echo
ansible.builtin.debug:
var: data_dir

➜  *rm defaults.yml *               
➜  *ansible-playbook ./datetime.yml*
PLAY [preflight] 
********************************************************************************************************

TASK [check for existing date_saved yaml file] 
**************************************************************************
ok: [localhost]

TASK [write current date to date.saved file] 
****************************************************************************
*changed*: [localhost]

PLAY [main] 
*************************************************************************************************************

TASK [echo] 
*************************************************************************************************************
ok: [localhost] => {
    "data_dir": "/file/to/dir_2023-03-24_021041"
}

PLAY RECAP 
**************************************************************************************************************
localhost                  : ok=5    changed=1    unreachable=0    failed=0 
   skipped=0    rescued=0    ignored=0   

➜  *ansible-playbook ./datetime.yml*
PLAY [preflight] 
********************************************************************************************************

TASK [check for existing date_saved yaml file] 
**************************************************************************
ok: [localhost]

TASK [write current date to date.saved file] 
****************************************************************************
*skipping*: [localhost]

PLAY [main] 
*************************************************************************************************************

TASK [echo] 
*************************************************************************************************************
ok: [localhost] => {
    "data_dir": "/file/to/dir_2023-03-24_021041"
}


On Thursday, March 23, 2023 at 1:19:36 PM UTC-4 [email protected] wrote:

ok that works to some extent. But I probably seem to need someting not 
toooo dynamic after all :-/ 

I did not get this entirely when we started. 

I need to create the variable value once at the beginning of the playbook 
but need to get back to it later down the road with the same initial value. 
So more like it works inside a bash script (despite the fact it's a 
playbook). 

I can think of a workaround for my current task, but still would be 
wondering whether this can be done. 

--- 
gunnar wagner | fichtestr. 1, 19386 lübz | fon: 0176 7808 9090 

-- 
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/310d23d1-60fb-4dbe-918a-663f8847b222n%40googlegroups.com.

Reply via email to