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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
# Test code for the command and shell modules.
# Copyright: (c) 2014, Richard Isaacson <richard.c.isaacson@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
- name: use command with unsupported executable arg
command: ls /dev/null
args:
executable: /bogus
register: executable
- name: assert executable warning was reported
assert:
that:
- executable.stdout == '/dev/null'
- executable.warnings | length() == 1
- "'no longer supported' in executable.warnings[0]"
- name: use command with no command
command:
args:
chdir: /
register: no_command
ignore_errors: true
- name: assert executable fails with no command
assert:
that:
- no_command is failed
- no_command.msg is contains 'one of the following is required'
- name: use argv
command:
argv:
- echo
- testing
register: argv_command
ignore_errors: true
- name: assert executable works with argv
assert:
that:
- "argv_command.stdout == 'testing'"
- name: use argv and command string
command: echo testing
args:
argv:
- echo
- testing
register: argv_and_string_command
ignore_errors: true
- name: assert executable fails with both argv and command string
assert:
that:
- argv_and_string_command is failed
- argv_and_string_command.msg is contains 'parameters are mutually exclusive'
- set_fact:
remote_tmp_dir_test: "{{ remote_tmp_dir }}/test_command_shell"
- name: make sure our testing sub-directory does not exist
file:
path: "{{ remote_tmp_dir_test }}"
state: absent
- name: create our testing sub-directory
file:
path: "{{ remote_tmp_dir_test }}"
state: directory
- name: prep our test script
copy:
src: test.sh
dest: "{{ remote_tmp_dir_test }}"
mode: '0755'
- name: prep our test script
copy:
src: create_afile.sh
dest: "{{ remote_tmp_dir_test }}"
mode: '0755'
- name: prep our test script
copy:
src: remove_afile.sh
dest: "{{ remote_tmp_dir_test }}"
mode: '0755'
- name: locate bash
shell: which bash
register: bash
##
## command
##
- name: execute the test.sh script via command
command: "{{ remote_tmp_dir_test }}/test.sh"
register: command_result0
- name: assert that the script executed correctly
assert:
that:
- command_result0.rc == 0
- command_result0.stderr == ''
- command_result0.stdout == 'win'
# executable
# FIXME doesn't have the expected stdout.
#- name: execute the test.sh script with executable via command
# command: "{{remote_tmp_dir_test }}/test.sh executable={{ bash.stdout }}"
# register: command_result1
#
#- name: assert that the script executed correctly with command
# assert:
# that:
# - "command_result1.rc == 0"
# - "command_result1.stderr == ''"
# - "command_result1.stdout == 'win'"
# chdir
- name: execute the test.sh script with chdir via command
command: ./test.sh
args:
chdir: "{{ remote_tmp_dir_test }}"
register: command_result2
- name: Check invalid chdir
command: echo
args:
chdir: "{{ remote_tmp_dir }}/nope"
ignore_errors: yes
register: chdir_invalid
- name: assert that the script executed correctly with chdir
assert:
that:
- command_result2.rc == 0
- command_result2.stderr == ''
- command_result2.stdout == 'win'
- chdir_invalid is failed
- chdir_invalid.msg is search('Unable to change directory')
# creates
- name: verify that afile.txt is absent
file:
path: "{{ remote_tmp_dir_test }}/afile.txt"
state: absent
- name: create afile.txt with create_afile.sh via command (check mode)
command: "{{ remote_tmp_dir_test }}/create_afile.sh {{remote_tmp_dir_test }}/afile.txt"
args:
creates: "{{ remote_tmp_dir_test }}/afile.txt"
register: check_mode_result
check_mode: yes
- assert:
that:
- check_mode_result.changed
- "'skipped' not in check_mode_result"
- name: verify that afile.txt still does not exist
stat:
path: "{{remote_tmp_dir_test}}/afile.txt"
register: stat_result
failed_when: stat_result.stat.exists
- name: create afile.txt with create_afile.sh via command
command: "{{ remote_tmp_dir_test }}/create_afile.sh {{remote_tmp_dir_test }}/afile.txt"
args:
creates: "{{ remote_tmp_dir_test }}/afile.txt"
- name: verify that afile.txt is present
file:
path: "{{ remote_tmp_dir_test }}/afile.txt"
state: file
- name: re-run previous command using creates with globbing (check mode)
command: "{{ remote_tmp_dir_test }}/create_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
args:
creates: "{{ remote_tmp_dir_test }}/afile.*"
register: check_mode_result
check_mode: yes
- assert:
that:
- not check_mode_result.changed
- "'skipped' not in check_mode_result"
- name: re-run previous command using creates with globbing
command: "{{ remote_tmp_dir_test }}/create_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
args:
creates: "{{ remote_tmp_dir_test }}/afile.*"
register: command_result3
- name: assert that creates with globbing is working
assert:
that:
- command_result3 is not changed
# removes
- name: remove afile.txt with remote_afile.sh via command (check mode)
command: "{{ remote_tmp_dir_test }}/remove_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
args:
removes: "{{ remote_tmp_dir_test }}/afile.txt"
register: check_mode_result
check_mode: yes
- assert:
that:
- check_mode_result.changed
- "'skipped' not in check_mode_result"
- name: verify that afile.txt still exists
stat:
path: "{{remote_tmp_dir_test}}/afile.txt"
register: stat_result
failed_when: not stat_result.stat.exists
- name: remove afile.txt with remote_afile.sh via command
command: "{{ remote_tmp_dir_test }}/remove_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
args:
removes: "{{ remote_tmp_dir_test }}/afile.txt"
- name: verify that afile.txt is absent
file: path={{remote_tmp_dir_test}}/afile.txt state=absent
- name: re-run previous command using removes with globbing (check mode)
command: "{{ remote_tmp_dir_test }}/remove_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
args:
removes: "{{ remote_tmp_dir_test }}/afile.*"
register: check_mode_result
check_mode: yes
- assert:
that:
- not check_mode_result.changed
- "'skipped' not in check_mode_result"
- name: re-run previous command using removes with globbing
command: "{{ remote_tmp_dir_test }}/remove_afile.sh {{ remote_tmp_dir_test }}/afile.txt"
args:
removes: "{{ remote_tmp_dir_test }}/afile.*"
register: command_result4
- name: assert that removes with globbing is working
assert:
that:
- command_result4.changed != True
- name: pass stdin to cat via command
command: cat
args:
stdin: 'foobar'
register: command_result5
- name: assert that stdin is passed
assert:
that:
- command_result5.stdout == 'foobar'
- name: send to stdin literal multiline block
command: "{{ ansible_python.executable }} -c 'import hashlib, sys; print(hashlib.sha1((sys.stdin.buffer if hasattr(sys.stdin, \"buffer\") else sys.stdin).read()).hexdigest())'"
args:
stdin: |-
this is the first line
this is the second line
this line is after an empty line
this line is the last line
register: command_result6
- name: assert the multiline input was passed correctly
assert:
that:
- "command_result6.stdout == '9cd0697c6a9ff6689f0afb9136fa62e0b3fee903'"
- name: check default var expansion
command: /bin/sh -c 'echo "\$TEST"'
environment:
TEST: z
register: command_result7
- name: assert vars were expanded
assert:
that:
- command_result7.stdout == '\\z'
- name: check disabled var expansion
command: /bin/sh -c 'echo "\$TEST"'
args:
expand_argument_vars: false
environment:
TEST: z
register: command_result8
- name: assert vars were not expanded
assert:
that:
- command_result8.stdout == '$TEST'
##
## shell
##
- name: Execute the test.sh script
shell: "{{ remote_tmp_dir_test }}/test.sh"
register: shell_result0
- name: Assert that the script executed correctly
assert:
that:
- shell_result0 is changed
- shell_result0.cmd == remote_tmp_dir_test + '/test.sh'
- shell_result0.rc == 0
- shell_result0.stderr == ''
- shell_result0.stdout == 'win'
# executable
# FIXME doesn't pass the expected stdout
#- name: execute the test.sh script
# shell: "{{remote_tmp_dir_test }}/test.sh executable={{ bash.stdout }}"
# register: shell_result1
#
#- name: assert that the shell executed correctly
# assert:
# that:
# - "shell_result1.rc == 0"
# - "shell_result1.stderr == ''"
# - "shell_result1.stdout == 'win'"
# chdir
- name: Execute the test.sh script with chdir
shell: ./test.sh
args:
chdir: "{{ remote_tmp_dir_test }}"
register: shell_result2
- name: Assert that the shell executed correctly with chdir
assert:
that:
- shell_result2 is changed
- shell_result2.cmd == './test.sh'
- shell_result2.rc == 0
- shell_result2.stderr == ''
- shell_result2.stdout == 'win'
# creates
- name: Verify that afile.txt is absent
file:
path: "{{ remote_tmp_dir_test }}/afile.txt"
state: absent
- name: Execute the test.sh script with chdir
shell: "{{ remote_tmp_dir_test }}/test.sh > {{ remote_tmp_dir_test }}/afile.txt"
args:
chdir: "{{ remote_tmp_dir_test }}"
creates: "{{ remote_tmp_dir_test }}/afile.txt"
- name: Verify that afile.txt is present
file:
path: "{{ remote_tmp_dir_test }}/afile.txt"
state: file
# multiline
- name: Remove test file previously created
file:
path: "{{ remote_tmp_dir_test }}/afile.txt"
state: absent
- name: Execute a shell command using a literal multiline block
args:
executable: "{{ bash.stdout }}"
shell: |
echo this is a \
"multiline echo" \
"with a new line
in quotes" \
| {{ ansible_python.executable }} -c 'import hashlib, sys; print(hashlib.sha1((sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin).read()).hexdigest())'
echo "this is a second line"
register: shell_result5
- name: Assert the multiline shell command ran as expected
assert:
that:
- shell_result5 is changed
- shell_result5.rc == 0
- shell_result5.cmd == 'echo this is a "multiline echo" "with a new line\nin quotes" | ' + ansible_python.executable + ' -c \'import hashlib, sys; print(hashlib.sha1((sys.stdin.buffer if hasattr(sys.stdin, "buffer") else sys.stdin).read()).hexdigest())\'\necho "this is a second line"\n'
- shell_result5.stdout == '5575bb6b71c9558db0b6fbbf2f19909eeb4e3b98\nthis is a second line'
- name: Execute a shell command using a literal multiline block with arguments in it
shell: |
executable="{{ bash.stdout }}"
creates={{ remote_tmp_dir_test }}/afile.txt
echo "test"
register: shell_result6
- name: Assert the multiline shell command with arguments in it run as expected
assert:
that:
- shell_result6 is changed
- shell_result6.rc == 0
- shell_result6.cmd == 'echo "test"\n'
- shell_result6.stdout == 'test'
- name: Execute a shell command using a multiline block where whitespaces matter
shell: |
cat <<EOF
One
Two
Three
EOF
register: shell_result7
- name: Assert the multiline shell command outputs with whitespaces preserved
assert:
that:
- shell_result7 is changed
- shell_result7.rc == 0
- shell_result7.cmd == 'cat <<EOF\nOne\n Two\n Three\nEOF\n'
- shell_result7.stdout == 'One\n Two\n Three'
- name: execute a shell command with no trailing newline to stdin
shell: cat > {{remote_tmp_dir_test }}/afile.txt
args:
stdin: test
stdin_add_newline: no
- name: make sure content matches expected
copy:
dest: "{{remote_tmp_dir_test }}/afile.txt"
content: test
register: shell_result7
failed_when:
- shell_result7 is failed or
shell_result7 is changed
- name: execute a shell command with trailing newline to stdin
shell: cat > {{remote_tmp_dir_test }}/afile.txt
args:
stdin: test
stdin_add_newline: yes
- name: make sure content matches expected
copy:
dest: "{{remote_tmp_dir_test }}/afile.txt"
content: |
test
register: shell_result8
failed_when:
- shell_result8 is failed or
shell_result8 is changed
- name: execute a shell command with trailing newline to stdin, default
shell: cat > {{remote_tmp_dir_test }}/afile.txt
args:
stdin: test
- name: make sure content matches expected
copy:
dest: "{{remote_tmp_dir_test }}/afile.txt"
content: |
test
register: shell_result9
failed_when:
- shell_result9 is failed or
shell_result9 is changed
- name: remove the previously created file
file:
path: "{{ remote_tmp_dir_test }}/afile.txt"
state: absent
- name: test check mode skip message
command:
cmd: "true"
check_mode: yes
register: result
- name: assert check message exists
assert:
that:
- "'Command would have run if not in check mode' in result.msg"
- result.skipped
- not result.changed
- name: test check mode creates/removes message
command:
cmd: "true"
creates: yes
check_mode: yes
register: result
- name: assert check message exists
assert:
that:
- "'Command would have run if not in check mode' in result.msg"
- "'skipped' not in result"
- result.changed
- name: command symlink handling
block:
- name: Create target folders
file:
path: '{{remote_tmp_dir}}/www_root/site'
state: directory
- name: Create symlink
file:
path: '{{remote_tmp_dir}}/www'
state: link
src: '{{remote_tmp_dir}}/www_root'
- name: check parent using chdir
shell: dirname "$PWD"
args:
chdir: '{{remote_tmp_dir}}/www/site'
register: parent_dir_chdir
- name: check parent using cd
shell: cd "{{remote_tmp_dir}}/www/site" && dirname "$PWD"
register: parent_dir_cd
- name: check expected outputs
assert:
that:
- parent_dir_chdir.stdout != parent_dir_cd.stdout
# These tests use endswith, to get around /private/tmp on macos
- 'parent_dir_cd.stdout.endswith(remote_tmp_dir ~ "/www")'
- 'parent_dir_chdir.stdout.endswith(remote_tmp_dir ~ "/www_root")'
- name: Set print error command for Python 3
set_fact:
print_error_command: print(msg, file=sys.stderr)
- name: run command with strip
command: '{{ ansible_python_interpreter }} -c "import sys; msg=''hello \n \r''; print(msg); {{ print_error_command }}"'
register: command_strip
- name: run command without strip
command: '{{ ansible_python_interpreter }} -c "import sys; msg=''hello \n \r''; print(msg); {{ print_error_command }}"'
args:
strip_empty_ends: no
register: command_no_strip
- name: Verify strip behavior worked as expected
assert:
that:
- command_strip.stdout == 'hello \n '
- command_strip.stderr == 'hello \n '
- command_no_strip.stdout== 'hello \n \r\n'
- command_no_strip.stderr == 'hello \n \r\n'
- name: run shell with expand_argument_vars
shell: echo 'hi'
args:
expand_argument_vars: false
register: shell_expand_failure
ignore_errors: true
- name: assert shell with expand_arguments_vars failed
assert:
that:
- shell_expand_failure is failed
- "shell_expand_failure.msg == 'Unsupported parameters for (shell) module: expand_argument_vars'"
- name: Run command that backgrounds, to ensure no hang
shell: '{{ role_path }}/scripts/yoink.sh &'
delegate_to: localhost
timeout: 5
- name: Run command that does not exist
command: commandthatdoesnotexist --would-be-awkward
register: res
failed_when: res.stdout != '' or res.stderr != ''
|