I need to generate some unique id in ansible. The generation works but accessing the variable results in some "not expected" behaviour.
The playbook is very simple:
---
- hosts: localhost
vars:
- var1: "{{ 99999999 | random }}"
tasks:
- debug: msg="{{ var1 }}"
- debug: msg="{{ var1 }}"
- debug: msg="{{ var1 }}"
I expected to always have the same output but the reality is different:
ansible-playbook -i localhost setup-env-test.yml
[WARNING]: Unable to parse .... as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************
ok: [localhost]
TASK [debug] *******************************************************************************************************************************
ok: [localhost] => {
"msg": "23317042"
}
TASK [debug] *******************************************************************************************************************************
ok: [localhost] => {
"msg": "23320954"
}
TASK [debug] *******************************************************************************************************************************
ok: [localhost] => {
"msg": "96866238"
}
PLAY RECAP *********************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
I looks like the variable is reevaluated on each access.
I know that reevaluation takes places when the context changes, e.g. entering a role, but this is not the case here. I also know that by using set_fact this behaviour changes and the variable content is not evaluated again.
Can anybody give me a hint why the reevaluation is taking place? It would be nice to find the ansible documentation which explains this.