【问题标题】:Find a value in a structured file with Ansible使用 Ansible 在结构化文件中查找值
【发布时间】:2021-05-28 08:07:20
【问题描述】:

关于如何使用 Ansible 在结构化文件中查找值,我有一个小问题。我看过 lineinfile 但我不太确定它是否会有所帮助。如果我们假设我的文件看起来像这样(实际上它要长得多,但由于明显的原因我不能在这里发布它^^)

################## junos.conf ##################

system {
    auto-snapshot;
    root-authentication {
        encrypted-password ## SECRET-DATA
    }
    services {
        ssh;
        netconf ssh;
        scp;
    }
    syslog {
        user * {
            any emergency;
        }
        file messages {
            any notice;
            authorization info;
        }
        file interactive-commands {
            interactive-commands any;
        }
    }
    processes {
        dhcp-service {
            traceoptions {
                file dhcp_logfile size 10m;
                level all;
                flag all;
            }
        }
    }
}
interfaces {
    irb {
        unit 0 {
            family inet {
                dhcp {
                    vendor-id Juniper-ex4300-48t;
                }
            }
        }
    }
    vme {
        unit 0 {
            family inet {
                address 172.0.0.1/24
            }
        }
    }
}
forwarding-options {
    storm-control-profiles default {
        all;
    }
}
vlans {
    default {
        vlan-id 1;
        l3-interface irb.0;
    }
}

这是一个.conf 文件,但它看起来像一个结构化文件。想象一下,我想找到一种方法在 Ansible 剧本中获取价值 interfaces->vme->unit 0->family inet,我该怎么做?我可以在 Ansible 中使用哪个解析器?

我已经阅读了这个页面,但我真的不知道要使用哪个解析器以及如何使用它:https://docs.ansible.com/ansible/latest/network/user_guide/cli_parsing.html

谢谢, 最大

【问题讨论】:

  • 你不能在 Ansible 中编程。 Ansible 将所有数据存储在一个包含所有副作用的大全局变量中。从历史上看,这使得嵌套循环成为问题(Ansible 现在有一个解决方法),当然它使得解析常规语法之外的任何东西都变得不可能。每当您需要超出现有模块或 Jinja (example) 功能的复杂功能时,您必须用 Python 编写自己的模块。或者你必须将你的数据转换成 Ansible 可以理解的格式,比如 JSON。
  • 感谢@ceving 的消息,我知道这些东西不是在 Ansible 中可编程的,但我期待像 Ansible 这样的东西,它将其转换为 JSON,然后我就可以从 JSON 输出中获取我想要的任何值。
  • 您可以使用Juniper Networks Ansible modules吗?
  • @a-maxime,关于您的评论“_was expecting like an Ansible that will convert it to JSON _”,您是否可以通过to convert Juniper config to json 即通过show config | display json 在导出到文件?

标签: parsing ansible python-textfsm


【解决方案1】:

关于你的问题

我可以在 Ansible 中使用哪个解析器?

如果您之前无法以 JSON 结构化格式导出配置 (show config | display json),因为它在示例中不受您的控制,您可能需要处理给定的结构。

这是一个.conf 文件,但它看起来像一个结构化文件。

由于结构看起来不像 Parsing semi-structured text with Ansible 中的可用模板之一,您可能需要寻找其他选项。

我假设您不想通过 file_lookup 模块读取整个文件并在 Ansible 中完全解析它。

Lookup Plugins 似乎也没有实现提供的结构。 INI lookup 似乎也不合适。

想象一下,我想找到一种方法在 Ansible 剧本中获取值 interfaces->vme->unit 0->family inet,我该怎么做?

如果你的配置文件只有那个结构并且没有改变,我们不知道

...实际上它要长得多...

以下方法可能会奏效一段时间:

- name: Read IP address from Junos config file
  shell:
    cmd: grep -o "address.*" /tmp/junos.conf | cut -f 2 -d " " | cut -f 1 -d "/"
  register: ip_address
  warn: false
  check_mode: false
  changed_when: false
  delegate_to: localhost
  tags: junos_conf

- name: Show IP address
  debug: 
    msg: "{{ ip_address }}"  
  tags: junos_conf

还可以选择编写自己的自定义插件。

感谢

来自 cmets 的链接

【讨论】:

  • 嗯,这是我认为公平的好方法。多亏了你的方法,我已经设法获得了所有的 conf 文件地址。我会定制它并在这里发布给其他人!
猜你喜欢
  • 2017-02-02
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多