我遇到了一个问题,从调用ansible find模块获得我想要的数据,而遍历字典。目标是获得一个可以清理的文件列表:
字典是这样的:
cleanUp:
data1:
paths: '/opt/some/path'
patterns:
- '^[0-9]{8}.*.csv$'
- '^.*[0-9]{8}.jar$'
age: '30d'
type: file
recurse: yes
regex: yes
data2:
paths: '/opt/other/path'
patterns: '^[0-9]{8}.*$'
age: '60d'
type: directory
recurse: no
regex: yes
我的剧本是这样访问字典的:
- name: find all files that are older than they should be
find:
paths: '{{item.value.paths}}'
patterns: '{{item.value.patterns}}'
age: '{{item.value.age}}'
file_type: '{{item.value.type}}'
recurse: '{{item.value.recurse}}'
use_regex: '{{item.value.regex}}'
register: oldFiles
loop: "{{ cleanUp|dict2items }}"
- name: add files to fileList
set_fact:
fileList: "{{ fileList + [item.path]}}"
loop: "{{ oldFiles.files }}"
- name: print Files to be deleted
debug:
var=fileList
上面的第二个和第三个任务失败了,但我认为它显示了我想做的事情。我尝试了许多不同的更改和搜索,但还是卡住了。
当我输入下面的调试时,它显示了我想要的所有数据,但是我还不能弄清楚如何使用它:
- name: print oldFiles
debug:
msg: "{{oldFiles}}"
输出如下:
ok: [uat-host-01] =>
msg:
changed: false
msg: All items completed
results:
- ansible_loop_var: item
changed: false
examined: 1526
failed: false
files:
- atime: 1602241325.6995838
ctime: 1602241322.1505592
dev: 51713
gid: 1001
gr_name: username
inode: 254462789
isblk: false
ischr: false
isdir: false
isfifo: false
isgid: false
islnk: false
isreg: true
issock: false
isuid: false
mode: '0644'
mtime: 1602241322.1505592
nlink: 1
path: /opt/some/path/some.csv
pw_name: user
rgrp: true
roth: true
rusr: true
size: 210721
uid: 1001
wgrp: false
woth: false
wusr: true
xgrp: false
xoth: false
xusr: false
- atime: 1602500497.3135107
ctime: 1602500493.8174858
dev: 51713
gid: 1001
gr_name: username
inode: 254462790
isblk: false
ischr: false
isdir: false
isfifo: false
isgid: false
islnk: false
isreg: true
issock: false
isuid: false
mode: '0644'
mtime: 1602500493.8174858
nlink: 1
path: /opt/some/path/someother.jar
pw_name: username
rgrp: true
roth: true
rusr: true
size: 2459535
uid: 1001
wgrp: false
woth: false
wusr: true
xgrp: false
xoth: false
xusr: false
我认为这里需要的是json_query过滤器,以便聚合循环迭代的所有结果。
首先,varoldFiles
将拥有子索引oldFiles['results']
下循环中每次迭代的结果列表,记录如下:https://docs.ansible.com/ansible/2.9/user_guide/playbooks_loops.html#registering-variables-with-a-loop
第二,使用Ansiblejson_query
过滤器(注意jmespath
python依赖):https://docs.ansible.com/ansible/2.9/user_guide/playbooks_filters.html#json-query-filter
根据你提供的信息,我在下面提供了一个例子。
---
- name: example remove old files
hosts: localhost
vars:
cleanUp:
data1:
paths: '/var/cache/dnf'
patterns:
- '.*.solvx$'
- '^.*.rpm$'
age: '30d'
type: file
recurse: yes
regex: yes
data2:
paths: '/var/cache/fwupd'
patterns: '^.*.xmlb$'
age: '10d'
type: file
recurse: no
regex: yes
tasks:
- name: find all files that are older than they should be
find:
paths: '{{item.value.paths}}'
patterns: '{{item.value.patterns}}'
age: '{{item.value.age}}'
file_type: '{{item.value.type}}'
recurse: '{{item.value.recurse}}'
use_regex: '{{item.value.regex}}'
register: oldFiles
loop: "{{ cleanUp|dict2items }}"
- name: set fact for file list
set_fact:
fileList: "{{ oldFiles|json_query('results[*].files') }}"
- debug:
var: fileList
谢谢@zigarn我接受了你的回复,并将其修改为以下内容,这正是我所需要的(…)以防其他人也对这个语法感到困惑)
- name: add files to fileList
no_log: true
set_fact:
fileList: "{{ oldFiles|json_query('results[*].files[*].path') }}"