2016-09-15 18:25:56 +00:00
|
|
|
- name: execute a powershell cmdlet
|
|
|
|
win_shell: Write-Output "hello from Ansible"
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is successful
|
|
|
|
- shellout is changed
|
2016-09-15 18:25:56 +00:00
|
|
|
- shellout.cmd == 'Write-Output "hello from Ansible"'
|
|
|
|
- shellout.delta is match('^\d:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.end is match('^(\d){4}\-(\d){2}\-(\d){2} (\d){2}:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.rc == 0
|
|
|
|
- shellout.start is match('^(\d){4}\-(\d){2}\-(\d){2} (\d){2}:(\d){2}:(\d){2}.(\d){6}$')
|
2017-01-25 03:57:10 +00:00
|
|
|
# assertion disabled since it does not pass on Windows Server 2016
|
|
|
|
# - shellout.stderr == ""
|
2016-09-15 18:25:56 +00:00
|
|
|
- shellout.stdout == "hello from Ansible\r\n"
|
|
|
|
- shellout.stdout_lines == ["hello from Ansible"]
|
|
|
|
|
2017-03-24 00:48:15 +00:00
|
|
|
- name: execute a powershell cmdlet with multi-line output that uses environment with embedded quotes
|
|
|
|
win_shell: Write-Output "hello from Ansible"; Write-Output "another line"; Write-Output "yet another line"; Write-Output "envvar was $env:taskvar"
|
|
|
|
environment:
|
|
|
|
taskvar: "o'doyle rules"
|
2016-09-15 18:25:56 +00:00
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is successful
|
|
|
|
- shellout is changed
|
2017-03-24 00:48:15 +00:00
|
|
|
- shellout.cmd == 'Write-Output "hello from Ansible"; Write-Output "another line"; Write-Output "yet another line"; Write-Output "envvar was $env:taskvar"'
|
2016-09-15 18:25:56 +00:00
|
|
|
- shellout.delta is match('^\d:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.end is match('^(\d){4}\-(\d){2}\-(\d){2} (\d){2}:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.rc == 0
|
|
|
|
- shellout.start is match('^(\d){4}\-(\d){2}\-(\d){2} (\d){2}:(\d){2}:(\d){2}.(\d){6}$')
|
2017-01-25 03:57:10 +00:00
|
|
|
# assertion disabled since it does not pass on Windows Server 2016
|
|
|
|
# - shellout.stderr == ""
|
2017-03-24 00:48:15 +00:00
|
|
|
- shellout.stdout == "hello from Ansible\r\nanother line\r\nyet another line\r\nenvvar was o'doyle rules\r\n"
|
|
|
|
- shellout.stdout_lines == ["hello from Ansible","another line", "yet another line", "envvar was o'doyle rules"]
|
2016-09-15 18:25:56 +00:00
|
|
|
|
|
|
|
- name: execute something nonexistent
|
|
|
|
win_shell: bogus_command1234
|
|
|
|
register: shellout
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is failed
|
2017-07-05 17:05:07 +00:00
|
|
|
- shellout.failed == true # check the failure key explicitly, since failed does magic with RC
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is changed
|
2016-09-15 18:25:56 +00:00
|
|
|
- shellout.cmd == 'bogus_command1234'
|
|
|
|
- shellout.delta is match('^\d:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.end is match('^(\d){4}\-(\d){2}\-(\d){2} (\d){2}:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.rc == 1
|
|
|
|
- shellout.start is match('^(\d){4}\-(\d){2}\-(\d){2} (\d){2}:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.stderr is search('not recognized')
|
|
|
|
- shellout.stdout == ""
|
|
|
|
- shellout.stdout_lines == []
|
|
|
|
|
|
|
|
- name: execute something with error output
|
|
|
|
win_shell: Write-Error "it broke"; Write-Output "some output"
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is successful
|
|
|
|
- shellout is changed
|
2016-09-15 18:25:56 +00:00
|
|
|
- shellout.cmd == 'Write-Error "it broke"; Write-Output "some output"'
|
|
|
|
- shellout.delta is match('^\d:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.end is match('^(\d){4}\-(\d){2}\-(\d){2} (\d){2}:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.rc == 0
|
|
|
|
- shellout.start is match('^(\d){4}\-(\d){2}\-(\d){2} (\d){2}:(\d){2}:(\d){2}.(\d){6}$')
|
|
|
|
- shellout.stderr is search('it broke')
|
|
|
|
- shellout.stdout == "some output\r\n"
|
|
|
|
- shellout.stdout_lines == ["some output"]
|
|
|
|
|
|
|
|
- name: ensure test file is absent
|
|
|
|
win_file:
|
|
|
|
path: c:\testfile.txt
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: run with creates, should create
|
|
|
|
win_shell: echo $null >> c:\testfile.txt
|
|
|
|
args:
|
|
|
|
creates: c:\testfile.txt
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is successful
|
|
|
|
- shellout is changed
|
2016-09-15 18:25:56 +00:00
|
|
|
|
|
|
|
- name: run again with creates, should skip
|
|
|
|
win_shell: echo $null >> c:\testfile.txt
|
|
|
|
args:
|
|
|
|
creates: c:\testfile.txt
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is skipped
|
2016-09-15 18:25:56 +00:00
|
|
|
- shellout.msg is search('exists')
|
|
|
|
|
2018-01-26 00:58:49 +00:00
|
|
|
- name: get path of pagefile
|
|
|
|
win_shell: |
|
|
|
|
$pagefile = $null
|
|
|
|
$cs = Get-CimInstance -ClassName Win32_ComputerSystem
|
|
|
|
if ($cs.AutomaticManagedPagefile) {
|
|
|
|
$pagefile = "$($env:SystemRoot.Substring(0, 1)):\pagefile.sys"
|
|
|
|
} else {
|
|
|
|
$pf = Get-CimInstance -ClassName Win32_PageFileSetting
|
|
|
|
if ($pf -ne $null) {
|
|
|
|
$pagefile = $pf[0].Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$pagefile
|
|
|
|
register: pagefile_path
|
|
|
|
|
2017-11-16 00:04:03 +00:00
|
|
|
- name: test creates with hidden system file, should skip
|
|
|
|
win_shell: echo test
|
|
|
|
args:
|
2018-01-26 00:58:49 +00:00
|
|
|
creates: '{{pagefile_path.stdout_lines[0]}}'
|
2017-11-16 00:04:03 +00:00
|
|
|
register: shellout
|
2018-01-26 00:58:49 +00:00
|
|
|
when: pagefile_path.stdout_lines|count != 0
|
2017-11-16 00:04:03 +00:00
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is skipped
|
2017-11-16 00:04:03 +00:00
|
|
|
- shellout.msg is search('exists')
|
2018-01-26 00:58:49 +00:00
|
|
|
when: pagefile_path.stdout_lines|count != 0
|
2017-11-16 00:04:03 +00:00
|
|
|
|
2016-09-15 18:25:56 +00:00
|
|
|
- name: ensure testfile is still present
|
|
|
|
win_stat:
|
|
|
|
path: c:\testfile.txt
|
|
|
|
register: statout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- statout.stat.exists == true
|
|
|
|
|
2018-04-02 20:30:53 +00:00
|
|
|
# https://github.com/ansible/ansible/issues/37967
|
|
|
|
- name: test creates with file in missing directory
|
|
|
|
win_shell: echo hi
|
|
|
|
args:
|
|
|
|
creates: c:\fakefolder\fakefolder2\fakefile.txt
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- shellout.skipped is not defined
|
|
|
|
- shellout.changed
|
|
|
|
|
2016-09-15 18:25:56 +00:00
|
|
|
- name: run with removes, should remove
|
|
|
|
win_shell: Remove-Item c:\testfile.txt
|
|
|
|
args:
|
|
|
|
removes: c:\testfile.txt
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is successful
|
|
|
|
- shellout is changed
|
2016-09-15 18:25:56 +00:00
|
|
|
|
|
|
|
- name: run again with removes, should skip
|
|
|
|
win_shell: echo $null >> c:\testfile.txt
|
|
|
|
args:
|
|
|
|
removes: c:\testfile.txt
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is skipped
|
2016-09-15 18:25:56 +00:00
|
|
|
- shellout.msg is search('does not exist')
|
|
|
|
|
|
|
|
- name: run something with known nonzero exit code
|
|
|
|
win_shell: exit 254
|
|
|
|
register: shellout
|
|
|
|
ignore_errors: true
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is failed
|
2017-07-05 17:05:07 +00:00
|
|
|
- shellout.failed == True # check the failure key explicitly, since failed does magic with RC
|
2016-09-15 18:25:56 +00:00
|
|
|
- shellout.rc == 254
|
|
|
|
|
|
|
|
- name: run something via cmd that will fail in powershell
|
|
|
|
win_shell: echo line1 & echo.line2
|
|
|
|
args:
|
|
|
|
executable: cmd
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: validate result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is successful
|
|
|
|
- shellout is changed
|
2016-09-15 18:25:56 +00:00
|
|
|
- shellout.rc == 0
|
|
|
|
- shellout.stdout == "line1 \r\nline2\r\n"
|
|
|
|
- shellout.stdout_lines == ["line1 ", "line2"]
|
|
|
|
- shellout.stderr == ""
|
|
|
|
|
2017-03-24 07:02:39 +00:00
|
|
|
- name: test with job to ensure that preamble-free InputEncoding is working
|
|
|
|
win_shell: Start-Job { echo yo } | Receive-Job -Wait
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: check job result
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is successful
|
2017-03-24 07:02:39 +00:00
|
|
|
- shellout.stdout_lines[0] == 'yo'
|
|
|
|
|
2016-10-25 23:06:57 +00:00
|
|
|
- name: interleave large writes between stdout/stderr (check for buffer consumption deadlock)
|
|
|
|
win_shell: $ba = New-Object byte[] 4096; (New-Object System.Random 32).NextBytes($ba); $text = [Convert]::ToBase64String($ba); Write-Output startout; Write-Error starterror; Write-Error $text; Write-Output $text; Write-Error $text; Write-Output $text; Write-Error $text; Write-Output $text; Write-Output doneout Write-Error doneerror
|
2016-09-15 18:25:56 +00:00
|
|
|
register: shellout
|
|
|
|
|
2016-10-25 23:06:57 +00:00
|
|
|
- name: ensure that the entirety of both streams were read
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- shellout.stdout is search("startout")
|
|
|
|
- shellout.stdout is search("doneout")
|
|
|
|
- shellout.stderr is search("starterror")
|
|
|
|
- shellout.stderr is search("doneerror")
|
2017-10-22 23:50:19 +00:00
|
|
|
|
|
|
|
- name: run stdin test
|
|
|
|
win_shell: '$string = [Console]::In.ReadToEnd(); Write-Output $string.Trim()'
|
|
|
|
args:
|
|
|
|
stdin: some input
|
|
|
|
register: shellout
|
|
|
|
|
|
|
|
- name: assert run stdin test
|
|
|
|
assert:
|
|
|
|
that:
|
2017-11-27 22:58:08 +00:00
|
|
|
- shellout is changed
|
2017-10-22 23:50:19 +00:00
|
|
|
- shellout.rc == 0
|
|
|
|
- shellout.stderr == ""
|
|
|
|
- shellout.stdout == "some input\r\n"
|
2018-04-09 19:25:02 +00:00
|
|
|
|
|
|
|
- name: echo some non ascii characters
|
|
|
|
win_shell: Write-Host über den Fußgängerübergang gehen
|
|
|
|
register: nonascii_output
|
|
|
|
|
|
|
|
- name: assert echo some non ascii characters
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- nonascii_output is changed
|
|
|
|
- nonascii_output.rc == 0
|
|
|
|
- nonascii_output.stdout_lines|count == 1
|
|
|
|
- nonascii_output.stdout_lines[0] == 'über den Fußgängerübergang gehen'
|
|
|
|
- nonascii_output.stderr == ''
|