summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/win_shell/tasks/main.yml
blob: 16759bb283ba17c907655ae4f78924794ffe89ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
- name: execute a powershell cmdlet
  win_shell: Write-Output "hello from Ansible"
  register: shellout

- name: validate result
  assert:
    that:
    - shellout|success
    - shellout|changed
    - 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}$')
    # assertion disabled since it does not pass on Windows Server 2016
    # - shellout.stderr == ""
    - shellout.stdout == "hello from Ansible\r\n"
    - shellout.stdout_lines == ["hello from Ansible"]

- 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"
  register: shellout

- name: validate result
  assert:
    that:
    - shellout|success
    - shellout|changed
    - shellout.cmd == 'Write-Output "hello from Ansible"; Write-Output "another line"; Write-Output "yet another line"; Write-Output "envvar was $env:taskvar"'
    - 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}$')
    # assertion disabled since it does not pass on Windows Server 2016
    # - shellout.stderr == ""
    - 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"]

- name: execute something nonexistent
  win_shell: bogus_command1234
  register: shellout
  ignore_errors: true

- name: validate result
  assert:
    that:
    - shellout|failed
    - shellout|changed
    - 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:
    - shellout|success
    - shellout|changed
    - 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:
    - shellout|success
    - shellout|changed

- 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:
    - shellout|skipped
    - shellout.msg is search('exists')

- name: ensure testfile is still present
  win_stat:
    path: c:\testfile.txt
  register: statout

- name: validate result
  assert:
    that:
    - statout.stat.exists == true

- 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:
    - shellout|success
    - shellout|changed

- 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:
    - shellout|skipped
    - 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:
    - shellout|failed
    - 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:
    - shellout|success
    - shellout|changed
    - shellout.rc == 0
    - shellout.stdout == "line1 \r\nline2\r\n"
    - shellout.stdout_lines == ["line1 ", "line2"]
    - shellout.stderr == ""

- 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
  register: shellout

- 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")