How to filter Ansible 'find' output

Asked by At

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.

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 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
    paths: /path/to/app
    file_type: link
  register: find_result

- name: Print find output
    var: find_result.results

Actual Result:

ok: [] => {
    "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: [] => {
    "find_result.files": [
            "path": "/path/to/app/softlink.1",

1 Answers

larsks On Best Solutions

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
    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": [

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

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