특정 조건을 충족하는 호스트 집합을 구성 가능

특정 조건을 충족하는 호스트 집합을 구성 가능

내 인벤토리 파일에 Inventory.txt라는 호스트 세트가 있는 사용 사례가 있습니다.

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

이러한 각 호스트에서 vstack이 활성화되어 있는지 확인해야 합니다. 활성화된 경우 자가 복구 프로세스로 이를 비활성화해야 합니다.

내 플레이북:

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

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

플레이 결과

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

일부 cisco 스위치는 Role: Client를 제공하고 일부 cisco 스위치는 Role: Client Smart Install 활성화를 제공하므로 위의 플레이에서는 클라이언트를 검색할 때 vstack이 없는 스위치에 대해서도 vstack 존재를 인쇄하고 Role: Client도 사용합니다. 클라이언트 문자열이 있으므로 Smartinstall이 비활성화되었습니다. 내 질문은 역할: 클라이언트 Smartinstall 비활성화됨이라는 조건이 있는 경우에도 Vstack이 존재한다는 메시지를 인쇄하는 더 좋은 방법이 있다는 것입니다.

답변1

다음과 같이 명령을 실행하면 됩니다.when가정 어구.

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"

조건이 충족되지 않으면 건너뜁니다.

관련 정보