0
我希望任何人都可以幫助我。我是新手,我一直在尋找最近幾天的解決方案,但沒有運氣。我想根據任務中寄存器變量的輸出創建一個變量。ansible - 根據寄存器變量的輸出創建新變量
下面是我使用的變量:
ports:
access:
- int:
- "Ethernet1/0"
- "Ethernet1/1"
voice: 10
data: 100
- int:
- "Ethernet2/0"
- "Ethernet2/1"
voice: 11
data: 101
trunk:
- int:
- "Ethernet0/0"
- "Ethernet0/1"
allowed_vlans: "10,100,11,101"
range: "range e0/0 - 1"
- name: show interface swicthport
ios_command:
provider: "{{ provider }}"
commands:
- show interface {{ item.1 }} switchport
register: show_out
with_subelements:
- "{{ ports.trunk }}"
- int
- debug: var=show
下面是調試輸出:
TASK [debug] *******************************************************************
ok: [acc_sw_01] => {
"show_out": {
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"invocation": {
"module_args": {
"auth_pass": null,
"authorize": false,
"commands": [
"show interface Ethernet0/0 switchport"
],
"host": "acc_sw_01",
"interval": 1,
"match": "all",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": null,
"provider": {
"host": "acc_sw_01",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"transport": "cli",
"username": "rey"
},
"retries": 10,
"ssh_keyfile": null,
"timeout": 10,
"transport": "cli",
"use_ssl": true,
"username": "rey",
"validate_certs": true,
"wait_for": null
},
"module_name": "ios_command"
},
"item": [
{
"allowed_vlans": "10,100,500",
"range": "range e0/0 - 1"
},
"Ethernet0/0"
],
"stdout": [
"Name: Et0/0\nSwitchport: Enabled\nAdministrative Mode: dynamic auto\nOperational Mode: down\nAdministrative Trunking Encapsulation: negotiate\nNegotiation of Trunking: On\nAccess Mode VLAN: 1 (default)\nTrunking Native Mode VLAN: 1 (default)\nAdministrative Native VLAN tagging: enabled\nVoice VLAN: none\nAdministrative private-vlan host-association: none \nAdministrative private-vlan mapping: none \nAdministrative private-vlan trunk native VLAN: none\nAdministrative private-vlan trunk Native VLAN tagging: enabled\nAdministrative private-vlan trunk encapsulation: dot1q\nAdministrative private-vlan trunk normal VLANs: none\nAdministrative private-vlan trunk associations: none\nAdministrative private-vlan trunk mappings: none\nOperational private-vlan: none\nTrunking VLANs Enabled: ALL\nPruning VLANs Enabled: 2-1001\nCapture Mode Disabled\nCapture VLANs Allowed: ALL\n\nProtected: false\nAppliance trust: none"
],
"stdout_lines": [
[
"Name: Et0/0",
"Switchport: Enabled",
"Administrative Mode: dynamic auto",
"Operational Mode: down",
"Administrative Trunking Encapsulation: negotiate",
"Negotiation of Trunking: On",
"Access Mode VLAN: 1 (default)",
"Trunking Native Mode VLAN: 1 (default)",
"Administrative Native VLAN tagging: enabled",
"Voice VLAN: none",
"Administrative private-vlan host-association: none ",
"Administrative private-vlan mapping: none ",
"Administrative private-vlan trunk native VLAN: none",
"Administrative private-vlan trunk Native VLAN tagging: enabled",
"Administrative private-vlan trunk encapsulation: dot1q",
"Administrative private-vlan trunk normal VLANs: none",
"Administrative private-vlan trunk associations: none",
"Administrative private-vlan trunk mappings: none",
"Operational private-vlan: none",
"Trunking VLANs Enabled: ALL",
"Pruning VLANs Enabled: 2-1001",
"Capture Mode Disabled",
"Capture VLANs Allowed: ALL",
"",
"Protected: false",
"Appliance trust: none"
]
],
"warnings": []
},
基於輸出,我怎麼可以設置一個變量,值「啓用中繼VLAN:ALL」?
預先感謝,我很欣賞任何迴應
編輯: @Konstantin我感謝您的幫助和耐心。你的回答是正確的,只是我的問題並不清楚,你對於with_subelements也沒有必要使用它。我編輯下面的任務。
- name: show interface swicthport
ios_command:
provider: "{{ provider }}"
commands:
- show interface switchport | include Name|Trunking VLANs Enabled
register: show_out
調試輸出:
TASK [debug] *******************************************************************
ok: [acc_sw_01] => {
"show_out": {
"changed": false,
"stdout": [
"Name: Et0/0\nTrunking VLANs Enabled: 11\nName: Et0/1\nTrunking VLANs Enabled: ALL"
],
"stdout_lines": [
[
"Name: Et0/0",
"Trunking VLANs Enabled: 11",
"Name: Et0/1",
"Trunking VLANs Enabled: ALL",
]
],
"warnings": []
}
}
我怎麼能夠創建基於輸出字典或散列的名單?是這樣的:
- name: "Et0/0"
trunkning_vlans_enabled:11
- name: "Et0/1"
trunking_vlans_enabled: all
or
Eth0/0:
trunking_vlans_enable: 11
Eth0/1:
trunkning_vlans_enable: all
感謝
提供的輸出肯定被截斷(它應該有'results','物品「等)。並請更改您的問題:您是否需要獲取具有特定行的特定界面? –
在你的例子中'show_out.results'是一個可能有多個接口的列表。在這種情況下你想要什麼? –
嗨康斯坦丁, 對不起,我刪除了以前的評論是不完整的,但無論如何,我想與「中繼VLAN啓用:ALL」的線,謝謝 – reynold