ansible - 如何从 ansibles 查找模块中获取文件路径
问题描述
我将 Ansible 用于一些 IAC(infra as code)任务。我有一本剧本,我在其中递归地使用 find 模块来搜索可读文件。
这是一个例子:
- name: Application logs with read access
become: true
find:
paths: /
file_type: file
recurse: yes
patterns:
- '*.log'
- '*.config'
register: rapplogs
- set_fact: read_app_logs={{rapplogs.matched}}
- debug: var=read_app_logs
- set_fact: read_log_list={{rapplogs.files}}
- debug: var=read_log_list
run_once: True
failed_when: read_app_logs >= 1
ignore_errors: True
它的输出是这样的:
TASK [infra_pt : set_fact] ******************************************************************
ok: [192.168.47.135]
TASK [infra_pt : debug] *********************************************************************
ok: [192.168.47.135] => {
"read_app_logs": "72"
}
TASK [infra_pt : set_fact] ******************************************************************
ok: [192.168.47.135]
TASK [infra_pt : debug] *********************************************************************
fatal: [192.168.47.135]: FAILED! => {
"failed_when_result": true,
"read_log_list": {
"changed": false,
"examined": 210060,
"failed": false,
"files": [
{
"atime": 1558446815.3474104,
"ctime": 1558446815.3474104,
"dev": 64768,
"gid": 0,
"gr_name": "root",
"inode": 2065610,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mode": "0644",
"mtime": 1558446815.3474104,
"nlink": 1,
"path": "/test2.log",
"pw_name": "root",
"rgrp": true,
"roth": true,
"rusr": true,
"size": 0,
"uid": 0,
"wgrp": false,
"woth": false,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
},
从输出列表中,我实际上只想访问“模式”和“路径”对象,如何做到这一点?任何的想法?
解决方案
当然。您可以遍历匹配文件列表并参考感兴趣的任何键:
- debug:
msg: "mode of {{ item.path }} is {{ item.mode }}"
loop: "{{ read_log_list.files }}"
根据您的示例输出,会产生如下内容:
TASK [debug] **********************************************************************************
ok: [localhost] => (item={u'islnk': False, u'uid': 0, u'rgrp': True, u'xoth': False, u'rusr': True, u'woth': False, u'nlink': 1, u'issock': False, u'mtime': 1558446815.3474104, u'gr_name': u'root', u'path': u'/test2.log', u'xusr': False, u'atime': 1558446815.3474104, u'inode': 2065610, u'isgid': False, u'size': 0, u'isdir': False, u'wgrp': False, u'ctime': 1558446815.3474104, u'isblk': False, u'xgrp': False, u'isuid': False, u'dev': 64768, u'roth': True, u'isreg': True, u'isfifo': False, u'mode': u'0644', u'pw_name': u'root', u'gid': 0, u'ischr': False, u'wusr': True}) => {
"msg": "mode of /test2.log is 0644"
}
推荐阅读
- javascript - DigitalPersona USB 指纹扫描仪能否仅使用 JavaScript 直接在浏览器中使用?
- sql - 根据键交换字段值
- javascript - 我该如何做这个关于学生问候的 javascript 问题?
- javascript - 您可以根据用户属性定义 Casl 规则吗?
- python - 使用 python-gitlab API 上传二进制文件
- r - 按组用数字序列填充 NA
- javascript - 为什么 Math.random()(在 Chrome 中)分配需要垃圾收集器 (gc) 清理的内存?
- nginx - 覆盖位置块上的 nginx http 版本
- python - 无效操作:[
] 在 Lambda 函数 python - javascript - 如何使一个单词在三元运算符中显示为粗体?