2

我想提取将 IPv6 地址分配给接口的 Juniper 配置行,并将该输出保存到文件中。

我所追求的输出是使用命令“show configuration |”生成的。展示套装| 匹配 "inet6 地址" '

我正在构建一个 Ansible 剧本并排除错误以完成以下任务。它基本上为我提供了完整的界面配置,我只想将其缩小到适合手动命令中“匹配”行的行。注释掉的过滤器不起作用,我找不到以我理解的方式解释过滤器的文档。

- name: "Get selected configuration hierarchies"
  juniper_junos_config:
    host: "{{ ansible_host }}"
    retrieve: "committed"
    config_mode : "private"
    filter: "<configuration><interfaces/></configuration>"
    #filter: "<configuration><interfaces/><family/><inet6/><address/></configuration>"
    #filter: "inet6/address"
    format: "set"
    options:
      inherit: "inherit"
    dest: "{{ inventory_hostname }}-inet6-config"
  register: response
- name: "Print result"
  debug:
    var: response

输出:

ok: [LAB-QFX5110-1] => {
    "response": {
        "changed": false,
"config": "\nset interfaces xe-0/0/0 apply-groups-except jumbo-frames\nset interfaces xe-0/0/0 description \"Test Laptop - DMZ;000\"\nset interfaces xe-0/0/0 gigether-options 802.3ad ae12\n<SNIP>\nset interfaces lo0 unit 10 family inet address 100.126.0.x/32\nset interfaces lo0 unit 10 family inet6 address ABCD:EF00:0000:01c4::1/128\n<SNIP>/n",
"config_lines": [
            "",
            "set interfaces xe-0/0/0 apply-groups-except jumbo-frames",
            "set interfaces xe-0/0/0 description \"Test Laptop - DMZ;000\"",
            "set interfaces xe-0/0/0 gigether-options 802.3ad ae12",
            "<SNIP>",
            "set interfaces lo0 unit 10 family inet address 100.126.0.x/32",
            "set interfaces lo0 unit 10 family inet6 address ABCD:EF00:0000:01c4::1/128",
            "<SNIP>",
        ],
        "failed": false,
        "msg": "Configuration has been: opened, retrieved, closed."
    }
}

我只想要这样的行:

设置接口单元 X 系列 inet6 地址 XXXX:YYYY:ZZZZ:1234::1/127

但我似乎无法插入正确的过滤器。

我还会提到,如果有更好的方法来收集它,我愿意探索它。看起来这就是 Ansible 被创建来执行的任务。

4

1 回答 1

0

这是怎么做的。由于您的response字典包含按行拆分的输出,因此我们将使用config_lines键,逐行处理:

代码:

---
- hosts: localhost
  gather_facts: false
  vars:
    response:
      changed: false
      config: |2-

        set interfaces xe-0/0/0 apply-groups-except jumbo-frames
        set interfaces xe-0/0/0 description "Test Laptop - DMZ;000"
        set interfaces xe-0/0/0 gigether-options 802.3ad ae12
        <SNIP>
        set interfaces lo0 unit 10 family inet address 100.126.0.x/32
        set interfaces lo0 unit 10 family inet6 address ABCD:EF00:0000:01c4::1/128
        <SNIP>/n
      config_lines:
      - ''
      - set interfaces xe-0/0/0 apply-groups-except jumbo-frames
      - set interfaces xe-0/0/0 description "Test Laptop - DMZ;000"
      - set interfaces xe-0/0/0 gigether-options 802.3ad ae12
      - "<SNIP>"
      - set interfaces lo0 unit 10 family inet address 100.126.0.x/32
      - set interfaces lo0 unit 10 family inet6 address ABCD:EF00:0000:01c4::1/128
      - "<SNIP>"
      failed: false
      msg: 'Configuration has been: opened, retrieved, closed.'

  tasks:

  - name: find entries containing inet6 address, add to results
    set_fact:
      interfaces: "{{ interfaces | default([]) + [item] }}"
    when: item is search('inet6 address')
    with_items:
    - "{{ response.config_lines }}"

  - debug:
      var: interfaces

  - name: save results to file
    template:
      src: results.j2
      dest: /tmp/results.out

您将需要一个 jinja2 模板来完成最后一个任务(在与您的剧本相同的目录下),其内容:

结果.j2:

# processing results:

{% for interface in interfaces -%}
{{ interface }}
{%- endfor %}

第一个任务解析每一行,如果满足when条件,则将给定的行添加到 resultsinterfaces列表中。第二个任务打印该变量。第三个任务将结果保存到 /tmp/ 下的文件中。

希望能帮助到你

于 2019-08-30T13:47:30.927 回答