# Search for string in stdout for failed\_when

**URL:** <https://forum.ansible.com/t/search-for-string-in-stdout-for-failed-when/31142>\
**Category:** Ansible Project\
**Created:** [August 27, 2019, 7:10pm UTC](https://forum.ansible.com/t/search-for-string-in-stdout-for-failed-when/31142 "2019-08-27T19:10:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JP-OKC](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@JP-OKC](https://forum.ansible.com/u/JP-OKC)\
**Post date:** [August 27, 2019, 7:10pm UTC](https://forum.ansible.com/t/search-for-string-in-stdout-for-failed-when/31142/1 "2019-08-27T19:10:20Z")

</div>

how can I pull a string from the stdout and use it for failed\_when? I run a command on a switch and then I want to match a string w/in that output to determine if it failed or not.

Here’s what the playbook looks like:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 27, 2019, 7:22pm UTC](https://forum.ansible.com/t/search-for-string-in-stdout-for-failed-when/31142/2 "2019-08-27T19:22:26Z")

</div>

As you can see here the [ indicate a list so first element is 0.  
The nxos\_command takes several commands, the first command is in list element 0, the second one is list element 1 and so on....

so you need to use element 0 like this

failed\_when: ("could not retrieve rsa key information" in print\_output.stdout.0)

---

<div class="post-metadata">

**Author:** ![JP-OKC](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@JP-OKC](https://forum.ansible.com/u/JP-OKC)\
**Post date:** [August 27, 2019, 7:54pm UTC](https://forum.ansible.com/t/search-for-string-in-stdout-for-failed-when/31142/3 "2019-08-27T19:54:33Z")

</div>

Thanks Kai, That worked.
