diff --git a/variable-scope.yaml b/variable-scope.yaml new file mode 100644 index 0000000..0e10f61 --- /dev/null +++ b/variable-scope.yaml @@ -0,0 +1,21 @@ +--- + +- name: Variables defined on tasks are local to those tasks + gather_facts: no + hosts: localhost + vars: + check: outter + tasks: + - block: + - debug: + var: check + - assert: + that: + - check == 'inner' + vars: + check: inner + - debug: + var: check + - assert: + that: + - check == 'outter' diff --git a/variables-and-fact.yaml b/variables-and-fact.yaml new file mode 100644 index 0000000..b2ad602 --- /dev/null +++ b/variables-and-fact.yaml @@ -0,0 +1,49 @@ +--- + +- 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'