# Add conditionals to debug msg

**URL:** <https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226>\
**Category:** Ansible Project\
**Created:** [August 10, 2017, 8:55pm UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226 "2017-08-10T20:55:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan1](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Ryan1](https://forum.ansible.com/u/Ryan1)\
**Post date:** [August 10, 2017, 8:55pm UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/1 "2017-08-10T20:55:10Z")

</div>

How do I add a conditionals to debug msg lists?

- name: Print results  
debug:  
msg:
- “Board: {{ ansible\_product\_name }}”
- “CPU: {{ ansible\_processor[1] }} Cores: {{ ansible\_processor\_cores }} CPU Count: {{ ansible\_processor\_count }}”
- “Memory: {{ ansible\_memtotal\_mb }}”
- “ansible processor count: {{ ansible\_processor\_count }}”
- “Log File: {{ logfile }}”
- “{{ out.stdout.split(‘\n’) }}”
- “{{sel\_out.stdout.split(‘\n’) }}”

Depending on the results, sometimes “register: out” and/or “register: sel\_out” may not be defined, so the last two messages, will fail if not defined. I tried using the following, but it does not like this method, so i am doing it wrong.

- name: Print results  
debug:  
msg:
- “Board: {{ ansible\_product\_name }}”
- “CPU: {{ ansible\_processor[1] }} Cores: {{ ansible\_processor\_cores }} CPU Count: {{ ansible\_processor\_count }}”
- “Memory: {{ ansible\_memtotal\_mb }}”
- “ansible processor count: {{ ansible\_processor\_count }}”
- “Log File: {{ logfile }}”
- “{{ out.stdout.split(‘\n’) }}”
- “{{sel\_out.stdout.split(‘\n’) }}”  
when: sel\_out is defined

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 11, 2017, 5:32am UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/2 "2017-08-11T05:32:32Z")

</div>

Not sure what you mean by ‘it doesn’t like this method’? I’m guessing you’re getting an error - which would have been helpful in diagnosing the issue.

So again, guessing, and hard to see from this message, but it looks like the indentation of the when statement is way off. It should be at the same column as name and debug.

Dick

---

<div class="post-metadata">

