可配置滿足特定條件的主機集

可配置滿足特定條件的主機集

我有一個用例,我的庫存檔案中有一組主機,例如 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

由於某些思科交換器提供角色:客戶端,而某些思科交換器提供角色:啟用客戶端智慧安裝,因此即使對於其中沒有vstack 的交換機,上述操作也會列印vstack,因為我正在搜尋客戶端,並且它還需要角色:客戶端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"

不符合條件時將被跳過。

相關內容