Ansible para configurar un conjunto de hosts que cumplan con una condición específica

Ansible para configurar un conjunto de hosts que cumplan con una condición específica

Tengo un caso de uso en el que tengo un conjunto de hosts en mi archivo de inventario, por ejemplo inventario.txt como

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

Necesito verificar si vstack está habilitado en cada uno de estos hosts; si está habilitado, debo deshabilitarlo como un proceso de autorreparación.

Mi libro de jugadas:

---
- 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')

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

Resultado del juego

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

Dado que algunos conmutadores de Cisco otorgan la Función: Cliente y algunos conmutadores de Cisco otorgan la Función: Cliente Instalación inteligente habilitada, la reproducción anterior imprimirá vstack presente incluso para los conmutadores que no tienen vstack ya que estoy buscando Cliente y también requiere Función: Cliente Smartinstall deshabilitado porque la cadena de cliente está presente en él. Mi pregunta es si existe una mejor manera de imprimir un mensaje que diga que Vstack está presente incluso cuando tengo una condición que dice Rol: Client Smartinstall Disabled.

Respuesta1

Simplemente ejecute el comando con elwhencondicional.

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"

Se omitirá cuando no se cumpla la condición.

información relacionada