如何过滤 Ansible 'find' 输出 [英] How to filter Ansible 'find' output

查看:22
本文介绍了如何过滤 Ansible 'find' 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在终端中查看来自某些远程服务器的符号链接列表,但是当我运行剧本时会打印很多信息.

I would like to view the list of symbolic links from some remote servers in the terminal, but a lot of information is being printed when I run the playbook.

这是在 Ubuntu 服务器上运行的 ansible 2.7.12.我正在使用查找"模块和 file_type: 链接来获取软链接详细信息.

This is ansible 2.7.12 running on an Ubuntu server. I am using 'find' module and file_type: link to get the softlink details.

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

Find is returning a lot of details with the return value key "files" but I just need the soft links and corresponding server name in the terminal.

---
# 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, 
            ...
            ...

希望在终端中获得一些过滤输出,例如:

Would like to get some filtered output in the terminal like:

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

推荐答案

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

There are a couple of ways of addressing this question. You could use the map filter to extract just the path attribute from your results:

- name: Print find output
  debug:
    var: results.files|map(attribute='path')|list

鉴于您问题中的示例数据,这将导致:

Given the sample data in your question, this would result in:

TASK [Print find output] *****************************************************************************************************************************************************
ok: [localhost] => {
    "results.files|map(attribute='path')|list": [
        "/path/to/app/softlink.1"
    ]
}

您还可以使用 json_query 过滤器完成类似的操作,该过滤器适用于 JMESPath 查询到您的数据:

You can also accomplish something similar using the json_query filter, which applies JMESPath queries to your data:

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

这篇关于如何过滤 Ansible 'find' 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