收集已配置 IP 地址的列表

Gather a List of Configured IP Addresses

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

我想要的输出是使用命令 'show configuration | display set| match "inet6 address" '

生成的

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

- 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."
    }
}

我只想要以下行:

set interfaces unit X family inet6 address XXXX:YYYY:ZZZZ:1234::1/127

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

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

这是操作方法。由于您的 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 条件,它会将给定的行添加到结果 interfaces 列表中。第二个任务打印该变量。第 3 个任务将结果保存到 /tmp/ 下的文件中。

希望对您有所帮助