如何过滤Ansible';查找';输出

如何过滤Ansible';查找';输出,ansible,Ansible,我想查看终端中一些远程服务器的符号链接列表,但运行playbook时会打印很多信息 这是在Ubuntu服务器上运行的ansible 2.7.12。我正在使用“查找”模块和文件类型:link获取软链接详细信息 Find使用返回值键“files”返回了很多详细信息,但我只需要终端中的软链接和相应的服务器名称 --- # tasks file for application - name: Get the current applications running find: paths:

我想查看终端中一些远程服务器的符号链接列表,但运行playbook时会打印很多信息

这是在Ubuntu服务器上运行的ansible 2.7.12。我正在使用“查找”模块和文件类型:link获取软链接详细信息

Find使用返回值键“files”返回了很多详细信息,但我只需要终端中的软链接和相应的服务器名称

---
# tasks file for application
- name: Get the current applications running
  find:
    paths: /path/to/app
    file_type: link
  register: find_result

- name: Print find output
  debug: 
    var: find_result.results
实际结果:

ok: [client3.example.com] => {
    "find_result.files": [
        {
            "atime": 1559027986.555, 
            "ctime": 1559027984.828, 
            "dev": 64768, 
            "gid": 0, 
            "gr_name": "root", 
            "inode": 4284972, 
            "isblk": false, 
            "ischr": false, 
            "isdir": false, 
            "isfifo": false, 
            "isgid": false, 
            "islnk": true, 
            "isreg": false, 
            "issock": false, 
            "isuid": false, 
            "mode": "0777", 
            "mtime": 1559027984.828, 
            "nlink": 1, 
            "path": "/path/to/app/softlink.1", 
            "pw_name": "root", 
            "rgrp": true, 
            ...
            ...
希望在终端中获得一些过滤输出,如:

ok: [client3.example.com] => {
    "find_result.files": [
        {
            "path": "/path/to/app/softlink.1",
},

有几种方法可以解决这个问题。您可以使用
map
过滤器仅从结果中提取
path
属性:

- name: Print find output
  debug:
    var: results.files|map(attribute='path')|list
鉴于您问题中的样本数据,这将导致:

TASK [Print find output] *****************************************************************************************************************************************************
ok: [localhost] => {
    "results.files|map(attribute='path')|list": [
        "/path/to/app/softlink.1"
    ]
}
您还可以使用
json\u query
过滤器完成类似的操作,该过滤器将查询应用于您的数据:

- name: Print find output
  debug:
    var: results.files|json_query('[*].path')

有几种方法可以解决这个问题。您可以使用
map
过滤器仅从结果中提取
path
属性:

- name: Print find output
  debug:
    var: results.files|map(attribute='path')|list
鉴于您问题中的样本数据,这将导致:

TASK [Print find output] *****************************************************************************************************************************************************
ok: [localhost] => {
    "results.files|map(attribute='path')|list": [
        "/path/to/app/softlink.1"
    ]
}
您还可以使用
json\u query
过滤器完成类似的操作,该过滤器将查询应用于您的数据:

- name: Print find output
  debug:
    var: results.files|json_query('[*].path')

正是我想要的。。。非常感谢拉尔克斯!!正是我想要的。。。非常感谢拉尔克斯!!