I tried adding "changed_when" for when sel_out is not defined, but it gives
me an error, so I am not sure how to use "changed_when" in this case.
I'm sorry, i meant to write failed_when and not changed_when, but the usage is the same.
What's the correct usage?
- name: Check for ECC's
shell: ipmitool sel list | grep -i ecc
register: sel_out
changed_when: sel_out is not defined
async: 3
poll: 2
fatal: [192.168.100.11]: FAILED! => {"ansible_job_id":
"186470565638.100676", "changed": false, "cmd": "ipmitool sel list | grep
-i ecc", "delta": "0:00:00.094723", "end": "2017-08-12 01:40:04.893313",
"failed": true, "finished": 1, "rc": 1, "start": "2017-08-12
01:40:04.798590", "stderr": "", "stderr_lines": , "stdout": "",
"stdout_lines": }
As you can see in you fatal output, the sel_out is defined.
rc (return code) is 1, failed = true and stdout is empty.
grep exit with 0 when it finds the string and 1 if it doesn't find the string, so an error you can be defined as rc > 1.
- name: Check for ECC's
shell: ipmitool sel list | grep -i ecc
register: sel_out
failed_when: sel_out.rc > 1
async: 3
poll: 2
I was able to use this method, which seems to work. Is this the correct
usage of "|| true"?
- name: Check for ECC's
shell: ipmitool sel list | grep -i ecc || true
register: sel_out
async: 3
poll: 2
By using || true sel_out.rc will always be 0, so you loose the ability to check what return code grep is giving. So i highly recommend the solution above.
With the above usage, I am back to my original question. How can I add
conditionals to individual items in a debug msg list?
Short answer, you can't.
when can only be used on debug it self, not the msg list.
For instance, how
can I add a conditional only for "{{sel_out.stdout.split('\n') }}"? If i
put the "when" in the same column as "name" and "debug" as Dick suggested,
then it will impact the full debug msg list, not only the sel_out line.
If the grep doesn't find anything the sel_out.stdout.split('\n') will be a empty string.
If you don't want that you can split debug in several tasks and use when: on the task.
or since you have 2 variables, this gives 4 combination, so you could make 4 debug task with 4 different when's.