Python 如何将文件路径放入ansible中的变量中?
我试图从一个文件到一个字典中的变量过滤路径 路径文件: 路径.ymlPython 如何将文件路径放入ansible中的变量中?,python,linux,dictionary,ansible,yaml,Python,Linux,Dictionary,Ansible,Yaml,我试图从一个文件到一个字典中的变量过滤路径 路径文件: 路径.yml --- files: - {path: /root/files_to_copy/file_1} files: - {path: /root/files_to_copy/file_2} - {path: /root/files_to_copy/file_3} - {path: /root/files_to_copy/file_4} files:
---
files:
- {path: /root/files_to_copy/file_1}
files:
- {path: /root/files_to_copy/file_2}
- {path: /root/files_to_copy/file_3}
- {path: /root/files_to_copy/file_4}
files:
- {path: /root/files_to_copy/file_5}
- {path: /root/files_to_copy/file_6 }
- {path: /root/files_to_copy/file_7 }
我的主要任务是什么。yml:
为什么我不能使用path.values().path??还是分开
- name: putting paths into a variable
include_vars:
file: paths.yml
name: paths
- debug:
msg: "{{ paths.values() }}"
result:
ok: [192.168.1.10] => {
"msg": [
[
{
"path": "/root/files_to_copy/file_2"
},
{
"path": "/root/files_to_copy/file_3"
},
{
"path": "/root/files_to_copy/file_4"
}
],
我希望输出是这样的:/root/files\u to\u copy/file\u 2,不带路径和大括号当您加载了名为
path
的path.yml
文件时,它将整个文件加载到变量path
中,值都是path.yml
文件中的键值对
要仅提取最终的路径
,有多个选项。以下是一个示例:
-调试:
msg:{paths.values()|展平|映射(attribute='path')|列表}
给予
另一种可能更简单的方法是使用过滤器
-调试:
msg:{{paths | json_查询('*[].path')|列表}”
Python在哪里显示了您尝试过的内容以及错误或不正确的结果?
ok: [localhost] =>
msg:
- /root/files_to_copy/file_1
- /root/files_to_copy/file_2
- /root/files_to_copy/file_3
- /root/files_to_copy/file_4
- /root/files_to_copy/file_5
- /root/files_to_copy/file_6
- /root/files_to_copy/file_7