mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-22 20:13:59 -07:00
Split out var_blending test into targets dir. (#17996)
This commit is contained in:
parent
410b96d716
commit
374e4348e4
16 changed files with 35 additions and 18 deletions
|
@ -0,0 +1,2 @@
|
|||
output_dir: .
|
||||
etest: 'from -e'
|
|
@ -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"
|
|
@ -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.
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
# 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
|
||||
|
||||
- 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:
|
||||
- var_undefined.undefined_variable == 'VARIABLE IS NOT DEFINED!'
|
||||
- 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
|
|
@ -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.
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
winter: coming
|
||||
etest: 'from role vars'
|
||||
role_var_beats_inventory: 'chevron 5 encoded'
|
||||
role_var_beats_default: 'chevron 6 encoded'
|
|
@ -0,0 +1,3 @@
|
|||
badwolf: badwolf
|
||||
|
||||
same_value_as_var_name_var: "same_value_as_var_name_var"
|
5
test/integration/targets/var_blending/runme.sh
Executable file
5
test/integration/targets/var_blending/runme.sh
Executable file
|
@ -0,0 +1,5 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -eux
|
||||
|
||||
ansible-playbook test_var_blending.yml -i ../../inventory -e @integration_config.yml -v "$@"
|
|
@ -0,0 +1,8 @@
|
|||
- hosts: testhost
|
||||
vars_files:
|
||||
- vars_file.yml
|
||||
vars:
|
||||
vars_var: 123
|
||||
gather_facts: True
|
||||
roles:
|
||||
- { role: test_var_blending, parameterized_beats_default: 1234, tags: test_var_blending }
|
12
test/integration/targets/var_blending/vars_file.yml
Normal file
12
test/integration/targets/var_blending/vars_file.yml
Normal file
|
@ -0,0 +1,12 @@
|
|||
# this file is here to support testing vars_files in the blending tests only.
|
||||
# in general define test data in the individual role:
|
||||
# roles/role_name/vars/main.yml
|
||||
|
||||
foo: "Hello"
|
||||
things1:
|
||||
- 1
|
||||
- 2
|
||||
things2:
|
||||
- "{{ foo }}"
|
||||
- "{{ foob | default('') }}"
|
||||
vars_file_var: 321
|
Loading…
Add table
Add a link
Reference in a new issue