2017-02-28 3 views
0

Ich hoffe, dass mir jemand helfen könnte. Ich bin neu zu verstehen, und ich habe nach Lösungen für die letzten paar Tage gesucht, aber kein Glück. Ich wollte eine Variable basierend auf der Ausgabe der Registervariablen in Tasks erstellen.ansible - neue Variable basierend auf der Ausgabe der Registervariable erstellen

Im Folgenden finden Sie die Variablen I verwendet:

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 

Unterhalb der Ausgabe von debug ist:

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

Basierend auf der Ausgabe, wie ich eine Variable mit ein Wert "Trunking VLANs Enabled: ALL"? schätzen

Vielen Dank im Voraus und ich jede Antwort

EDIT: @Konstantin Ich schätze Ihre Hilfe und Geduld. Deine Antwort war richtig, nur dass meine Frage nicht klar ist, hast du auch recht bezüglich der Mitbenutzungselemente keine Notwendigkeit sie zu benutzen. Ich bearbeite die folgende Aufgabe.

- name: show interface swicthport 
    ios_command: 
    provider: "{{ provider }}" 
    commands: 
    - show interface switchport | include Name|Trunking VLANs Enabled 
    register: show_out 

Debug-Ausgabe:

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

Wie bin ich in der Lage, eine Liste von Wörterbüchern oder Hashes basierend auf der Ausgabe zu erstellen? so etwas wie:

- 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 

Dank

+0

bereitgestellt Ausgabe definitiv abgeschnitten wird (es sollte 'results' haben,' Gegenstände usw.). Und bitte formulieren Sie Ihre Frage neu: Benötigen Sie eine spezifische Schnittstelle, die diese spezifische Leitung in ihrer Konfiguration hat? –

+0

In Ihrem Beispiel 'show_out.results' ist eine Liste mit möglicherweise mehreren Schnittstellen beschrieben. Welche Linie willst du in diesem Fall? –

+0

Hallo Konstantin, Entschuldigung, ich löschte den vorherigen Kommentar, der unvollständig war, aber trotzdem wollte ich die Zeile mit "Trunking VLANs Enabled: ALL", danke – reynold

Antwort

0

nicht sicher, was Sie wirklich wollen, aber Sie können mit diesem Beispiel beginnen:

--- 
- 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 }}" 
Verwandte Themen