Ansible para configurar um conjunto de hosts que atenda a condições específicas

Ansible para configurar um conjunto de hosts que atenda a condições específicas

Eu tenho um caso de uso em que configurei hosts em meu arquivo de inventário, digamos, inventário.txt como

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

Preciso verificar se o vstack está habilitado em cada um desses hosts, se habilitado preciso desabilitá-lo como um processo de autocorreção.

Meu manual:

---
- 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 do jogo

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

Como alguns switches Cisco fornecem Role: Client e alguns switches Cisco fornecem Role: Client Smart Install habilitado, a reprodução acima imprimirá vstack presente mesmo para switches que não possuem vstack nele, pois estou procurando por Client e também assume Role: Client Smartinstall desativado porque a string do cliente está presente nele. Minha pergunta é: existe uma maneira melhor de imprimir uma mensagem dizendo que o Vstack está presente mesmo quando eu tenho uma condição dizendo Função: Cliente Smartinstall desativado

Responder1

Basta executar o comando com owhencondicional.

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"

Ele será ignorado quando a condição não for atendida.

informação relacionada