| 1 |
- name: Test missing playbook meta file |
| 2 |
command: ansible-playbook {{ playbook }} |
| 3 |
vars: |
| 4 |
playbook_name: missing_metadata |
| 5 |
register: result |
| 6 |
ignore_errors: True |
| 7 |
|
| 8 |
- assert: |
| 9 |
that: |
| 10 |
- result is failed |
| 11 |
- result.stderr is search("A playbook meta file is required. Considered:") |
| 12 |
- result.stderr is search(playbook_meta) |
| 13 |
vars: |
| 14 |
playbook_name: missing_metadata |
| 15 |
|
| 16 |
- name: Test configuring validate_argspec as no (False) |
| 17 |
command: ansible-playbook {{ playbook }} |
| 18 |
vars: |
| 19 |
playbook_name: test_keyword_bool_no |
| 20 |
register: result |
| 21 |
|
| 22 |
- assert: |
| 23 |
that: "{{ result.stdout is not search('Validating arguments against arg spec') }}" |
| 24 |
|
| 25 |
- name: Test configuring validate_argspec as "no" (string) |
| 26 |
command: ansible-playbook {{ playbook }} |
| 27 |
vars: |
| 28 |
playbook_name: test_keyword_str_no |
| 29 |
register: result |
| 30 |
|
| 31 |
- assert: |
| 32 |
that: "{{ result.stdout is search('Validating arguments against arg spec no') }}" |
| 33 |
|
| 34 |
- name: Test configuring argument spec validation |
| 35 |
vars: |
| 36 |
playbook_name: test_keyword |
| 37 |
block: |
| 38 |
- name: Test configuring argument spec validation |
| 39 |
command: ansible-playbook {{ playbook }} -e 'test_variable="{{ spec_name }}"' |
| 40 |
vars: |
| 41 |
spec_name: !unsafe '{{ "Argument Spec Name" }}' |
| 42 |
register: result |
| 43 |
|
| 44 |
- assert: |
| 45 |
that: "{{ result.stdout is search('Validating arguments against arg spec Argument Spec Name') }}" |
| 46 |
|
| 47 |
- name: Test turning off argument spec validation |
| 48 |
command: ansible-playbook {{ playbook }} -e 'test_variable="{{ bool_var }}"' |
| 49 |
vars: |
| 50 |
bool_var: !unsafe '{{ False }}' |
| 51 |
register: result |
| 52 |
|
| 53 |
- assert: |
| 54 |
that: "{{ result.stdout is not search('Validating arguments against arg spec') }}" |
| 55 |
|
| 56 |
- name: Test undefined configuration |
| 57 |
command: ansible-playbook {{ playbook }} -e 'test_variable="{{ undef_var }}"' |
| 58 |
vars: |
| 59 |
undef_var: !unsafe "{{ inventory_hostname != 'localhost' }}" |
| 60 |
ignore_errors: True |
| 61 |
register: result |
| 62 |
|
| 63 |
- assert: |
| 64 |
that: |
| 65 |
- result is failed |
| 66 |
- >- |
| 67 |
result.stderr is search("Error processing keyword 'validate_argspec': .* is undefined") |
| 68 |
|
| 69 |
- name: Test omitted configuration |
| 70 |
command: ansible-playbook {{ playbook }} -e 'test_variable="{{ omitted }}"' |
| 71 |
vars: |
| 72 |
omitted: !unsafe "{{ omit }}" |
| 73 |
register: result |
| 74 |
|
| 75 |
- assert: |
| 76 |
that: "{{ result.stdout is not search('Validating arguments against arg spec') }}" |
| 77 |
|
| 78 |
- name: Test configuring unknown argument spec name |
| 79 |
command: ansible-playbook {{ playbook }} -e 'test_variable="No argument spec"' |
| 80 |
ignore_errors: True |
| 81 |
register: result |
| 82 |
|
| 83 |
- assert: |
| 84 |
that: |
| 85 |
- result is failed |
| 86 |
- result.stderr is search("No argument spec named 'No argument spec' in " ~ playbook_meta) |
| 87 |
|
| 88 |
- name: Validate missing required argument |
| 89 |
command: ansible-playbook {{ playbook }} -e 'play_name="{{ play_name }}"' |
| 90 |
register: result |
| 91 |
ignore_errors: True |
| 92 |
|
| 93 |
- assert: |
| 94 |
that: |
| 95 |
- result is failed |
| 96 |
- 'result.stdout is search("missing required arguments: required_str")' |
| 97 |
|
| 98 |
- name: Validate incorrect argument choice |
| 99 |
command: ansible-playbook {{ playbook }} -e 'play_name="{{ play_name }}" required_str="" optional="fail"' |
| 100 |
register: result |
| 101 |
ignore_errors: True |
| 102 |
|
| 103 |
- assert: |
| 104 |
that: |
| 105 |
- result is failed |
| 106 |
- 'result.stdout is search("value of optional must be one of: option1, option2, got: fail")' |
| 107 |
|
| 108 |
# Test compatibility with play level fact gathering |
| 109 |
|
| 110 |
- name: Test validating play level fact gathering |
| 111 |
command: ansible-playbook {{ playbook }} |
| 112 |
vars: |
| 113 |
playbook_name: validate_facts |
| 114 |
environment: |
| 115 |
ANSIBLE_LIBRARY: "{{ role_path }}/library" |
| 116 |
register: result |
| 117 |
|
| 118 |
- assert: |
| 119 |
that: |
| 120 |
- result.stdout is search('Validating arguments against arg spec Gather Facts') |
| 121 |
- result.stdout is not search('SECRET') |
| 122 |
- result.stdout is search('\$REDACTED\$') |
| 123 |
|
| 124 |
# Test compatibility with the play name keyword |
| 125 |
|
| 126 |
- name: Test validating a vars variable play name argument spec |
| 127 |
command: ansible-playbook {{ playbook }} -e 'play_name_from_vars="{{ play_name }}" required_str=""' |
| 128 |
register: result |
| 129 |
|
| 130 |
- assert: |
| 131 |
that: "{{ result.stdout is search('Validating arguments against arg spec ' ~ play_name) }}" |
| 132 |
|
| 133 |
- name: Test a play name is required |
| 134 |
command: ansible-playbook {{ playbook }} -e 'play_name="{{ undef_var }}"' |
| 135 |
register: result |
| 136 |
ignore_errors: True |
| 137 |
loop_control: |
| 138 |
loop_var: undef_var |
| 139 |
loop: |
| 140 |
- !unsafe "{{ inventory_hostname }}" |
| 141 |
- !unsafe "{{ omit }}" |
| 142 |
- "" |
| 143 |
|
| 144 |
- assert: |
| 145 |
that: |
| 146 |
- result.results[0] is failed |
| 147 |
- >- |
| 148 |
result.results[0].stderr is search("Error processing keyword 'name': .* is undefined") |
| 149 |
- result.results[1] is failed |
| 150 |
- result.results[1].stderr is search("A play name is required when validate_argspec is True.") |
| 151 |
- result.results[2] is failed |
| 152 |
- result.results[2].stderr is search("A play name is required when validate_argspec is True.") |
| 153 |
|
| 154 |
- name: Test host pattern is not used as an argument spec name |
| 155 |
command: ansible-playbook {{ playbook }} |
| 156 |
vars: |
| 157 |
playbook_name: no_play_name |
| 158 |
register: result |
| 159 |
ignore_errors: True |
| 160 |
|
| 161 |
- assert: |
| 162 |
that: |
| 163 |
- result is failed |
| 164 |
- result.stderr is search(err) |
| 165 |
vars: |
| 166 |
err: >- |
| 167 |
A play name is required when validate_argspec is True. |
| 168 |
Alternatively, set validate_argspec to the name of an argument spec. |
| 169 |
A playbook meta file is required. Considered: |
| 170 |
|
| 171 |
# Test compatibility with the play module_defaults keyword |
| 172 |
|
| 173 |
- name: Test using module_defaults to validate arbitrary variables |
| 174 |
command: ansible-playbook {{ playbook }} -e 'nodoc="fail"' |
| 175 |
vars: |
| 176 |
playbook_name: module_defaults |
| 177 |
register: result |
| 178 |
ignore_errors: True |
| 179 |
|
| 180 |
- assert: |
| 181 |
that: |
| 182 |
- result is failed |
| 183 |
- 'result.stdout is search("nodoc. Supported parameters include: valid_argument.")' |
| 184 |
|
| 185 |
# Test compatibility with the play tags keyword |
| 186 |
|
| 187 |
- name: Test skipping the whole play, including argspec validation |
| 188 |
command: ansible-playbook {{ playbook }} --skip-tags play_level_tag |
| 189 |
vars: |
| 190 |
playbook_name: tagged_play |
| 191 |
|
| 192 |
- name: Test validation only runs when the play tag runs |
| 193 |
command: ansible-playbook {{ playbook }} --tags mismatch |
| 194 |
vars: |
| 195 |
playbook_name: tagged_play |
| 196 |
register: result |
| 197 |
|
| 198 |
- assert: |
| 199 |
that: |
| 200 |
- result.stdout is not search("Validating arguments against arg spec Tagged Play") |
| 201 |
|
| 202 |
- name: Test no_log and vault argument spec compatibility |
| 203 |
command: ansible-playbook {{ playbook }} --vault-password-file="{{ role_path }}/files/vaultpass" |
| 204 |
vars: |
| 205 |
playbook_name: no_log_argspec |
| 206 |
register: result |
| 207 |
|
| 208 |
- assert: |
| 209 |
that: |
| 210 |
- result.stdout is search("Validating arguments against arg spec Test no_log redaction") |
| 211 |
- result.stdout is not search("SECRET") |
| 212 |
- result.stdout is search("\$REDACTED\$") |