在ansible中循环嵌套列表
我正在使用find模块获取不同装载点(/E,/F)和中名为“deployments”的所有目录 然后使用文件模块在所有找到的目录中设置组所有权。 现在ansible正在嵌套列表中提供find输出,而with_items无法循环所有装入点的文件列表。 如何循环任务中的所有嵌套列表在ansible中循环嵌套列表,ansible,Ansible,我正在使用find模块获取不同装载点(/E,/F)和中名为“deployments”的所有目录 然后使用文件模块在所有找到的目录中设置组所有权。 现在ansible正在嵌套列表中提供find输出,而with_items无法循环所有装入点的文件列表。 如何循环任务中的所有嵌套列表 results=[ { "_ansible_item_result": true, "_ansible_no_log": false
results=[
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"examined": 139898,
"files": [
{
"atime": 1526307047.608814,
"ctime": 1523368503.64159,
"dev": 64778,
"gid": 780200012,
"inode": 39583770,
"isblk": false,
"ischr": false,
"isdir": true,
"isfifo": false,
"isgid": true,
"islnk": false,
"isreg": false,
"issock": false,
"isuid": true,
"mode": "6775",
"mtime": 1523368503.64159,
"nlink": 2,
"path": "/F/Ford/AutoDeploy/PRD/local_1/deployments",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 4096,
"uid": 780200029,
"wgrp": true,
"woth": false,
"wusr": true,
"xgrp": true,
"xoth": true,
"xusr": true
}
],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"contains": null,
"file_type": "directory",
"follow": false,
"get_checksum": false,
"hidden": false,
"paths": [
"/F"
],
"patterns": [
"deployments"
],
"recurse": true,
"size": null,
"use_regex": false
}
},
"item": "/F",
"matched": 1,
"msg": ""
},
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"examined": 60251,
"files": [
{
"atime": 1526365588.0262258,
"ctime": 1521525712.8813984,
"dev": 64777,
"gid": 780200012,
"inode": 12058651,
"isblk": false,
"ischr": false,
"isdir": true,
"isfifo": false,
"isgid": true,
"islnk": false,
"isreg": false,
"issock": false,
"isuid": true,
"mode": "6775",
"mtime": 1521525712.8813984,
"nlink": 2,
"path": "/H/Hyundai/AutoDeploy/PRD/local_6/deployments",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 4096,
"uid": 780200029,
"wgrp": true,
"woth": false,
"wusr": true,
"xgrp": true,
"xoth": true,
"xusr": true
}
],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"contains": null,
"file_type": "directory",
"follow": false,
"get_checksum": false,
"hidden": false,
"paths": [
"/H"
],
"patterns": [
"deployments"
],
"recurse": true,
"size": null,
"use_regex": false
}
},
"item": "/H",
"matched": 1,
"msg": ""
}
]
Playbook:
---
- name: deployment and syntaxCheck dir group verfication
become: yes
hosts: P98
gather_facts: no
tasks:
- name: checking for deployments
find:
paths: "{{ item }}"
patterns: "deployments"
recurse: yes
file_type: directory
with_items: "{{ path }}"
register: find_result
- name: display the output of find
debug: var=find_result
- name: change the group ownership of deployments
file:
path: "{{ item.path }}"
group: sag
with_items:
- "{{ find_result.results | map(attribute='files') | list }}"
#with_items: "{{ find_result.results[0].files }}"
此过滤器序列将使您获得列表中的
路径
,以便逐个解析:
- name: print paths
debug:
msg: "{{ item }}"
with_items:
- "{{ ansible_variable | map(attribute='files') | sum(start=[]) | map(attribute='path') | list }}"
输出:
TASK [print paths] **************************************************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {
"msg": "/F/Ford/AutoDeploy/PRD/local_1/deployments"
}
ok: [localhost] => (item=None) => {
"msg": "/H/Hyundai/AutoDeploy/PRD/local_6/deployments"
}
感谢它现在的工作,您能告诉我map、sum和list在这里做什么吗?或者有什么好的在线资源可以让我学习。map从初始变量的结构中提取
文件
属性,sum用于将其转换为列表,然后我们选择路径
属性,最后转换为列表。关于过滤的两个重要链接是,以及。他们有很好的例子来尝试和熟悉他们。