| 1 |
# DTFIX-FUTURE: de-dupe with tests in play.yml that are not warning/output-dependent |
| 2 |
|
| 3 |
- hosts: localhost |
| 4 |
gather_facts: no |
| 5 |
name: tests for indirect conditional templates |
| 6 |
vars: |
| 7 |
int_var: 5 |
| 8 |
str_var: hi mom |
| 9 |
untrusted_template_looking_var: !unsafe '{{ "not " + "a " + "template" }}' |
| 10 |
list_of_true_expressions: |
| 11 |
- 1 == 1 |
| 12 |
- '{{ 1 == 1 }}' |
| 13 |
list_of_false_expressions: |
| 14 |
- 1 == 0 |
| 15 |
list_of_false_templates: |
| 16 |
- '{{ 1 == 0 }}' |
| 17 |
tasks: |
| 18 |
- name: assert test for inline list of true template expressions |
| 19 |
assert: |
| 20 |
that: |
| 21 |
- 1 == 1 |
| 22 |
- '{{ 1 == 1 }}' |
| 23 |
|
| 24 |
- name: assert test for indirect true expressions |
| 25 |
assert: |
| 26 |
that: '{{ list_of_true_expressions }}' |
| 27 |
|
| 28 |
- name: invoke assert via FQCN |
| 29 |
ansible.builtin.assert: |
| 30 |
that: '{{ 1 + 1 == 2}}' |
| 31 |
|
| 32 |
- name: invoke assert via action keyword |
| 33 |
action: |
| 34 |
module: ansible.legacy.assert |
| 35 |
args: |
| 36 |
that: '{{ 1 + 1 == 2}}' |
| 37 |
|
| 38 |
- name: assert true template via kv |
| 39 |
action: ansible.legacy.assert that='{{ 1 + 1 == 2}}' |
| 40 |
|
| 41 |
- name: assert false template via kv |
| 42 |
action: ansible.legacy.assert that='{{ 1 + 1 == 0}}' |
| 43 |
register: result |
| 44 |
failed_when: result is success |
| 45 |
|
| 46 |
- name: assert true template via templated arg dict |
| 47 |
assert: '{{ my_args }}' |
| 48 |
vars: |
| 49 |
my_args: |
| 50 |
that: 1 == 1 |
| 51 |
|
| 52 |
# DTFIX-FUTURE: this test is really asserting stuff about action arg canonicalization and should be moved $elsewhere |
| 53 |
# but for now we want to be able to verify that the warning occurred |
| 54 |
- name: ensure that actions can accept their entire arg dict as a template (with a warning if INJECT_FACTS_AS_VARS is enabled) |
| 55 |
debug: '{{ debug_args }}' |
| 56 |
vars: |
| 57 |
debug_args: |
| 58 |
msg: '{{ "hello from an embedded template" }}' |
| 59 |
register: result |
| 60 |
failed_when: result.msg != "hello from an embedded template" |
| 61 |
|
| 62 |
- name: assert false scalar expression via templated dict of task args |
| 63 |
assert: '{{ my_args }}' |
| 64 |
vars: |
| 65 |
my_args: |
| 66 |
that: 1 == 0 |
| 67 |
register: result |
| 68 |
failed_when: result is success |
| 69 |
|
| 70 |
- name: assert list of false scalar expressions via templated dict of task args |
| 71 |
assert: '{{ my_args }}' |
| 72 |
vars: |
| 73 |
my_args: |
| 74 |
that: |
| 75 |
- 1 == 0 |
| 76 |
register: result |
| 77 |
failed_when: result is success |
| 78 |
|
| 79 |
|
| 80 |
# DTFIX-FUTURE: indirected expressions like these never worked (truthy-only expression tests masked the failure). |
| 81 |
# Convert these to tests that assert a broken conditional error. |
| 82 |
# |
| 83 |
# - name: assert false scalar expression as loop arg |
| 84 |
# assert: |
| 85 |
# that: item |
| 86 |
# loop: |
| 87 |
# - 1 == 0 |
| 88 |
# register: result |
| 89 |
# failed_when: result is success |
| 90 |
# |
| 91 |
# - name: assert list of true expressions as loop arg |
| 92 |
# assert: |
| 93 |
# that: item |
| 94 |
# loop: |
| 95 |
# - [1 == 1] |
| 96 |
# |
| 97 |
# - name: assert list of false expressions as loop arg |
| 98 |
# assert: |
| 99 |
# that: item |
| 100 |
# loop: |
| 101 |
# - [1 == 0] |
| 102 |
# register: result |
| 103 |
# failed_when: result is success |
| 104 |
# |
| 105 |
# - name: assert true scalar expression as loop arg |
| 106 |
# assert: |
| 107 |
# that: item |
| 108 |
# loop: |
| 109 |
# - 1 == 1 |
| 110 |
|
| 111 |
- name: assert test for indirect false expression (EXPECTED FAILURE) |
| 112 |
assert: |
| 113 |
that: '{{ list_of_false_expressions }}' |
| 114 |
register: bang |
| 115 |
failed_when: bang is not failed |
| 116 |
|
| 117 |
- name: assert test for indirect false template (EXPECTED FAILURE) |
| 118 |
assert: |
| 119 |
that: '{{ list_of_false_templates }}' |
| 120 |
register: bang |
| 121 |
failed_when: bang is not failed |
| 122 |
|
| 123 |
- hosts: localhost |
| 124 |
gather_facts: no |
| 125 |
name: tests for playbook object conditionals |
| 126 |
tasks: |
| 127 |
- name: test false plain boolean conditional expression |
| 128 |
fail: |
| 129 |
when: 1 == 0 |
| 130 |
|
| 131 |
- name: test false templated boolean conditional expression |
| 132 |
fail: |
| 133 |
when: '{{ 1 == 0 }}' |
| 134 |
|
| 135 |
- name: test true plain boolean conditional expression |
| 136 |
debug: |
| 137 |
register: true_plain |
| 138 |
when: 1 == 1 |
| 139 |
|
| 140 |
- name: test true templated boolean conditional expression |
| 141 |
debug: |
| 142 |
register: true_templated |
| 143 |
when: '{{ 1 == 1 }}' |
| 144 |
|
| 145 |
- name: validate true results |
| 146 |
assert: |
| 147 |
that: |
| 148 |
- true_plain is not skipped |
| 149 |
- true_templated is not skipped |
| 150 |
|
| 151 |
- hosts: localhost |
| 152 |
gather_facts: no |
| 153 |
name: tests for conditionals in actions |
| 154 |
tasks: |
| 155 |
- name: smoke-test failed_when before we rely on it |
| 156 |
debug: |
| 157 |
failed_when: false |
| 158 |
|
| 159 |
- name: smoke-test failed_when before we rely on it |
| 160 |
debug: |
| 161 |
failed_when: true |
| 162 |
ignore_errors: true |
| 163 |
register: result |
| 164 |
|
| 165 |
- name: assert failed_when behavior |
| 166 |
assert: |
| 167 |
that: result is failed |
| 168 |
|
| 169 |
- name: test false plain boolean conditional expression |
| 170 |
assert: |
| 171 |
that: 1 == 0 |
| 172 |
register: result |
| 173 |
failed_when: result is success |
| 174 |
|
| 175 |
- name: test false templated boolean conditional expression |
| 176 |
assert: |
| 177 |
that: '{{ 1 == 0 }}' |
| 178 |
register: result |
| 179 |
failed_when: result is success |
| 180 |
|
| 181 |
- name: test true plain boolean conditional expression |
| 182 |
assert: |
| 183 |
that: 1 == 1 |
| 184 |
|
| 185 |
- name: test true templated boolean conditional expression |
| 186 |
assert: |
| 187 |
that: '{{ 1 == 1 }}' |
| 188 |
|
| 189 |
|
| 190 |
# Ideally we'd use failed_when in place of two-tasks w/ assert, but due to an impl-detail (possibly bug), changed/failed_when |
| 191 |
# are not evaluated if the task failed while evaluating a `when` conditional. |
| 192 |
- name: untrusted playbook conditionals should hard fail (EXPECTED FAILURE) |
| 193 |
debug: |
| 194 |
when: !unsafe 1 == 0 |
| 195 |
ignore_errors: true |
| 196 |
register: result |
| 197 |
|
| 198 |
- name: assert failed as expected |
| 199 |
assert: |
| 200 |
that: |
| 201 |
- result is failed |
| 202 |
- result.msg is contains 'untrusted template or expression' |
| 203 |
|
| 204 |
- name: untrusted assert conditional list item indirected through template should hard fail (EXPECTED FAILURE) |
| 205 |
vars: |
| 206 |
assert_expr_with_untrusted_value: |
| 207 |
- true |
| 208 |
- !unsafe 1 == 0 |
| 209 |
assert: |
| 210 |
that: '{{ assert_expr_with_untrusted_value }}' |
| 211 |
ignore_errors: true |
| 212 |
register: result |
| 213 |
|
| 214 |
- name: assert failed as expected |
| 215 |
assert: |
| 216 |
that: |
| 217 |
- result is failed |
| 218 |
- result.msg is contains 'untrusted template or expression' |
| 219 |
|
| 220 |
- name: test inline constant template in an optimizable expression |
| 221 |
assert: |
| 222 |
that: | |
| 223 |
"{{'hi'}}" == "hi" |