diff options
Diffstat (limited to '')
20 files changed, 260 insertions, 0 deletions
diff --git a/test/integration/targets/templating/aliases b/test/integration/targets/templating/aliases new file mode 100644 index 0000000..8278ec8 --- /dev/null +++ b/test/integration/targets/templating/aliases @@ -0,0 +1,2 @@ +shippable/posix/group3 +context/controller diff --git a/test/integration/targets/templating/tasks/main.yml b/test/integration/targets/templating/tasks/main.yml new file mode 100644 index 0000000..312e171 --- /dev/null +++ b/test/integration/targets/templating/tasks/main.yml @@ -0,0 +1,35 @@ +- command: echo {% raw %}{{ foo }}{% endraw %} + register: result + +- assert: + that: + - result.stdout_lines|first == expected + vars: + expected: !unsafe '{{ foo }}' + +- name: Assert that templating can convert JSON null, true, and false to Python + assert: + that: + - foo.null is none + - foo.true is true + - foo.false is false + vars: + # Kind of hack to just send a JSON string through jinja, by templating out nothing + foo: '{{ "" }}{"null": null, "true": true, "false": false}' + +- name: Make sure that test with name that isn't a valid Ansible plugin name does not result in a crash (1/2) + set_fact: + foo: '{{ [{"failed": false}] | selectattr("failed", "==", true) }}' + +- name: Make sure that test with name that isn't a valid Ansible plugin name does not result in a crash (2/2) + template: + src: invalid_test_name.j2 + dest: /tmp/foo + ignore_errors: true + register: result + +- assert: + that: + - result is failed + - >- + "TemplateSyntaxError: Could not load \"asdf \": 'invalid plugin name: ansible.builtin.asdf '" in result.msg diff --git a/test/integration/targets/templating/templates/invalid_test_name.j2 b/test/integration/targets/templating/templates/invalid_test_name.j2 new file mode 100644 index 0000000..98b836f --- /dev/null +++ b/test/integration/targets/templating/templates/invalid_test_name.j2 @@ -0,0 +1 @@ +{{ [{"failed": false}] | selectattr("failed", "asdf ", true) }} diff --git a/test/integration/targets/templating_lookups/aliases b/test/integration/targets/templating_lookups/aliases new file mode 100644 index 0000000..8278ec8 --- /dev/null +++ b/test/integration/targets/templating_lookups/aliases @@ -0,0 +1,2 @@ +shippable/posix/group3 +context/controller diff --git a/test/integration/targets/templating_lookups/runme.sh b/test/integration/targets/templating_lookups/runme.sh new file mode 100755 index 0000000..60b3923 --- /dev/null +++ b/test/integration/targets/templating_lookups/runme.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -eux + +ANSIBLE_LOOKUP_PLUGINS=. ANSIBLE_ROLES_PATH=./ UNICODE_VAR=café ansible-playbook runme.yml "$@" + +ansible-playbook template_lookup_vaulted/playbook.yml --vault-password-file template_lookup_vaulted/test_vault_pass "$@" + +ansible-playbook template_deepcopy/playbook.yml -i template_deepcopy/hosts "$@" diff --git a/test/integration/targets/templating_lookups/runme.yml b/test/integration/targets/templating_lookups/runme.yml new file mode 100644 index 0000000..a27337b --- /dev/null +++ b/test/integration/targets/templating_lookups/runme.yml @@ -0,0 +1,4 @@ +- hosts: localhost + gather_facts: no + roles: + - { role: template_lookups } diff --git a/test/integration/targets/templating_lookups/template_deepcopy/hosts b/test/integration/targets/templating_lookups/template_deepcopy/hosts new file mode 100644 index 0000000..ecd3b96 --- /dev/null +++ b/test/integration/targets/templating_lookups/template_deepcopy/hosts @@ -0,0 +1 @@ +h1 ansible_connection=local host_var=foo diff --git a/test/integration/targets/templating_lookups/template_deepcopy/playbook.yml b/test/integration/targets/templating_lookups/template_deepcopy/playbook.yml new file mode 100644 index 0000000..da55c16 --- /dev/null +++ b/test/integration/targets/templating_lookups/template_deepcopy/playbook.yml @@ -0,0 +1,10 @@ +- hosts: h1 + gather_facts: no + tasks: + - set_fact: + templated_foo: "{{ lookup('template', 'template.in') }}" + + - name: Test that the hostvar was templated correctly + assert: + that: + - templated_foo == "foo\n" diff --git a/test/integration/targets/templating_lookups/template_deepcopy/template.in b/test/integration/targets/templating_lookups/template_deepcopy/template.in new file mode 100644 index 0000000..77de0ad --- /dev/null +++ b/test/integration/targets/templating_lookups/template_deepcopy/template.in @@ -0,0 +1 @@ +{{hostvars['h1'].host_var}} diff --git a/test/integration/targets/templating_lookups/template_lookup_vaulted/playbook.yml b/test/integration/targets/templating_lookups/template_lookup_vaulted/playbook.yml new file mode 100644 index 0000000..23f32e8 --- /dev/null +++ b/test/integration/targets/templating_lookups/template_lookup_vaulted/playbook.yml @@ -0,0 +1,13 @@ +# https://github.com/ansible/ansible/issues/34209 +- hosts: localhost + gather_facts: no + vars: + hello_world: Hello World + tasks: + - name: Test that template lookup can handle vaulted templates + set_fact: + vaulted_hello_world: "{{ lookup('template', 'vaulted_hello.j2') }}" + + - assert: + that: + - "vaulted_hello_world|trim == 'Unvaulted Hello World!'" diff --git a/test/integration/targets/templating_lookups/template_lookup_vaulted/templates/vaulted_hello.j2 b/test/integration/targets/templating_lookups/template_lookup_vaulted/templates/vaulted_hello.j2 new file mode 100644 index 0000000..a6e98bd --- /dev/null +++ b/test/integration/targets/templating_lookups/template_lookup_vaulted/templates/vaulted_hello.j2 @@ -0,0 +1,6 @@ +$ANSIBLE_VAULT;1.1;AES256 +33623433323331343363343830343365376233386637366264646634663632343963396664393463 +3734626234626639323061643863613164643365363063310a663336663762356135396430353435 +39303930613231336135623761363130653235666433383965306235653963343166633233323638 +6635303662333734300a623063393761376531636535383164333632613839663237336463616436 +62643437623538633335366435346532636666616139386332323034336530356131 diff --git a/test/integration/targets/templating_lookups/template_lookup_vaulted/test_vault_pass b/test/integration/targets/templating_lookups/template_lookup_vaulted/test_vault_pass new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/test/integration/targets/templating_lookups/template_lookup_vaulted/test_vault_pass @@ -0,0 +1 @@ +test diff --git a/test/integration/targets/templating_lookups/template_lookups/mock_lookup_plugins/77788.py b/test/integration/targets/templating_lookups/template_lookups/mock_lookup_plugins/77788.py new file mode 100644 index 0000000..436ceaf --- /dev/null +++ b/test/integration/targets/templating_lookups/template_lookups/mock_lookup_plugins/77788.py @@ -0,0 +1,6 @@ +from ansible.plugins.lookup import LookupBase + + +class LookupModule(LookupBase): + def run(self, terms, variables, **kwargs): + return {'one': 1, 'two': 2} diff --git a/test/integration/targets/templating_lookups/template_lookups/tasks/errors.yml b/test/integration/targets/templating_lookups/template_lookups/tasks/errors.yml new file mode 100644 index 0000000..da57631 --- /dev/null +++ b/test/integration/targets/templating_lookups/template_lookups/tasks/errors.yml @@ -0,0 +1,31 @@ +- name: Task that fails due to templating error for plugin option + debug: msg="{{ 5 / 0 | int }}" + ignore_errors: true + register: result + +- assert: + that: + - result.failed + - result.exception + +- name: Loop that fails due to templating error in first entry and ignores errors + debug: msg="{{ 5 / item }}" + ignore_errors: true + register: result + loop: [0, 0, 1] + +- debug: var=result + +- assert: + that: + - result.results[0].failed + - result.results[0].exception + - result.results[0].item == 0 + + - result.results[1].failed + - result.results[1].exception + - result.results[1].item == 0 + + - not result.results[2].failed + - result.results[2].exception is undefined + - result.results[2].item == 1 diff --git a/test/integration/targets/templating_lookups/template_lookups/tasks/main.yml b/test/integration/targets/templating_lookups/template_lookups/tasks/main.yml new file mode 100644 index 0000000..430ac91 --- /dev/null +++ b/test/integration/targets/templating_lookups/template_lookups/tasks/main.yml @@ -0,0 +1,101 @@ +# UNICODE + +# https://github.com/ansible/ansible/issues/65297 +- name: get UNICODE_VAR environment var value + shell: "echo $UNICODE_VAR" + register: unicode_var_value + +- name: verify the UNICODE_VAR is defined + assert: + that: + - "unicode_var_value.stdout" + +- name: use env lookup to get UNICODE_VAR value + set_fact: + test_unicode_val: "{{ lookup('env', 'UNICODE_VAR') }}" + +- debug: var=unicode_var_value +- debug: var=test_unicode_val + +- name: compare unicode values + assert: + that: + - "test_unicode_val == unicode_var_value.stdout" + +# LOOKUP TEMPLATING + +- name: use bare interpolation + debug: msg="got {{item}}" + with_items: "{{things1}}" + register: bare_var + +- name: verify that list was interpolated + assert: + that: + - "bare_var.results[0].item == 1" + - "bare_var.results[1].item == 2" + +- name: use list with bare strings in it + debug: msg={{item}} + with_items: + - things2 + - things1 + +- name: use list with undefined var in it + debug: msg={{item}} + with_items: "{{things2}}" + ignore_errors: True + +# BUG #10073 nested template handling + +- name: set variable that clashes + set_fact: + PATH: foobar + +- name: get PATH environment var value + set_fact: + known_var_value: "{{ lookup('pipe', 'echo $PATH') }}" + +- name: do the lookup for env PATH + set_fact: + test_val: "{{ lookup('env', 'PATH') }}" + +- debug: var=test_val + +- name: compare values + assert: + that: + - "test_val != ''" + - "test_val == known_var_value" + +- name: set with_dict + shell: echo "{{ item.key + '=' + item.value }}" + with_dict: "{{ mydict }}" + +# BUG #34144 bad template caching + +- name: generate two random passwords + set_fact: + password1: "{{ lookup('password', '/dev/null length=20') }}" + password2: "{{ lookup('password', '/dev/null length=20') }}" + # If the passwords are generated randomly, the chance that they + # coincide is neglectable (< 1e-18 assuming 120 bits of randomness + # per password). + +- name: make sure passwords are not the same + assert: + that: + - password1 != password2 + +# 77788 - KeyError when wantlist=False with dict returned +- name: Test that dicts can be parsed with wantlist false + set_fact: + dict_wantlist_true: "{{ lookup('77788', wantlist=True) }}" + dict_wantlist_false: "{{ lookup('77788', wantlist=False) }}" + +- assert: + that: + - dict_wantlist_true is mapping + - dict_wantlist_false is string + +- include_tasks: ./errors.yml diff --git a/test/integration/targets/templating_lookups/template_lookups/vars/main.yml b/test/integration/targets/templating_lookups/template_lookups/vars/main.yml new file mode 100644 index 0000000..4c44b1c --- /dev/null +++ b/test/integration/targets/templating_lookups/template_lookups/vars/main.yml @@ -0,0 +1,9 @@ +mydict: + mykey1: myval1 + mykey2: myval2 +things1: + - 1 + - 2 +things2: + - "{{ foo }}" + - "{{ foob | default('') }}" diff --git a/test/integration/targets/templating_settings/aliases b/test/integration/targets/templating_settings/aliases new file mode 100644 index 0000000..1d28bdb --- /dev/null +++ b/test/integration/targets/templating_settings/aliases @@ -0,0 +1,2 @@ +shippable/posix/group5 +context/controller diff --git a/test/integration/targets/templating_settings/dont_warn_register.yml b/test/integration/targets/templating_settings/dont_warn_register.yml new file mode 100644 index 0000000..277ce78 --- /dev/null +++ b/test/integration/targets/templating_settings/dont_warn_register.yml @@ -0,0 +1,6 @@ +- hosts: testhost + gather_facts: false + tasks: + - name: template in register warns, but no template should not + debug: msg=unimportant + register: thisshouldnotwarn diff --git a/test/integration/targets/templating_settings/runme.sh b/test/integration/targets/templating_settings/runme.sh new file mode 100755 index 0000000..2fb202c --- /dev/null +++ b/test/integration/targets/templating_settings/runme.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +set -eux + +ansible-playbook test_templating_settings.yml -i ../../inventory -v "$@" +[ "$(ansible-playbook dont_warn_register.yml -i ../../inventory -v "$@" 2>&1| grep -c 'is not templatable, but we found')" == "0" ] diff --git a/test/integration/targets/templating_settings/test_templating_settings.yml b/test/integration/targets/templating_settings/test_templating_settings.yml new file mode 100644 index 0000000..0c024df --- /dev/null +++ b/test/integration/targets/templating_settings/test_templating_settings.yml @@ -0,0 +1,14 @@ +--- +- name: 'Test templating in name' + hosts: testhost + vars: + a_list: + - 'part' + - 'of a' + - 'name' + + tasks: + # Note: this only tests that we do not traceback. It doesn't test that the + # name goes through templating correctly + - name: 'Task: {{ a_list | to_json }}' + debug: msg='{{ a_list | to_json }}' |