# Action execution conditioned to non-empty output of previous command

**URL:** <https://forum.ansible.com/t/action-execution-conditioned-to-non-empty-output-of-previous-command/13182>\
**Category:** Ansible Project\
**Created:** [July 23, 2013, 9:44pm UTC](https://forum.ansible.com/t/action-execution-conditioned-to-non-empty-output-of-previous-command/13182 "2013-07-23T21:44:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucc](https://avatars.discourse-cdn.com/v4/letter/l/34f0e0/32.png) [@lucc](https://forum.ansible.com/u/lucc)\
**Post date:** [July 23, 2013, 9:44pm UTC](https://forum.ansible.com/t/action-execution-conditioned-to-non-empty-output-of-previous-command/13182/1 "2013-07-23T21:44:02Z")

</div>

Hello,

I'm using ansible 1.2 and I would like to do the following:

- script: somescriptA.sh  
&nbsp;&nbsp;&nbsp;register: resultA

- script: somescriptB.sh {{resultA.stdout}}  
&nbsp;&nbsp;&nbsp;register: resultB  
&nbsp;&nbsp;&nbsp;when: resultA.stdout.find("somemark") != -1

- script: somescriptC.sh  
&nbsp;&nbsp;&nbsp;when: resultB.stdout|strip

Where the last script shall be executed when somescriptB succeed and produced non-empty output (that's why the |strip).

It happens the when the second action is skipped, the 3rd when:  
fails (possibly because resultB is not defined) with something that  
looks like a Jinja2 template error message:

"Conditional expression must evaluate to True or False: {% if resultB.stdout %} True {% else %} False {% endif %}"

I've tried also with the 3rd when:

&nbsp;&nbsp;resultB is defined  
&nbsp;&nbsp;resultB.stdout != ''  
&nbsp;&nbsp;resultB.stdout.strip() != ''

also without success.

What's the proper way of doing such conditional chaining?

Thanks in advance.

Luciano Cavalheiro

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 24, 2013, 1:10am UTC](https://forum.ansible.com/t/action-execution-conditioned-to-non-empty-output-of-previous-command/13182/2 "2013-07-24T01:10:53Z")

</div>

I’d suggest:

when: resultB is defined and resultB.stdout.strip() != “”

no need for the Jinja2 filter as you can call methods on the string

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [July 24, 2013, 10:38pm UTC](https://forum.ansible.com/t/action-execution-conditioned-to-non-empty-output-of-previous-command/13182/3 "2013-07-24T22:38:54Z")

</div>

I believe in this case, resultB will exist, but it'll have skipped: True in it and may not have an stdout. So testing whether it exists wouldn't be prudent.

-jlk

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [July 25, 2013, 6:43am UTC](https://forum.ansible.com/t/action-execution-conditioned-to-non-empty-output-of-previous-command/13182/4 "2013-07-25T06:43:14Z")

</div>

There is also foo.skipped as a variable to see if a task is skipped.

---

<div class="post-metadata">

**Author:** ![Jesse\_Keating](https://avatars.discourse-cdn.com/v4/letter/j/3bc359/32.png) [@Jesse\_Keating](https://forum.ansible.com/u/Jesse_Keating)\
**Post date:** [July 25, 2013, 4:02pm UTC](https://forum.ansible.com/t/action-execution-conditioned-to-non-empty-output-of-previous-command/13182/5 "2013-07-25T16:02:49Z")

</div>

Right, that's what I meant you should check for, rather than checking if the variable exists at all, because it will exist.

-jlk
