ansible正确的方法来分割扩展变量并创建新变量以在其他任务中使用?

问题描述 投票:0回答:1

运行 ansible 2.10.17

当我尝试扮演一个角色时,我不断得到

% Invalid input detected at '^' marker.
。 这是我的代码。请注意,“filter”、“trust”和“untrust”作为额外变量通过
-e
传入。

-- name: Include vars
  include_vars:
    file: ../../../settings.yaml
    name: settings

- name: Extract tags from trust + untrust variables
  set_fact:
    trust_tag_filter: "{ {{ trust }}.split(':')[0] }"
    untrust_tag_filter: "{ {{ untrust }}.split(':')[0] }"

- name: Routers configuration, filter "{{ filter }}", with trust tag filter "{{ trust_tag_filter }}", and untrust tag filter "{{ untrust_tag_filter }}"
  iosxr_command:
    commands:
    - show run formal object-group network ipv4 | u egrep {{ filter }}
    - show run ipv4 access-list acl_trust_in | u egrep {{ filter }}
    - show run formal interface | u egrep "BVI{{ trust_tag_filter }}"
    - show run formal interface | u egrep "BVI{{ untrust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether21.{{ trust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether21.{{ untrust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether22.{{ trust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether22.{{ untrust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether23.{{ trust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether23.{{ untrust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether24.{{ trust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether24.{{ untrust_tag_filter }}"
    - show run l2vpn bridge group TRUST bridge-domain "VLAN{{ trust_tag_filter }}"
    - show run l2vpn bridge group UNTRUST bridge-domain "VLAN{{ untrust_tag_filter }}"
    - show run router static address-family ipv4 unicast | utility egrep 'router\ static|family|{{settings.lan_subnet_prefix}}{{filter}}|10.30.{{filter}}'
    - show evpn evi | u egrep 'VPN-ID|{{ trust_tag_filter }}|{{ untrust_tag_filter }}'

  register: response

- debug: msg="{{ response.stdout }}"

但是当我运行它时,我不断收到此错误

The full traceback is:
  File "/tmp/ansible_iosxr_command_payload_kxd7v36e/ansible_iosxr_command_payload.zip/ansible_collections/cisco/iosxr/plugins/module_utils/network/iosxr/iosxr.py", line 586, in run_commands
    return connection.run_commands(commands=commands, check_rc=check_rc)
  File "/tmp/ansible_iosxr_command_payload_kxd7v36e/ansible_iosxr_command_payload.zip/ansible/module_utils/connection.py", line 200, in __rpc__
    raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
fatal: [router.our.domain]: FAILED! => changed=false
  invocation:
    module_args:
      commands:
      - show run formal object-group network ipv4 | u egrep 89
      - show run ipv4 access-list acl_trust_in | u egrep 89
      - show run formal interface | u egrep "BVI{ 1234:192.168.20.0/29.split(':')[0] }"
      - show run formal interface | u egrep "BVI{ 1235:192.168.30.0/29.split(':')[0] }"
      - show run formal interface | u egrep "Bundle-Ether21.{ 1234:192.168.20.0/29.split(':')[0] }"
      - show run formal interface | u egrep "Bundle-Ether21.{ 1235:192.168.30.0/29.split(':')[0] }"
      - show run formal interface | u egrep "Bundle-Ether22.{ 1234:192.168.20.0/29.split(':')[0] }"
      - show run formal interface | u egrep "Bundle-Ether22.{ 1235:192.168.30.0/29.split(':')[0] }"
      - show run formal interface | u egrep "Bundle-Ether23.{ 1234:192.168.20.0/29.split(':')[0] }"
      - show run formal interface | u egrep "Bundle-Ether23.{ 1235:192.168.30.0/29.split(':')[0] }"
      - show run formal interface | u egrep "Bundle-Ether24.{ 1234:192.168.20.0/29.split(':')[0] }"
      - show run formal interface | u egrep "Bundle-Ether24.{ 1235:192.168.30.0/29.split(':')[0] }"
      - show run l2vpn bridge group TRUST bridge-domain "VLAN{ 1234:192.168.20.0/29.split(':')[0] }"
      - show run l2vpn bridge group UNTRUST bridge-domain "VLAN{ 1235:192.168.30.0/29.split(':')[0] }"
      - show run router static address-family ipv4 unicast | utility egrep 'router\ static|family|10.35.89|10.30.89'
      - show evpn evi | u egrep 'VPN-ID|{ 1234:192.168.20.0/29.split(':')[0] }|{ 1235:192.168.30.0/29.split(':')[0] }'
      interval: 1
      match: all
      provider: null
      retries: 10
      wait_for: null
  msg: |-
    show run formal interface | u egrep "BVI{ 1234:192.168.20.0/29.split(':')[0] }"
                                                                                                   ^
    % Invalid input detected at '^' marker.
    RP/0/RP0/CPU0:router#

为什么?我是否没有正确定义变量?

python variables split ansible cisco
1个回答
0
投票

想通了。两个问题。

  1. 我没有正确分裂。正确的方法是在变量内部进行拆分
trust_tag_filter: "{{ trust.split(':')[0] }}"
untrust_tag_filter: "{{ untrust.split(':')[0] }}"
  1. 我正在查询带双引号的 VLAN,必须将其删除。
- show run l2vpn bridge group TRUST bridge-domain VLAN{{ trust_tag_filter }}
- show run l2vpn bridge group UNTRUST bridge-domain VLAN{{ untrust_tag_filter }}

调整后的代码如下:

-- name: Include vars
  include_vars:
    file: ../../../settings.yaml
    name: settings

- name: Extract tags from trust + untrust variables
  set_fact:
    trust_tag_filter: "{{ trust.split(':')[0] }}"
    untrust_tag_filter: "{{ untrust.split(':')[0] }}"

- name: Routers configuration, filter "{{ filter }}", with trust tag filter "{{ trust_tag_filter }}", and untrust tag filter "{{ untrust_tag_filter }}"
  iosxr_command:
    commands:
    - show run formal object-group network ipv4 | u egrep {{ filter }}
    - show run ipv4 access-list acl_trust_in | u egrep {{ filter }}
    - show run formal interface | u egrep "BVI{{ trust_tag_filter }}"
    - show run formal interface | u egrep "BVI{{ untrust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether21.{{ trust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether21.{{ untrust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether22.{{ trust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether22.{{ untrust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether23.{{ trust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether23.{{ untrust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether24.{{ trust_tag_filter }}"
    - show run formal interface | u egrep "Bundle-Ether24.{{ untrust_tag_filter }}"
    - show run l2vpn bridge group TRUST bridge-domain VLAN{{ trust_tag_filter }}
    - show run l2vpn bridge group UNTRUST bridge-domain VLAN{{ untrust_tag_filter }}
    - show run router static address-family ipv4 unicast | utility egrep 'router\ static|family|{{settings.lan_subnet_prefix}}{{filter}}|10.30.{{filter}}'
    - show evpn evi | u egrep 'VPN-ID|{{ trust_tag_filter }}|{{ untrust_tag_filter }}'

  register: response

- debug: msg="{{ response.stdout }}"
© www.soinside.com 2019 - 2024. All rights reserved.