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