# trouble with shell and error-handling

**URL:** <https://forum.ansible.com/t/trouble-with-shell-and-error-handling/18875>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [July 28, 2015, 1:56pm UTC](https://forum.ansible.com/t/trouble-with-shell-and-error-handling/18875 "2015-07-28T13:56:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trond\_Hindenes](https://avatars.discourse-cdn.com/v4/letter/t/96bed5/32.png) [@Trond\_Hindenes](https://forum.ansible.com/u/Trond_Hindenes)\
**Post date:** [July 28, 2015, 1:56pm UTC](https://forum.ansible.com/t/trouble-with-shell-and-error-handling/18875/1 "2015-07-28T13:56:00Z")

</div>

Hi all,  
I’m trying to build some logic around rebooting Windows nodes and waiting until they come back online. I’m building a role to easily allow us to check for pending reboots and reboot if necessary as part of a playbook.

Since Ansible is using winrm I figured it best to using winrm for testing also, so I basically want to loop until a curl request to the winrm endpoint responds with a certain string.

My problems:

1. If the node doesnt respond, there simply isnt any output. When this is the case, it looks like Ansible isn’t able to evaluate "result.stdout.find(“some string”) since result doesnt have a stdout attribute. I’ve been able to get around by using some bash conditonals that basically output “nothing returned” if the curl call returns empty.
2. Ansible seems to just hang there indefinetely if it cannot reach the url. This should cause my shell script to just output “nothing returned” (which I’ve verified manually using shell).
3. Although I have set ignore\_errors to true, the script originally failed. I used the shell “|| true” to have curl always report success, although I don’t think I should have to. Ansible bug?

I’m running on the latest devel commit in v1 mode.

Any pointers would be greatly appreciated. Here’s the task file from the role:

- name: Wait for windows reboot

# when: pending\_reboot\_output.pending\_reboot == true

shell: ‘if [[-n $(curl -s -f -k --header “Content-Type: application/soap+xml;charset=UTF-8” --header “WSMANIDENTIFY: unauthenticated” [https://thansiblewin01.ansib.le:5986/wsman](https://thansiblewin01.ansib.le:5986/wsman) --data “\<s:Envelope xmlns:s=[http://www.w3.org/2003/05/soap-envelope](http://www.w3.org/2003/05/soap-envelope) xmlns:wsmid=[http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd](http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd)\>\<s:Header/\>\<s:Body\>wsmid:Identify/\</s:Body\>\</s:Envelope\>” || true) ]]; then curl -s -f -k --header “Content-Type: application/soap+xml;charset=UTF-8” --header “WSMANIDENTIFY: unauthenticated” [https://thansiblewin01.ansib.le:5986/wsman](https://thansiblewin01.ansib.le:5986/wsman) --data “\<s:Envelope xmlns:s=[http://www.w3.org/2003/05/soap-envelope](http://www.w3.org/2003/05/soap-envelope) xmlns:wsmid=[http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd](http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd)\>\<s:Header/\>\<s:Body\>wsmid:Identify/\</s:Body\>\</s:Envelope\>” || true ;else echo “nothing returned” ;fi’  
register: result2  
until: result2.stdout.find(“[http://www.w3.org/2003/05/soap-envelope](http://www.w3.org/2003/05/soap-envelope)”)  
retries: 10  
delay: 10  
delegate\_to: 127.0.0.1  
ignore\_errors: yes  
changed\_when: False

prettier copy at: [https://gist.github.com/trondhindenes/23137e66f8a34603b8cf](https://gist.github.com/trondhindenes/23137e66f8a34603b8cf)

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [July 28, 2015, 7:26pm UTC](https://forum.ansible.com/t/trouble-with-shell-and-error-handling/18875/2 "2015-07-28T19:26:19Z")

</div>

have you looked at using the existing wait\_for module? it should be  
able to detect if the winrm port is open.
