ansible - 根据寄存器变量的输出创建新变量

时间:2017-02-28 12:30:42

标签: ansible

我希望有人能帮助我。我是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

以下是debug的输出:

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": []
        }, 

根据输出,我如何设置一个值为#34的变量;中继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

由于

1 个答案:

答案 0 :(得分:0)

不确定你真正想要什么,但你可以从这个例子开始:

---
- hosts: localhost
  gather_facts: no
  vars:
    show_out:
      results:
        - item: Ethernet0/0
          stdout_lines: [
                [
                    "Name: Et0/0",
                    "Switchport: Enabled",
                    "Trunking VLANs Enabled: ALL",
                    "Appliance trust: none"
                ]
            ]
        - item: Ethernet0/1
          stdout_lines: [
                [
                    "Name: Et0/1",
                    "Switchport: Enabled",
                    "Trunking VLANs Enabled: ALL",
                    "Appliance trust: none"
                ]
            ]
  tasks:
    - debug:
        msg: "{{ show_out.results | map(attribute='stdout_lines') | map('first') | map('intersect',['Trunking VLANs Enabled: ALL']) | list }}"