json - Ansible 从文件中获取类似字典的值到剧本或循环
问题描述
我正在寻找一种在循环时从文本、yaml 或 json 文件中获取值的方法。
我的代码正在工作,因为我正在迭代一个哈希列表,loop
但问题是当我将有多个条目进行迭代时,这就是为什么我想将所有这些值放入一个文件中,然后在任务中调用它们/剧本而不是在剧本中写一堆线。
请建议或帮助通过..
下面是工作代码:
---
- hosts: localhost
gather_facts: no
vars:
config: "{{ playbook_dir }}/{{ config_file }}"
contents: "{{lookup('file', config)}}"
server_profile_template: 'Test_apc_SY 480 Gen10 2 NVMe Application Template 20190601 V1.0'
server_hardware: "SY 480 Gen9 1"
template_name: []
server_list: []
tasks:
- name: Create server profile
oneview_server_profile:
config: "{{ config }}"
data:
serverProfileTemplateName: "{{ server_profile_template }}"
serverHardwareName: "{{ item.Bay }}"
name: "{{ item.Profilename }}"
params:
force: True
loop:
- { Bay: "ENT0005, bay 11", Profilename: "test_profile01" }
- { Bay: "ENT0005, bay 12", Profilename: "test_profile02" }
delegate_to: localhost
register: result
- debug: msg= "{{ result }}"
- debug: msg= "{{ result.msg }}"
...
目的是什么:
$ cat bayfile.yml
---
- 'Bay: "ENT0005, bay 11", Profilename: "test_profile01"'
- 'Bay: "ENT0005, bay 12", Profilename: "test_profile02"'
...
我尝试了什么:
loop: "{{ lookup('file', '{{ bayfile.yml }}') }}"
但以上不起作用。
解决方案
您正朝着正确的方向前进,但有几个问题:
数据文件中的 YAML 语法错误。
在您的示例中,您显示:
- 'Bay: "ENT0005, bay 11", Profilename: "test_profile01"'
- 'Bay: "ENT0005, bay 12", Profilename: "test_profile02"'
这是一个字符串列表(因为每个列表项都用单引号括起来)。如果您想重现您在剧本中显示的数据,您需要一个字典列表。你可能想要这个:
- Bay: "ENT0005, bay 11"
Profilename: "test_profile01"
- Bay: "ENT0005, bay 12"
Profilename: "test_profile02"
以下是相同的,只是使用了稍微不同的语法:
- {Bay: "ENT0005, bay 11", Profilename: "test_profile01"}
- {Bay: "ENT0005, bay 12", Profilename: "test_profile02"}
嵌套的 jinja2 模板标记
你永远不会嵌套{{...}}
标记;你只需要最外面的集合......里面的一切都已经在 Jinja 模板上下文中了。而不是:
loop: "{{ lookup('file', '{{ bayfile.yml }}') }}"
你会写:
loop: "{{ lookup('file', 'bayfile.yml') }}"
将数据转换为 YAML
最后,当您使用这样的file
查找时,结果只是一个字符串(文件的内容)。您想将其反序列化为 Ansible 数据结构,因此您需要from_yaml
过滤器:
loop: "{{ lookup('file', 'bayfile.yml') | from_yaml }}"
把所有这些放在一起,我们得到这样的东西:
- hosts: localhost
gather_facts: false
tasks:
- name: Create server profile
debug:
msg:
- "{{ item.Bay }}"
- "{{ item.Profilename }}"
loop: "{{ lookup('file', 'bayfile.yml') | from_yaml }}"
使用 include_vars
请注意,您可以使用剧本中的任务,而不是使用file
查找和过滤器。您首先需要重新格式化您的数据文件,使其成为字典而不是列表,如下所示:from_yaml
include_vars
oneview_servers:
- Bay: "ENT0005, bay 11"
Profilename: "test_profile01"
- Bay: "ENT0005, bay 12"
Profilename: "test_profile02"
然后你可以这样写你的剧本:
- hosts: localhost
gather_facts: false
tasks:
- name: Read data
include_vars:
file: bayfile.yml
name: data
- name: Create server profile
debug:
msg:
- "{{ item.Bay }}"
- "{{ item.Profilename }}"
loop: "{{ data.oneview_servers }}"
推荐阅读
- javascript - Fabric JS 将 Textbox 对象文本限制为仅包含一行文本
- rust - 从未装箱的切片创建 NDArray 的无复制方式?
- asp.net - Asp.Net Identity TwoFactorRememberBrowser Cookie 覆盖第二个用户登录
- formal-verification - 如果属性不满足,UPPAAL 不会显示跟踪
- ios - 为什么 Diffable Datasources 以不同的方式对待类和结构类型?
- leaflet - 将长坐标和纬度坐标划分为子坐标(较小的正方形)?
- r - 合并数据框并按组划分行
- java - 理解Java中的equals方法的麻烦
- spring-batch - Spring Cloud Data Flow UI - 删除平台属性
- r - 使用 broom 和 dplyr 合并数据框中的所有观测值的估计模型后进行预测