【发布时间】: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 输出中获取我想要的任何值。
-
@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