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

360 views
Skip to first unread message

Steve Kersley

unread,
Aug 22, 2016, 8:34:59 AM8/22/16
to Ansible Project
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.

Kai Stian Olstad

unread,
Aug 22, 2016, 9:48:08 AM8/22/16
to ansible...@googlegroups.com
It's not empty, it does contain a list.
So if you do this I think it should work
when: syslog.stdout[0] != ""

--
Kai Stian Olstad

Steve Kersley

unread,
Aug 23, 2016, 10:49:19 AM8/23/16
to Ansible Project, ansible-pr...@olstad.com
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.

Peter Sprygada

unread,
Aug 23, 2016, 10:58:38 AM8/23/16
to ansible...@googlegroups.com, ansible-pr...@olstad.com
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

--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7f4354fc-df5f-4992-b480-595b05a25f43%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages