ansible - 如何使用 with_items/loops 从 ansible 输出中提取值
问题描述
尝试使用以下代码行
- name: reconstruct the path to test
set_fact:
folder_check: "{{ item | join('\\\\') }}"
# | regex_replace('\\\\', ',')
with_items:
- "{{ path_list }}"
ignore_errors: yes
register: output
当我执行这部分代码时,我得到了 SET_FACT 任务的输出,而不是 Registered output var 的输出。
ok: [shancamw2k12.iafdomain.local] => (item=[u'C:', u'temp', u'temp1']) => {"ansible_facts": {"folder_check": "C:\\\\temp\\\\temp1"}, "changed": false, "item": ["C:", "temp", "temp1"]}
ok: [shancamw2k12.iafdomain.local] => (item=[u'C:', u'temp']) => {"ansible_facts": {"folder_check": "C:\\\\temp"}, "changed": false, "item": ["C:", "temp"]}
ok: [shancamw2k12.iafdomain.local] => (item=[u'C:']) => {"ansible_facts": {"folder_check": "C:"}, "changed": false, "item": ["C:"]}.
从这行我应该只提取"folder_check": "C:\\\\temp\\\\temp1" ; "C:\\\\temp" and C:\\
.
解决方案
因此,我根据您问题的内容和格式假设了一些事情。
- 提供给 to 变量构建任务的列表将按照您想要的顺序排列,不需要排序。
- 您将始终使用它们
- 您这样做是因为输入 set_fact 任务的项目(及其内容)的数量不是静态的
- C 后面的斜线数量不同,具体取决于它是单独存在还是伴随路径的其他部分。
此外,由于您正在设置一个事实,因此您不需要将其保存到寄存器中,除非您需要关于变量构建的其他元数据,而不是其输出。
- name: Build Variable
hosts:
- all
gather_facts: no
vars:
- path_list:
- "C:"
- "temp"
- "temp1"
tasks:
- set_fact:
temp_var: "{{ temp_var|default([]) + [ item + ('//' if my_idx == 0 else '' if my_idx == path_list|length - 1 else '////') ] }}"
loop: "{{ path_list }}"
loop_control:
index_var: my_idx
- set_fact:
folder_check: "{{ folder_check|default([]) + [ item if my_idx|int-1 < 0 else folder_check[my_idx|int-1] + '//' + item if my_idx|int-1 == 0 else folder_check[my_idx|int-1] + item ] }}"
loop: "{{ temp_var }}"
loop_control:
index_var: my_idx
- debug: var=folder_check
这会给你这样的输出:
PLAY [Build Variable] ********************************************************************************************************
TASK [set_fact] **************************************************************************************************************
ok: [host1] => (item=C:) => {"ansible_facts": {"temp_var": ["C://"]}, "changed": false, "item": "C:", "my_idx": 0}
ok: [host1] => (item=temp) => {"ansible_facts": {"temp_var": ["C://", "temp////"]}, "changed": false, "item": "temp", "my_idx": 1}
ok: [host1] => (item=temp1) => {"ansible_facts": {"temp_var": ["C://", "temp////", "temp1"]}, "changed": false, "item": "temp1", "my_idx": 2}
TASK [set_fact] **************************************************************************************************************
ok: [host1] => (item=C://) => {"ansible_facts": {"folder_check": ["C://"]}, "changed": false, "item": "C://", "my_idx": 0}
ok: [host1] => (item=temp////) => {"ansible_facts": {"folder_check": ["C://", "C:////temp////"]}, "changed": false, "item": "temp////", "my_idx": 1}
ok: [host1] => (item=temp1) => {"ansible_facts": {"folder_check": ["C://", "C:////temp////", "C:////temp////temp1"]}, "changed": false, "item": "temp1", "my_idx": 2}
TASK [debug] *****************************************************************************************************************
ok: [host1] => {
"folder_check": [
"C://",
"C:////temp////",
"C:////temp////temp1"
]
}
推荐阅读
- swift - 如何使用`variable:[String:Codable]`使结构可编码?
- java - java lock如何知道应该锁定哪些资源
- javascript - 如何在 Vue 安装的应用程序中仅渲染一次原生 WebComponent
- r - lavaan 中具有多个中介和多个自变量的 SEM 模型
- tensorflow - 如何在回归问题中从 CNN 输出 float32 图像?
- mysql - 在从 SQL 查询导入之前检查 Google 工作表中是否存在记录
- python - PYPDF2 - 提取所有页面并转换为 CSV
- javascript - 如何将 setTimeout 与 puppeteer page.evaluate 一起使用
- mysql - Nodejs - MySQL - 高流量解决方案
- python-3.x - minizinc 已安装但 python 认为没有