0

I have a use case where I have set of hosts in my inventory file say inventory.txt as

[ios]
a.b.c.d
s.i.s.i
x.x.c.c

I need to check if vstack is enabled in each of these hosts, if enabled I need to disable it as a self healing process.

My Playbook :

---
- hosts: all

  connection: network_cli

  become: true
  become_method: enable
  tasks:
    - name: Verify whether vstack feature is enabled

      ios_command:
        commands:
          - show vstack config | incl Role
      vars:
        ansible_command_timeout: 30
      register: showvstack

    - debug:
       var: showvstack.stdout
  #   when: showvstack.stdout is regex("'Role.*'")
    #- name: set regex pattern
     # set_fact:
      #  regex_pattern: "^.*Client (SmartInstall enabled).*$"

    - name: Check with when condition
      debug:
        msg: "Vstack Enabled!!!!"
      when: showvstack.stdout | join('') | match('Client (SmartInstall enabled)') or showvstack.stdout | join('') | match('Client')


    - name: If vstack is enabled on switch disable vstack
      ios_config:
       lines:
         - no vstack
      when: showvstack.stdout | join('') | search('Client')

   ----------------------------------------------------------------------------

Play result

TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [a.b.c.d]
ok: [x.v.b.n]
ok: [z.z.x.c]

TASK [Verify whether vstack feature is enabled] *********************************************************************************************************************************************
ok: [a.b.c.d]
ok: [x.v.b.n]
ok: [z.z.x.c]

TASK [debug] ********************************************************************************************************************************************************************************
ok: [a.b.c.d] => {
    "showvstack.stdout": [
        "Role: Client (SmartInstall enabled)"
    ]
}
ok: [x.v.b.n] => {
    "showvstack.stdout": [
        "Role: NA"
    ]
}
ok: [z.z.x.c] => {
    "showvstack.stdout": [
        "Role: Client"
    ]
}

TASK [Check with when condition] ************************************************************************************************************************************************************
skipping: [a.b.c.d]
skipping: [x.v.b.n]
skipping: [z.z.x.c]

TASK [If vstack is enabled on switch disable vstack] ****************************************************************************************************************************************
skipping: [a.b.c.d]
changed: [x.v.b.n]
changed: [z.z.x.c]

PLAY RECAP **********************************************************************************************************************************************************************************
a.b.c.d             : ok=4    changed=1    unreachable=0    failed=0
x.v.b.n                 : ok=3    changed=0    unreachable=0    failed=0
z.z.x.c    : ok=4    changed=1    unreachable=0    failed=0

Since some cisco switches gives Role: Client and some cisco switches gives Role: Client Smart Install enabled, the above play will print vstack present even for switch which do not have vstack in it as I am searching for Client and it also takes Role: Client Smartinstall Disabled as Client string is present in it. My question is there a better way to print msg saying Vstack is present even when I have a condition saying Role: Client Smartinstall Disabled

0

Just run the command with the when conditional.

tasks
  - name: Run show command
    ios_command: show vstack config | incl Role
    register: showvstack

  - name: disable vstack
    ios_command: # whatever command disables vstack
    when: "'Role: Enabled' in showvstack"

It will be skipped when the condition is not met.

4
  • Thanks Gerald, I will try and let you know. – pinkbask Jul 23 '19 at 13:31
  • I have a problem with the string Role:Client as some switches throw output as Role: client some as Role :client (Smartinstall enabled), in that case how do I write an assert statement and based on its output I need to loop the hosts which do not comply and configure using no vstack command – pinkbask Jul 24 '19 at 12:39
  • No idea without seeing actual output of the commands you are running. – Gerald Schneider Jul 24 '19 at 12:50
  • I have added the code and the output and what I am expecting as output – pinkbask Jul 26 '19 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.