You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.0 KiB
49 lines
1.0 KiB
---
|
|
|
|
- name: Show how ansible defer variable evaluation to the last moment
|
|
gather_facts: no
|
|
hosts: localhost
|
|
vars:
|
|
output: '{{ input }}'
|
|
tasks:
|
|
- set_fact:
|
|
input: 'dummy'
|
|
- set_fact:
|
|
input: 'foo'
|
|
- debug:
|
|
var: output
|
|
- assert:
|
|
that:
|
|
# "output" contains the value of "input" at the time it has been used
|
|
- output == 'foo'
|
|
- set_fact:
|
|
input: bar
|
|
- debug:
|
|
var: output
|
|
- assert:
|
|
that:
|
|
- output == 'bar'
|
|
|
|
- name: But the value of a fact is computed during assignation
|
|
gather_facts: no
|
|
hosts: localhost
|
|
tasks:
|
|
- set_fact:
|
|
input: test1
|
|
# Now "output" is not a variable but a fact
|
|
- set_fact:
|
|
output: '{{ input }}'
|
|
- debug:
|
|
var: output
|
|
- assert:
|
|
that:
|
|
# "output" contains the value of "input" at the time it has been defined
|
|
- output == 'test1'
|
|
- set_fact:
|
|
input: test2
|
|
- debug:
|
|
var: output
|
|
- assert:
|
|
that:
|
|
# "output" contains the value of "input" at the time it has been defined
|
|
- output == 'test1'
|
|
|