首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于寄存器变量的输出创建新变量

基于寄存器变量的输出创建新变量
EN

Stack Overflow用户
提问于 2017-02-28 12:30:42
回答 1查看 1.2K关注 0票数 0

我希望任何人都能帮助我。我是新来的ansible,过去几天我一直在寻找解决方案,但没有运气。我想根据任务中寄存器变量的输出创建一个变量。

下面是我使用的变量:

代码语言:javascript
复制
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"
代码语言:javascript
复制
- 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

下面是调试的输出:

代码语言:javascript
复制
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": []
        }, 

根据输出,如何设置一个值"Trunking已启用: ALL“的变量?

谢谢,谢谢您的回复。

编辑: @Konstantin我感谢您的帮助和耐心。你的回答是对的,只是我的问题不清楚,你对with_subelements的看法也是对的,不需要使用它。我编辑下面的任务。

代码语言:javascript
复制
- name: show interface swicthport
  ios_command:
    provider: "{{ provider }}"
    commands:
    - show interface switchport | include Name|Trunking VLANs Enabled
  register: show_out

调试输出:

代码语言:javascript
复制
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": []
  }
}

如何根据输出创建字典或散列列表?类似于:

代码语言:javascript
复制
- 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

谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-03-01 19:03:10

不确定您真正想要的是什么,但可以从下面的示例开始:

代码语言:javascript
复制
---
- 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 }}"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42508845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档