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

**URL:** <https://forum.ansible.com/t/stdout-looks-empty-but-when-thinks-it-isnt/22342>\
**Category:** Ansible Project\
**Created:** [August 22, 2016, 10:10am UTC](https://forum.ansible.com/t/stdout-looks-empty-but-when-thinks-it-isnt/22342 "2016-08-22T10:10:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Kersley](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@Steve\_Kersley](https://forum.ansible.com/u/Steve_Kersley)\
**Post date:** [August 22, 2016, 10:10am UTC](https://forum.ansible.com/t/stdout-looks-empty-but-when-thinks-it-isnt/22342/1 "2016-08-22T10:10:04Z")

</div>

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.

---

<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 22, 2016, 1:47pm UTC](https://forum.ansible.com/t/stdout-looks-empty-but-when-thinks-it-isnt/22342/2 "2016-08-22T13:47:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve\_Kersley](https://avatars.discourse-cdn.com/v4/letter/s/77aa72/32.png) [@Steve\_Kersley](https://forum.ansible.com/u/Steve_Kersley)\
**Post date:** [August 23, 2016, 9:49am UTC](https://forum.ansible.com/t/stdout-looks-empty-but-when-thinks-it-isnt/22342/3 "2016-08-23T09:49:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![psprygada](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@psprygada](https://forum.ansible.com/u/psprygada)\
**Post date:** [August 23, 2016, 2:58pm UTC](https://forum.ansible.com/t/stdout-looks-empty-but-when-thinks-it-isnt/22342/4 "2016-08-23T14:58:32Z")

</div>

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
