
使用 ansible 2.9.9 在 Windows 10 和 Ubuntu 上執行 WISL。我是 Ansible 的新手。我在 Cisco 設備上執行 show 來產生運行給定網路協定的介面。然後我想提取接口並在它們上執行命令。在這種情況下要關閉協定。理想情況下,show 指令可以輕鬆更改。對於許多協議來說,這是我檢查此狀態的一致方法。 Ansible 可能有多種方式儲存此協定資訊。也許有事實根據?我在以下位置找到了使用 ios_config 的範例https://docs.ansible.com/ansible/latest/modules/ios_config_module.html但介面是硬編碼的,如幫助範例所示:
- name: configure ip helpers on multiple interfaces
ios_config:
lines:
- ip helper-address 172.26.1.10
- ip helper-address 172.26.3.8
parents: "{{ item }}"
with_items:
- interface Ethernet1
- interface Ethernet2
- interface GigabitEthernet1
我的嘗試如下,它為我提供了兩個處於多播活動狀態的介面。但是接下來要在迴圈中對這些介面執行什麼操作呢? :
tasks:
- name: Gather interfaces running PIM
ios_command:
commands:
- show ip pim interface
register: pim
- name: Write PIM interface data to file
copy:
content: "{{pim.stdout_lines[0]}}"
dest: "backups/{{ansible_alias}}-pim-interfaces.txt"
- name: Glean PIM INTF's
shell: cat backups/{{ ansible_alias }}-pim-interfaces.txt | tr ' ' '\n' | grep 'GigabitEthernet'
register: pim
- debug: msg='{{ pim.stdout_lines }}'
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [10.239.121.2] => {
"msg": [
"GigabitEthernet0/0/0",
"GigabitEthernet0/0/1.125"
]
}
非常感謝您的指導。
答案1
這是您使用loop
(從 2.5 開始,這也導致所有with_
指令被棄用,儘管許多文件尚未反映這一點)。
修改 Ansible 範例可以得到:
- name: configure ip helpers on multiple interfaces
ios_config:
lines:
- ip helper-address 172.26.1.10
- ip helper-address 172.26.3.8
parents: item
loop: '{{ ["interface "]|product(pim.stdout_lines)|map("join")|list }}'
僅檢查輸出:
- debug:
var: item
loop: '{{ ["interface "]|product(pim.stdout_lines)|map("join")|list }}'
這loop
是修改自如何在 Ansible 中為清單中的每個字串加上前綴。