- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 ansible 中使用 Jinja 过滤器以正确的格式提取我需要的值来处理它。
这是 JSON 格式的数据(我缩短了输出,通常每个项目有更多变量,并不是所有项目都有 IPv4 变量等等):
"interfaces": {
"GigabitEthernet0": {
"arp_timeout": "00:20:00",
"arp_type": "arpa",
"auto_negotiate": true,
"bandwidth": 1000000
},
"GigabitEthernet0/0/0": {
"arp_timeout": "00:20:00",
"arp_type": "arpa",
"auto_negotiate": true,
"bandwidth": 10000
},
"GigabitEthernet0/0/0.3": {
"arp_timeout": "04:00:00",
"arp_type": "arpa",
"bandwidth": 10000,
"delay": 10,
"description": "Private1 MPLS",
"enabled": true,
"encapsulations": {
"encapsulation": "dot1q",
"first_dot1q": "3"
},
"ipv4": {
"10.10.84.2/30": {
"ip": "10.10.84.2",
"prefix_length": "30"
}
然后我使用那个简单的 Jinja 文件管理器来提取我需要的信息,例如接口(interface)名称和 IPv4:
[
{% for interface in interfaces if interfaces[interface]['ipv4'] is defined %}
{
"name": "{{ interface }}",
{% if interfaces[interface]['ipv4'] is defined %}
"prefix": "{{ interfaces[interface]['ipv4'] }}",
{% endif %}
"hostname": "{{ hostname }}"
}{{ ", " if not loop.last else "" }}
{% endfor %}
]
我现在的问题是解析数据看起来像这样:
{
"name": "GigabitEthernet0/0/0.3",
"prefix": "{'10.10.84.2/30': {'ip': '10.10.84.2', 'prefix_length': '30'}}",
"hostname": "Horst1"
},
但我只想像这样嵌套字典中的键:
{
"name": "GigabitEthernet0/0/0.3",
"prefix": "10.10.84.2/30",
"hostname": "Horst1"
},
Jinja 中没有一个简单的方法来从嵌套对象中获取键吗?
最佳答案
这是一个可能更简单的模板,使用 for key, value in dict.items()
构造:
[
{% for name, interface in interfaces.items() if interface.ipv4 is defined %}
{
"name": "{{ name }}",
"prefix": "{{ (interface.ipv4.keys() | list).0 }}",
"hostname": "{{ hostname }}"
}{{ ", " if not loop.last }}
{% endfor %}
]
keys()
方法是来自 Python 的方法,它返回一个表示该字典的键列表的 View 。将其转换回列表并获取其中的第一个元素,您应该可以开始了。
另一种选择是使用 dict2items
,再次获取生成列表的第一个元素并获取其键:
"prefix": "{{ (interface.ipv4 | dict2items).0.key }}",
关于ansible - 使用 Jinja 过滤器从嵌套对象中获取唯一键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71343779/
这是我的代码: {% if {{post.author.profile.image.url}} is None %} 当我运行这段代码时出现错误 Could not parse the remaind
我想将 jinja 循环内的单个 jinja 变量作为参数传递给 python 函数。在这种情况下,我需要将 jinja 循环内的 {{ book.isbn }} 传递给 python 函数才能使用该
问题几乎在标题中。 IntelliJ 是否具有针对 .jinja 的 native 语法高亮显示?和 .j2文件? 如果没有,是否有插件? 最佳答案 这个插件:https://plugins.jetb
我只想循环现有列表并从中创建一个逗号分隔的字符串。 像这样的东西:my_string = 'stuff,stuff,stuff,stuff' 我已经了解loop.last,我只需要知道如何使下面代码中
我在 HTML 表格中添加了复选框,我想知道是否选中了任何复选框。我有一个在 Jquery 中运行的脚本,它告诉我变量中标记的复选框的数量,但我还没有找到如何在 Jinja if 语句条件中使用 Jq
嗨,我觉得我需要为我现在挣扎了几个小时的事情提供答案。我希望有一天这对某人有所帮助。 我一直在尝试自动化 Ansible 部署的模板: 库存内容: [splunk_license] 10.10.113
我想记录我的 Jinja 宏是如何工作的。在 Jinja 中是否有用于记录宏的官方格式,就像有多种格式化 Python 文档字符串的方法一样? 也许是这样的: {% macro my_macro(ar
有没有办法加载自定义标签集 - 甚至是单个自定义标签? 在 django 模板中,我可以这样做: {% load custom_tag_set %} 有没有办法在神社中做到这一点? 最佳答案 存在相同
我正在创建一种表单,用户可以在其中添加一个或两个额外的字段...我正在使用jQuery进行交互,并使用Flask进行模板化,它看起来像这样: Add College or University
所以我试图在 url_for 中嵌套 2 个变量来提取特定人物的照片。这是我的代码 我的图像采用“用户名.png”格式,因此我在开头和结尾添加“_”。 这是它的Python部分: @app.rout
我想做的基础是使用“随机”过滤器从我的列表中选择一个随机项目,但随后我想在多个位置使用该随机选择的项目。 如何将过滤器的结果设置为可以在多个位置使用的变量。 如果我多次调用“随机”过滤器,它们几乎不可
我想知道在文件中添加包含或宏时是否有任何方法可以保持 jinja 的缩进。我想使用 jinja 生成代码文件。一个例子是 文件:class.html class MyClass: def s
我已经能够使用 BaseOperator 中的函数 render_template 成功渲染 Jinja 模板。 我的问题是,有谁知道在Rendered或Rendered Template选项卡下将渲
我从以下 ninja2 模板开始,但如果第二列的值 > 指定的宽度,它只会打印整个字符串,其余列右移。 {{ "%-7s"|format(tbl[3]) }} {{ "%-12s"|form
我一直在尝试使用 Flask-Cache 的 memoize 功能来仅返回 statusTS() 的缓存结果,除非在另一个请求中满足特定条件,然后删除缓存。 但它并没有被删除,并且 Jinja 模板仍
在 jinja 文件中找到其中一些: {%- else -%} 还有 {% if freeswitch_dispatcher -%} 看到破折号了吗?知道它的用途吗? 最佳答案 结果是 + 和
我从以下 ninja2 模板开始,但如果第二列的值 > 指定的宽度,它只会打印整个字符串,其余列右移。 {{ "%-7s"|format(tbl[3]) }} {{ "%-12s"|form
好的,所以我有了第一个 HTML 文件(header.html): M4A {% load staticfiles %} {% block content %}
我有一个看起来像这样的Python字典 [{"hat": 91, "d1b": 2, "d1c": 1, "d1d": 5, "d1e": 7, "d1f": 77, "d1e": 999} {"ha
我正在使用 SaltStack 来管理 BIND9 区域文件。以前我使用过这样的支柱数据: zones: example.com: a: www1: 1.2.3.4
我是一名优秀的程序员,十分优秀!