Action execution conditioned to non-empty output of previous command

Hello,

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

- script: somescriptA.sh
   register: resultA

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

- script: somescriptC.sh
   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:

  resultB is defined
  resultB.stdout != ''
  resultB.stdout.strip() != ''

also without success.

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

Thanks in advance.

Luciano Cavalheiro

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

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

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

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

-jlk