Add conditionals to debug msg

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

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

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

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.

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.

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’) }}”

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.

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.

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!