diff options
Diffstat (limited to '')
6 files changed, 222 insertions, 0 deletions
diff --git a/test/integration/targets/var_blending/roles/test_var_blending/defaults/main.yml b/test/integration/targets/var_blending/roles/test_var_blending/defaults/main.yml new file mode 100644 index 0000000..671a127 --- /dev/null +++ b/test/integration/targets/var_blending/roles/test_var_blending/defaults/main.yml @@ -0,0 +1,4 @@ +etest: "from role defaults" +role_var_beats_default: "shouldn't see this" +parameterized_beats_default: "shouldn't see this" +inventory_beats_default: "shouldn't see this" diff --git a/test/integration/targets/var_blending/roles/test_var_blending/files/foo.txt b/test/integration/targets/var_blending/roles/test_var_blending/files/foo.txt new file mode 100644 index 0000000..d51be39 --- /dev/null +++ b/test/integration/targets/var_blending/roles/test_var_blending/files/foo.txt @@ -0,0 +1,77 @@ +The value of groups_tree_var = 5000. +This comes from host, not the parents or grandparents. + +The value of the grandparent variable grandparent_var is +not overridden and is = 2000 + +The value of the parent variable is not overridden and +is = 6000 + +The variable 'overridden_in_parent' is set in the parent +and grandparent, so the parent wins. It's value is = 1000. + +The values of 'uno', 'dos', and 'tres' are set in group_vars/all but 'tres' is +set to the value of 'three' in group_vars/local, which should override it. + +uno = 1 +dos = 2 +tres = three + +The values of 'a', 'b', 'c', and 'd' are set in host_vars/local and should not +be clobbered by values that are also set in group_vars. + +a = 1 +b = 2 +c = 3 +d = 4 + +The value of 'badwolf' is set via the include_vars plugin. + +badwolf = badwolf + +The value of 'winter' is set via the main.yml in the role. + +winter = coming + +Here's an arbitrary variable set as vars_files in the playbook. + +vars_file_var = 321 + +And vars. + +vars = 123 + +Variables about other hosts can be looked up via hostvars. This includes +facts but here we'll just access a variable defined in the groups. + +999 + +Ansible has pretty basic precedence rules for variable overriding. We already have +some tests above about group order. Here are a few more. + + * -e variables always win + * then comes "most everything else" + * then comes variables defined in inventory + * then "role defaults", which are the most "defaulty" and lose in priority to everything. + +Given the above rules, here's a test that a -e variable overrides inventory, +and also defaults, and role vars. + +etest = from -e + +Now a test to make sure role variables can override inventory variables. + +role_var_beats_inventory = chevron 5 encoded + +Role variables should also beat defaults. + +role_var_beats_default = chevron 6 encoded + +But defaults are lower priority than inventory, so inventory should win. + +inventory_beats_default = narf + +That's the end of the precedence tests for now, but more are welcome. + + + diff --git a/test/integration/targets/var_blending/roles/test_var_blending/tasks/main.yml b/test/integration/targets/var_blending/roles/test_var_blending/tasks/main.yml new file mode 100644 index 0000000..f2b2e54 --- /dev/null +++ b/test/integration/targets/var_blending/roles/test_var_blending/tasks/main.yml @@ -0,0 +1,57 @@ +# test code +# (c) 2014, Michael DeHaan <michael.dehaan@gmail.com> + +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +- include_vars: more_vars.yml + +- set_fact: + output_dir: "{{ lookup('env', 'OUTPUT_DIR') }}" + +- name: deploy a template that will use variables at various levels + template: src=foo.j2 dest={{output_dir}}/foo.templated + register: template_result + +- name: copy known good into place + copy: src=foo.txt dest={{output_dir}}/foo.txt + +- name: compare templated file to known good + shell: diff {{output_dir}}/foo.templated {{output_dir}}/foo.txt + register: diff_result + +- name: verify templated file matches known good + assert: + that: + - 'diff_result.stdout == ""' + +- name: check debug variable with same name as var content + debug: var=same_value_as_var_name_var + register: same_value_as_var_name + +- name: check debug variable output when variable is undefined + debug: var=undefined_variable + register: var_undefined + +- assert: + that: + - "'VARIABLE IS NOT DEFINED!' in var_undefined.undefined_variable" + - same_value_as_var_name.same_value_as_var_name_var == 'same_value_as_var_name_var' + +- name: cleanup temporary template output + file: path={{output_dir}}/foo.templated state=absent + +- name: cleanup temporary copy + file: path={{output_dir}}/foo.txt state=absent diff --git a/test/integration/targets/var_blending/roles/test_var_blending/templates/foo.j2 b/test/integration/targets/var_blending/roles/test_var_blending/templates/foo.j2 new file mode 100644 index 0000000..10709b1 --- /dev/null +++ b/test/integration/targets/var_blending/roles/test_var_blending/templates/foo.j2 @@ -0,0 +1,77 @@ +The value of groups_tree_var = {{ groups_tree_var }}. +This comes from host, not the parents or grandparents. + +The value of the grandparent variable grandparent_var is +not overridden and is = {{ grandparent_var }} + +The value of the parent variable is not overridden and +is = {{ parent_var }} + +The variable 'overridden_in_parent' is set in the parent +and grandparent, so the parent wins. It's value is = {{ overridden_in_parent }}. + +The values of 'uno', 'dos', and 'tres' are set in group_vars/all but 'tres' is +set to the value of 'three' in group_vars/local, which should override it. + +uno = {{ uno }} +dos = {{ dos }} +tres = {{ tres }} + +The values of 'a', 'b', 'c', and 'd' are set in host_vars/local and should not +be clobbered by values that are also set in group_vars. + +a = {{ a }} +b = {{ b }} +c = {{ c }} +d = {{ d }} + +The value of 'badwolf' is set via the include_vars plugin. + +badwolf = {{ badwolf }} + +The value of 'winter' is set via the main.yml in the role. + +winter = {{ winter }} + +Here's an arbitrary variable set as vars_files in the playbook. + +vars_file_var = {{ vars_file_var }} + +And vars. + +vars = {{ vars_var }} + +Variables about other hosts can be looked up via hostvars. This includes +facts but here we'll just access a variable defined in the groups. + +{{ hostvars['testhost2']['a'] }} + +Ansible has pretty basic precedence rules for variable overriding. We already have +some tests above about group order. Here are a few more. + + * -e variables always win + * then comes "most everything else" + * then comes variables defined in inventory + * then "role defaults", which are the most "defaulty" and lose in priority to everything. + +Given the above rules, here's a test that a -e variable overrides inventory, +and also defaults, and role vars. + +etest = {{ etest }} + +Now a test to make sure role variables can override inventory variables. + +role_var_beats_inventory = {{ role_var_beats_inventory }} + +Role variables should also beat defaults. + +role_var_beats_default = {{ role_var_beats_default }} + +But defaults are lower priority than inventory, so inventory should win. + +inventory_beats_default = {{ inventory_beats_default }} + +That's the end of the precedence tests for now, but more are welcome. + + + diff --git a/test/integration/targets/var_blending/roles/test_var_blending/vars/main.yml b/test/integration/targets/var_blending/roles/test_var_blending/vars/main.yml new file mode 100644 index 0000000..1bb08bf --- /dev/null +++ b/test/integration/targets/var_blending/roles/test_var_blending/vars/main.yml @@ -0,0 +1,4 @@ +winter: coming +etest: 'from role vars' +role_var_beats_inventory: 'chevron 5 encoded' +role_var_beats_default: 'chevron 6 encoded' diff --git a/test/integration/targets/var_blending/roles/test_var_blending/vars/more_vars.yml b/test/integration/targets/var_blending/roles/test_var_blending/vars/more_vars.yml new file mode 100644 index 0000000..bac93d3 --- /dev/null +++ b/test/integration/targets/var_blending/roles/test_var_blending/vars/more_vars.yml @@ -0,0 +1,3 @@ +badwolf: badwolf + +same_value_as_var_name_var: "same_value_as_var_name_var" |