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.

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/593b3798-a437-41b2-b4b4-65afa3e13bff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to