| 1 |
# test code for the changed_when parameter |
| 2 |
# (c) 2014, James Tanner <[email protected]> |
| 3 |
|
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
- name: ensure shell is always changed |
| 20 |
shell: ls -al /tmp |
| 21 |
register: shell_result |
| 22 |
|
| 23 |
- debug: var=shell_result |
| 24 |
|
| 25 |
- name: changed should always be true for shell |
| 26 |
assert: |
| 27 |
that: |
| 28 |
- "shell_result.changed" |
| 29 |
|
| 30 |
- name: test changed_when override for shell |
| 31 |
shell: ls -al /tmp |
| 32 |
changed_when: False |
| 33 |
register: shell_result |
| 34 |
|
| 35 |
- debug: var=shell_result |
| 36 |
|
| 37 |
- name: changed should be false |
| 38 |
assert: |
| 39 |
that: |
| 40 |
- "not shell_result.changed" |
| 41 |
|
| 42 |
- name: Add hosts to test group and ensure it appears as changed |
| 43 |
group_by: |
| 44 |
key: "cw_test1_{{ inventory_hostname }}" |
| 45 |
register: groupby |
| 46 |
|
| 47 |
- name: verify its changed |
| 48 |
assert: |
| 49 |
that: |
| 50 |
- groupby is changed |
| 51 |
|
| 52 |
- name: Add hosts to test group and ensure it does NOT appear as changed |
| 53 |
group_by: |
| 54 |
key: "cw_test2_{{ inventory_hostname }}" |
| 55 |
changed_when: False |
| 56 |
register: groupby |
| 57 |
|
| 58 |
- name: verify its not changed |
| 59 |
assert: |
| 60 |
that: |
| 61 |
- groupby is not changed |
| 62 |
|
| 63 |
- name: invalid conditional |
| 64 |
command: echo foo |
| 65 |
changed_when: boomboomboom |
| 66 |
register: invalid_conditional |
| 67 |
ignore_errors: true |
| 68 |
|
| 69 |
- assert: |
| 70 |
that: |
| 71 |
- invalid_conditional is failed |
| 72 |
- invalid_conditional.stdout is defined |
| 73 |
- invalid_conditional.changed_when_result is contains('boomboomboom') |
| 74 |
|
| 75 |
- add_host: |
| 76 |
name: 'host_{{item}}' |
| 77 |
loop: |
| 78 |
- 1 |
| 79 |
- 2 |
| 80 |
changed_when: item == 2 |
| 81 |
register: add_host_loop_res |
| 82 |
|
| 83 |
- assert: |
| 84 |
that: |
| 85 |
- add_host_loop_res.results[0] is not changed |
| 86 |
- add_host_loop_res.results[1] is changed |
| 87 |
- add_host_loop_res is changed |
| 88 |
|
| 89 |
- group_by: |
| 90 |
key: "test_{{ item }}" |
| 91 |
loop: |
| 92 |
- 1 |
| 93 |
- 2 |
| 94 |
changed_when: item == 2 |
| 95 |
register: group_by_loop_res |
| 96 |
|
| 97 |
- assert: |
| 98 |
that: |
| 99 |
- group_by_loop_res.results[0] is not changed |
| 100 |
- group_by_loop_res.results[1] is changed |
| 101 |
- group_by_loop_res is changed |
| 102 |
|
| 103 |
- name: use changed in changed_when |
| 104 |
add_host: |
| 105 |
name: 'host_3' |
| 106 |
changed_when: add_host_loop_res is changed |
| 107 |
register: add_host_loop_res |
| 108 |
|
| 109 |
- assert: |
| 110 |
that: |
| 111 |
- add_host_loop_res is changed |
| 112 |
|
| 113 |
- name: use an invalid changed_when on a failed task |
| 114 |
ping: |
| 115 |
data: crash |
| 116 |
register: failed_task |
| 117 |
changed_when: 1 / 0 |
| 118 |
ignore_errors: yes |
| 119 |
|
| 120 |
- name: verify the suppressed exception was preserved |
| 121 |
assert: |
| 122 |
that: |
| 123 |
- failed_task is not changed |
| 124 |
- failed_task is failed |
| 125 |
- failed_task.changed_when_result is contains "division by zero" |
| 126 |
- failed_task.changed_when_suppressed_exception is defined # RPFIX-9: FUTURE: enable traceback and verify this is the ping crash |
| 127 |
- failed_task.exception is defined # RPFIX-9: FUTURE: enable traceback and verify this is the exception from the failed changed_when expression |
| 128 |
- failed_task.msg is search "Task failed. Module failed. A 'changed_when' expression failed. Error rendering expression. division by zero" |