Reputation: 79
Let's have a playbook:
---
- name: testplaybook
hosts: 127.0.0.1
connection: local
become: yes
vars:
x: 'latest'
y: '{{x.split("-SNAPSHOT")[0]}}'
tasks:
- name: 1st debug x, y
debug:
msg: 'x={{x}}, y={{y}}'
- set_fact: x='1.0.1-SNAPSHOT'
- name: 2nd debug x, y
debug:
msg: 'x={{x}}, y={{y}}'
The output is:
TASK [1st debug x, y]
**************************************************************
task path: /var/tmp/test_ansible/testPlaybook.yml:17
ok: [127.0.0.1] => {
"msg": "x=latest, y=latest"
}
TASK [set_fact]
****************************************************************
task path: /var/tmp/test_ansible/testPlaybook.yml:20
ok: [127.0.0.1] => {"ansible_facts": {"x": "1.0.1-SNAPSHOT"}, "changed": false}
TASK [2nd debug x, y]
**********************************************************
task path: /var/tmp/test_ansible/testPlaybook.yml:21
ok: [127.0.0.1] => {
"msg": "x=1.0.1-SNAPSHOT, y=1.0.1"
}
The question is, why y is changed.
When does ansible assign values to variables and does it do a reassignment in particular cases?
Upvotes: 0
Views: 694
Reputation: 6158
You have set y
to be a string, which is {{x.split("-SNAPSHOT")[0]}}
.
Only when you actually USE the variable does all the de-referencing occur.
Upvotes: 0
Reputation: 311238
The value of y
hasn't changed. The value of y
is a Jinja template expression, {{x.split("-SNAPSHOT")[0]}}
. Ansible performs lazy evaluation of these expressions, so this is evaluated whenver you use y
. Since the value of the expression is dependent on x
, it will evaluate to a new value if you change x
.
Upvotes: 2