| 1 |
# test code for the raw module when using winrm connection |
| 2 |
# (c) 2014, Chris Church <[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: run getmac |
| 20 |
raw: getmac |
| 21 |
register: getmac_result |
| 22 |
|
| 23 |
- name: assert that getmac ran |
| 24 |
assert: |
| 25 |
that: |
| 26 |
- "getmac_result.rc == 0" |
| 27 |
- "getmac_result.stdout | length > 0" |
| 28 |
- (ansible_connection == 'ssh') | ternary(getmac_result.stderr is defined, not getmac_result.stderr) |
| 29 |
- "getmac_result is not failed" |
| 30 |
- "getmac_result is changed" |
| 31 |
|
| 32 |
- name: run ipconfig with /all argument |
| 33 |
raw: ipconfig /all |
| 34 |
register: ipconfig_result |
| 35 |
|
| 36 |
- name: assert that ipconfig ran with /all argument |
| 37 |
assert: |
| 38 |
that: |
| 39 |
- "ipconfig_result.rc == 0" |
| 40 |
- "'Physical Address' in ipconfig_result.stdout" |
| 41 |
- (ansible_connection == 'ssh') | ternary(ipconfig_result.stderr is defined, not ipconfig_result.stderr) |
| 42 |
- "ipconfig_result is not failed" |
| 43 |
- "ipconfig_result is changed" |
| 44 |
|
| 45 |
- name: run ipconfig with invalid argument |
| 46 |
raw: ipconfig /badswitch |
| 47 |
register: ipconfig_invalid_result |
| 48 |
ignore_errors: true |
| 49 |
|
| 50 |
- name: assert that ipconfig with invalid argument failed |
| 51 |
assert: |
| 52 |
that: |
| 53 |
- "ipconfig_invalid_result.rc != 0" |
| 54 |
- "ipconfig_invalid_result.stdout | length > 0" # ipconfig displays errors on stdout. |
| 55 |
# - "not ipconfig_invalid_result.stderr" |
| 56 |
- "ipconfig_invalid_result is failed" |
| 57 |
- "ipconfig_invalid_result is changed" |
| 58 |
|
| 59 |
- name: run an unknown command |
| 60 |
raw: uname -a |
| 61 |
register: unknown_result |
| 62 |
ignore_errors: true |
| 63 |
|
| 64 |
- name: assert that an unknown command failed |
| 65 |
assert: |
| 66 |
that: |
| 67 |
- "unknown_result.rc != 0" |
| 68 |
- "not unknown_result.stdout" |
| 69 |
- "unknown_result.stderr | length > 0" # An unknown command displays error on stderr. |
| 70 |
- "unknown_result is failed" |
| 71 |
- "unknown_result is changed" |
| 72 |
|
| 73 |
- name: run a command that takes longer than 60 seconds |
| 74 |
raw: Start-Sleep -s 75 |
| 75 |
register: sleep_command |
| 76 |
|
| 77 |
- name: assert that the sleep command ran |
| 78 |
assert: |
| 79 |
that: |
| 80 |
- "sleep_command.rc == 0" |
| 81 |
- "not sleep_command.stdout" |
| 82 |
- (ansible_connection == 'ssh') | ternary(sleep_command.stderr is defined, not sleep_command.stderr) |
| 83 |
- "sleep_command is not failed" |
| 84 |
- "sleep_command is changed" |
| 85 |
|
| 86 |
- name: run a raw command with key=value arguments |
| 87 |
raw: echo wwe=raw |
| 88 |
register: raw_result |
| 89 |
|
| 90 |
- name: make sure raw is really raw and not removing key=value arguments |
| 91 |
assert: |
| 92 |
that: |
| 93 |
- "raw_result.stdout_lines[0] == 'wwe=raw'" |
| 94 |
|
| 95 |
# ssh cannot pre-set the codepage so we need to do it in the command. |
| 96 |
- name: unicode tests for winrm |
| 97 |
when: ansible_connection != 'psrp' # Write-Host does not work over PSRP |
| 98 |
block: |
| 99 |
- name: run a raw command with unicode chars and quoted args (from https://github.com/ansible/ansible-modules-core/issues/1929) |
| 100 |
raw: | |
| 101 |
{{ (ansible_connection == 'ssh') | ternary("[Console]::OutputEncoding = [System.Text.Encoding]::UTF8", "") }} |
| 102 |
Write-Host --% icacls D:\somedir\ /grant "! ЗАО. Руководство":F |
| 103 |
register: raw_result2 |
| 104 |
|
| 105 |
- name: make sure raw passes command as-is and doesn't split/rejoin args |
| 106 |
assert: |
| 107 |
that: |
| 108 |
- "raw_result2.stdout_lines[0] == '--% icacls D:\\\\somedir\\\\ /grant \"! ЗАО. Руководство\":F'" |
| 109 |
|
| 110 |
- name: unicode tests for psrp |
| 111 |
when: ansible_connection == 'psrp' |
| 112 |
block: |
| 113 |
# Cannot test unicode passed into separate exec as PSRP doesn't run with a preset CP of 65001 which results in ? for unicode chars |
| 114 |
- name: run a raw command with unicode chars |
| 115 |
raw: Write-Output "! ЗАО. Руководство" |
| 116 |
register: raw_result2 |
| 117 |
|
| 118 |
- name: make sure raw passes command as-is and doesn't split/rejoin args |
| 119 |
assert: |
| 120 |
that: |
| 121 |
- "raw_result2.stdout_lines[0] == '! ЗАО. Руководство'" |
| 122 |
|
| 123 |
# Assumes MaxShellsPerUser == 30 (the default) |
| 124 |
|
| 125 |
- name: test raw + with_items to verify that winrm connection is reused for each item |
| 126 |
raw: echo "{{item}}" |
| 127 |
with_items: "{{range(32)|list}}" |
| 128 |
register: raw_with_items_result |
| 129 |
|
| 130 |
- name: check raw + with_items result |
| 131 |
assert: |
| 132 |
that: |
| 133 |
- "raw_with_items_result is not failed" |
| 134 |
- "raw_with_items_result.results|length == 32" |
| 135 |
|
| 136 |
# TODO: this test fails, since we're back to passing raw commands without modification |
| 137 |
#- name: test raw with job to ensure that preamble-free InputEncoding is working |
| 138 |
# raw: Start-Job { echo yo } | Receive-Job -Wait |
| 139 |
# register: raw_job_result |
| 140 |
# |
| 141 |
#- name: check raw with job result |
| 142 |
# assert: |
| 143 |
# that: |
| 144 |
# - raw_job_result is successful |
| 145 |
# - raw_job_result.stdout_lines[0] == 'yo' |