Add conditionals to debug msg

101 views
Skip to first unread message

Ryan

unread,
Aug 10, 2017, 4:55:10 PM8/10/17
to Ansible Project
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


Dick Visser

unread,
Aug 11, 2017, 1:32:55 AM8/11/17
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9e896ba5-12df-4a8b-8cff-0dec822277bd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Dick Visser
GÉANT

Want to join us? We're hiring: https://www.geant.org/jobs

Ryan

unread,
Aug 11, 2017, 2:25:52 AM8/11/17
to Ansible Project

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": []}

Ryan

unread,
Aug 11, 2017, 2:36:41 AM8/11/17
to Ansible Project
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.

Kai Stian Olstad

unread,
Aug 11, 2017, 5:51:29 AM8/11/17
to ansible...@googlegroups.com
On 11. aug. 2017 08:25, Ryan wrote:
>
> 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
> 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": []} >

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.


--
Kai Stian Olstad

Ryan

unread,
Aug 11, 2017, 10:09:16 PM8/11/17
to Ansible Project, ansible-pr...@olstad.com
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') }}"



Kai Stian Olstad

unread,
Aug 12, 2017, 4:43:10 AM8/12/17
to ansible...@googlegroups.com
On 12. aug. 2017 04:09, Ryan wrote:
> 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.


--
Kai Stian Olstad

Ryan

unread,
Aug 14, 2017, 5:58:29 PM8/14/17
to Ansible Project, ansible-pr...@olstad.com
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'"}

Kai Stian Olstad

unread,
Aug 14, 2017, 6:13:25 PM8/14/17
to ansible...@googlegroups.com
On 14. aug. 2017 23:58, Ryan wrote:
> 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'"}
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.


--
Kai Stian Olstad

Ryan

unread,
Aug 15, 2017, 2:49:04 PM8/15/17
to Ansible Project, ansible-pr...@olstad.com
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!
Reply all
Reply to author
Forward
0 new messages