如何从Ansibles查找模块获取文件路径

时间:2019-05-23 13:10:56

标签: ansible find devops

我正在将Ansible用于某些IAC(以下为代码)任务。 我有一本剧本,在其中我递归地使用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
            },

从输出列表中,我实际上只想访问“模式”和“路径”对象,该怎么做?任何想法?

2 个答案:

答案 0 :(得分:0)

好的。您可以遍历匹配文件列表,然后参考感兴趣的任何键:

- 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"
}

答案 1 :(得分:0)

尝试json_query

- set_fact:
    read_app_logs: "{{ rapplogs.files|json_query('[].{path: path, mode: mode}') }}"

(未经测试)