**Author:** ![Ryan1](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Ryan1](https://forum.ansible.com/u/Ryan1)\
**Post date:** [August 11, 2017, 6:25am UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/3 "2017-08-11T06:25:52Z")

</div>

Sorry, I will attache the error next time.

I tried indenting it to the same column as name and debug, but it still failed. If I change the command to something that returns a value for sel\_out, the debug msg returns the expected output. The error only comes when sel\_out is not defined.

- name: Print results

debug:  
msg:

- “Board: {{ ansible\_product\_name }}”
- “CPU: {{ ansible\_processor[1] }} Cores: {{ ansible\_processor\_cores }} CPU Count: {{ ansible\_processor\_count }}”
- “Memory: {{ ansible\_memtotal\_mb }}”
- “ansible processor count: {{ ansible\_processor\_count }}”
- “Log File: {{ logfile }}”
- “{{ out.stdout.split(‘\n’) }}”
- “{{sel\_out.stdout.split(‘\n’) }}”  
when: sel\_out is defined

fatal: [192.168.100.11]: FAILED! =\> {“ansible\_job\_id”: “717909993947.50020”, “changed”: true, “cmd”: “ipmitool sel list | grep -i ecc”, “delta”: “0:00:00.088089”, “end”: “2017-08-11 06:02:57.289040”, “failed”: true, “finished”: 1, “rc”: 1, “start”: “2017-08-11 06:02:57.200951”, “stderr”: “”, “stderr\_lines”: , “stdout”: “”, “stdout\_lines”: }

---

<div class="post-metadata">

**Author:** ![Ryan1](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Ryan1](https://forum.ansible.com/u/Ryan1)\
**Post date:** [August 11, 2017, 6:36am UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/4 "2017-08-11T06:36:41Z")

</div>

There seems to be a paste formatting issue. Under vim name and debug are in the same column. They were not when I pasted them in the last reply.

---

<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 11, 2017, 9:51am UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/5 "2017-08-11T09:51:12Z")

</div>

> Sorry, I will attache the error next time.

But you didn't include all relevant output.

> I tried indenting it to the same column as name and debug, but it still  
> failed. If I change the command to something that returns a value for  
> sel\_out, the debug msg returns the expected output. The error only comes  
> when sel\_out is not defined.
> 
> - name: Print results  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "Board: {{ ansible\_product\_name }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "CPU: {{ ansible\_processor[1] }} Cores: {{  
> ansible\_processor\_cores }} CPU Count: {{ ansible\_processor\_count }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "Memory: {{ ansible\_memtotal\_mb }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "ansible processor count: {{ ansible\_processor\_count }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "Log File: {{ logfile }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ out.stdout.split('\n') }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{sel\_out.stdout.split('\n') }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: sel\_out is defined
> 
> fatal: [192.168.100.11]: FAILED! =\> {"ansible\_job\_id":  
> "717909993947.50020", "changed": true, "cmd": "ipmitool sel list | grep -i  
> ecc", "delta": "0:00:00.088089", "end": "2017-08-11 06:02:57.289040",  
> "failed": true, "finished": 1, "rc": 1, "start": "2017-08-11  
> 06:02:57.200951", "stderr": "", "stderr\_lines": , "stdout": "",  
> "stdout\_lines": } \>

This error has noting to do with you debug task, it's the task you running "ipmitool sel list"  
Had you included more information about this error you would have seen that easy a few lines before this statement.

grep isn't finding any ecc in the output av return code 1, this means failure to ansible.  
To change this you have can add "changed\_when" to the task.

---

<div class="post-metadata">

**Author:** ![Ryan1](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Ryan1](https://forum.ansible.com/u/Ryan1)\
**Post date:** [August 12, 2017, 2:09am UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/6 "2017-08-12T02:09:16Z")

</div>

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. 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”: }

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

With the above usage, I am back to my original question. How can I add conditionals to individual items in a debug 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.

- name: Print results  
debug:  
msg:
- “Board: {{ ansible\_product\_name }}”
- “CPU: {{ ansible\_processor[1] }} Cores: {{ ansible\_processor\_cores }} CPU Count: {{ ansible\_processor\_count }}”
- “Memory: {{ ansible\_memtotal\_mb }}”
- “ansible processor count: {{ ansible\_processor\_count }}”
- “Log File: {{ logfile }}”
- “{{ out.stdout.split(‘\n’) }}”
- “{{sel\_out.stdout.split(‘\n’) }}”

---

<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 12, 2017, 8:42am UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/7 "2017-08-12T08:42:56Z")

</div>

> 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.

> &nbsp;&nbsp;What's the correct usage?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Check for ECC's  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: ipmitool sel list | grep -i ecc  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: sel\_out  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed\_when: sel\_out is not defined  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;async: 3  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

&nbsp;&nbsp;&nbsp;- name: Check for ECC's  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: ipmitool sel list | grep -i ecc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: sel\_out  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;failed\_when: sel\_out.rc \> 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;async: 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;poll: 2

> I was able to use this method, which seems to work. Is this the correct  
> usage of "|| true"?
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Check for ECC's  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: ipmitool sel list | grep -i ecc || true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: sel\_out  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;async: 3  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Ryan1](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Ryan1](https://forum.ansible.com/u/Ryan1)\
**Post date:** [August 14, 2017, 9:58pm UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/8 "2017-08-14T21:58:29Z")

</div>

Thanks for the detailed reply.

- 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

TASK [Check for ECC’s] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [192.168.100.11]: FAILED! =\> {“failed”: true, “msg”: “The conditional check ‘sel\_out.rc \> 1’ failed. The error was: error while evaluating conditional (sel\_out.rc \> 1): ‘dict object’ has no attribute ‘rc’”}

---

<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 14, 2017, 10:13pm UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/9 "2017-08-14T22:13:12Z")

</div>

This usually doesn't happen, I guess it's because of async and/or poll.  
Why are you using them? Remove them and I guess it would work fine.

---

<div class="post-metadata">

**Author:** ![Ryan1](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Ryan1](https://forum.ansible.com/u/Ryan1)\
**Post date:** [August 15, 2017, 6:49pm UTC](https://forum.ansible.com/t/add-conditionals-to-debug-msg/25226/10 "2017-08-15T18:49:04Z")

</div>

If i remove async and poll your solution does work. I was using them because depending on the platform the playbook is executed on, the time to complete can vary a lot. I will play around with not using the async and poll and see what the result is, as I am still new to Ansible obviously.

Thanks!
