ansible - Ansible 从外部文件读取变量
问题描述
我将所有 json 格式的变量存储在一个外部文件中,并尝试在剧本中读取这是一个示例文件
{out_file: exp_app_20.xml, control_file: export_control.xml }
{out_file: exp_app_21.xml, control_file: export_control.xml }
现在,当我尝试读取变量out_file和control_file时,我找不到合适的方式来读取它。我试过 with_items 和 with_lines 但没有运气
- name: searching for text file
gather_facts: false
vars:
host_tgt: TGT
hosts: "{{ host_tgt }}"
tasks:
- name: get the file contents
shell: cat /dir/export.prop
register: my_items
- debug:
var: my_items
- name: Export
shell: echo {{ item.out_file }} **---error**
with_items: my_items.stdout_lines
register: find_output
- debug:
var: find_output
任何建议表示赞赏
解决方案
假设您可以更改文件的格式....
{
"array": [
{ "out_file": "exp_app_20.xml", "control_file": "export_control.xml" },
{ "out_file": "exp_app_21.xml", "control_file": "export_control.xml" }
]
}
include_vars
然后在您的剧本中,使用...加载文件。
tasks:
- include_vars:
file: /home/jack/test.json
name: my_items
- debug:
var: my_items
- debug:
msg: "{{ item.out_file }}"
with_items: "{{ my_items.array }}"
这给出了这个输出:
TASK [debug] ************************************************************************************************************************
ok: [localhost] => {
"my_items": {
"array": [
{
"control_file": "export_control.xml",
"out_file": "exp_app_20.xml"
},
{
"control_file": "export_control.xml",
"out_file": "exp_app_21.xml"
}
]
}
}
TASK [debug] ************************************************************************************************************************
ok: [localhost] => (item=None) => {
"msg": "exp_app_20.xml"
}
ok: [localhost] => (item=None) => {
"msg": "exp_app_21.xml"
}
推荐阅读
- .net-core - 在 RabbitMQ 中重启后如何让我的 dotnet 核心应用程序连接到仲裁队列?
- sql - SQL“NOT LIKE”在一行中尽可能多地减少 NOT LIKE
- javascript - 数据结构转换
- css - 带角度的分隔线,在下面的部分中带有图像
- python - Dataframe:在一个dataframe中找到最大的连续等数组,并得到该组的第一个和最后一个索引
- c++ - 计算物理并仅使用 GPU 显示
- c# - Asp.Net Core MVC 计算器
- javascript - 如何在 CRA 中使用 Webpack 配置覆盖更改 CSS 包名称?
- java - 由于对象是子类,程序如何决定运行哪个方法
- html - How to add an animated scroll down button using Html/Css?