stdout looks empty, but 'when' thinks it isn't?

As an Ansible newcomer, I’m trying to write some scripts to standardize Cisco switch configuration. Unfortunately since Cisco doesn’t offer a way to clear existing configuration before setting (in this instance I’m wanting to erase and standardize syslog targets, which is mostly configured as one value, but on some switches may have never been set, or may have been pointed at one of three other hosts in addition to the normal), I decided the easiest way was to first fetch the current setting(s), then iterate through and erase them, then set the correct target.

This works fine, but it fails on a switch that has no current value set, so I used a ‘when’ clause to check if stdout from the collection task is empty. However, this isn’t working. If I print via debug the value of stdout, it certainly looks empty so why isn’t the when clause working?

Any help on what I’m doing wrong (or a better way to do it) appreciated? For the moment I can kludge around it by setting a value before I delete all the values, so that I know it should always have something and not worry about it, but that’s bad practice and I’d prefer to know what I’m doing wrong for next time!

`

  • hosts: switches
    gather_facts: False
    vars:
    cli:
    host: “{{ inventory_hostname }}”
    username: user
    password: password
    auth_pass: password
    transport: cli
    tasks:

  • name: Collect logging configuration
    connection: local
    become: false
    register: syslog
    ios_command:
    provider: “{{ cli }}”
    authorize: yes
    commands:

  • show running-config | include logging host

  • debug: msg=“empty”
    when: syslog.stdout == “”

  • debug: msg=“not empty”
    when: syslog.stdout != “”

  • debug: var=syslog.stdout

  • name: Remove existing syslog config
    connection: local
    become: false
    when: syslog.stdout != “”
    ios_command:
    provider: “{{ cli }}”
    authorize: yes
    commands:

  • configure terminal

  • no {{ item }}

  • exit
    with_items: “{{ syslog.stdout_lines }}”

`

Output:

`

PLAY [switches] ****************************************************************

TASK [Collect logging configuration] *******************************************
ok: [switchname]

TASK [debug] *******************************************************************
skipping: [switchname]

TASK [debug] *******************************************************************
ok: [switchname] => {
“msg”: “not empty”
}

TASK [debug] *******************************************************************
ok: [switchname] => {
“syslog.stdout”: [
“”
]
}

TASK [Remove existing syslog config] *******************************************
failed: [switchname] (item=) => {“commands”: [“configure terminal”, "no ", “exit”], “failed”: true, “item”: “”, “msg”: “matched error in response: no \r\n% Incomplete command.\r\n\r\nSwitchName(config)#”}

`

Thanks,
Steve.

It's not empty, it does contain a list.
So if you do this I think it should work
when: syslog.stdout[0] != ""

Aha - yes, had missed that in the output. Have been trying to work out why it differed from the example use of ‘when’ to check for empty stdout in the documentation, but I now see in the module docs for ios_command, that since it takes a list of commands as input, it also returns a list of stdout values rather than a single value. That makes sense and explains the difference.

Many thanks for pointing me the right way.

Also, I noticed you are using ios_command to send configuration statements. Please switch to using ios_config. In 2.2, ios_command will no longer allow you to send configuration commands