method for deleting ip sla config lines at different routers

13 views
Skip to first unread message

aegiac...@hotmail.com

unread,
Mar 19, 2018, 9:29:32 PM3/19/18
to Ansible Project
Hi guys, i 've been tasked for deletion of "ip sla x" lines at several routers.
I have created this playbook, that works fine with all routers, except the ones which do not have "ip sla x" configured.
This is the playbook:
---
- name: IOS show command
  hosts: test
  connection: local
  gather_facts: no
  
  vars:
    cli:
      username: cisco
      password: cisco

  tasks:
    - ios_command:
        commands:
          - show run | i ip sla [0-9]
        provider: "{{ cli }}"
      register: showcommand
    - debug: msg="{{ showcommand.stdout_lines }}"

    - ios_config:
        lines: no {{item}}
        provider: "{{ cli }}"
      with_items: "{{showcommand.stdout_lines}}"
      when: showcommand.stdout != ""
      register: configcommand

Example, if router has an output for "sh run | inc ip sla" and show "ip sla 1".
ok: [test1] => {
    "msg": [
        [
            "ip sla 1", 
        ]
    ]

then the ios_config will execute "no ip sla 1", and it works perfectly. Even when several ip sla numbers are there from multiple ip sla tracks configurations.

But if the router does not have and ip sla configured, then the output will be an empty but defined 
ok: [test1] => {
    "msg": [
        [
            ""
        ]
    ]

And this will cause an execution of a command as "no ", and that creates an error, because the command is incomplete.

I have been browsing on this, and tested the "when x != "" " statement for "with_items", it works fine to control the loop when the router has an "ip sla x" to remove, but it seems to enter the loop anyway when the output is "" (an empty string), for cases where routers do not have ip sla configured.

I don't know if this is an issue, something that i'm doing wrong, even wrong way to solve this requirement.

What do you think?

if you reach here at reading this email.... thanks for you time ;)

regards

Kai Stian Olstad

unread,
Mar 22, 2018, 4:44:45 PM3/22/18
to ansible...@googlegroups.com
> "*ip
> sla 1*".
> ok: [test1] => {
> "msg": [
> [
> "ip sla 1",
> ]
> ]
>
> then the ios_config will execute "no ip sla 1", and it works perfectly.
> Even when several ip sla numbers are there from multiple ip sla tracks
> configurations.
>
> But if the router does not have and ip sla configured, then the output
> will
> be an empty but defined
> ok: [test1] => {
> "msg": [
> [
> ""
> ]
> ]

You should ha provided the content of the variable showcommand
- debug: var=showcommand

Since I don't use ios_command and don't have the content this might be
completely wrong.

I thing you need to use showcommand.stdout and not
showcommand.stdout_lines.
When using that with with_items you will have the content of each of
them in the variable item so you need to use item in when:

with_items: "{{ showcommand.stdout }}"
when: item != ""


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages