Ansible – How to Parse and Combine Output from Win_Find

ansiblewindows

Could someone please throw some light on how the below could be achieved with ansible:

I am currently searching for a file recursively using the win_find module (it's a windows box):

  - win_find:
      paths:
        - C:\Apps
      patterns: [ 'specific.file' ]
      recurse: yes
    register: apps_found

This produces the following list:

"apps_found": {
    "changed": false,
    "examined": 785,
    "failed": false,
    "files": [
        {
            "attributes": "Archive",
            "checksum": "67a43ebf47567ba43a29573a28479180392168d5",
            "creationtime": 1560515765.3164558,
            "extension": ".file",
            "filename": "specific.file",
            "isarchive": true,
            "isdir": false,
            "ishidden": false,
            "islnk": false,
            "isreadonly": false,
            "isshared": false,
            "lastaccesstime": 1560515765.2705016,
            "lastwritetime": 1560515765.2944584,
            "owner": "BUILTIN\\Administrators",
            "path": "C:\\Apps\\App1\\specific.file",
            "size": 247
        },
        {
            "attributes": "Archive",
            "checksum": "64dea9b49819fa4eee34dce52d2dc589a6f9667b",
            "creationtime": 1560769272.8943222,
            "extension": ".file",
            "filename": "specific.file",
            "isarchive": true,
            "isdir": false,
            "ishidden": false,
            "islnk": false,
            "isreadonly": false,
            "isshared": false,
            "lastaccesstime": 1560769272.847435,
            "lastwritetime": 1560769272.8788242,
            "owner": "BUILTIN\\Administrators",
            "path": "C:\\Apps\\App2\\specific.file",
            "size": 246
        }
    ],
    "matched": 2
}

What I want to do is:

For each found file, the 'path' key/value pair, I need to parse the subfolder name (App1, App2 in this case) and display it along with the contents of the specific.file.

So that at the end my playbook will produce a message such as:

"found application app1 with contents: "
"print the contents of the file."

How to do this easily in Ansible?
Thanks a lot upfront.

Best Answer

I don't have a windows machine to test so I briefly validated my below solution against your current data structure and some tests I made on my linux localhost.

To achieve your requirement, I first used the json_query filter to extract the paths from your result. This is used as a loop input to the slurp module to retreive the base64 representation of each file in a registered var.

Finally, I used a combination of win_dirname and win_basename filters to extract your application name in the final debug loop.

Here is the final example playbook (not fully tested as advised above):

---
- name: Show found app files
  hosts: whatever_group

  tasks:

    - name: Look for files on host
      win_find:
        paths:
          - C:\Apps
        patterns: [ 'specific.file' ]
        recurse: yes
      register: apps_found

    - name: Get the content of found files from host
      slurp:
        src: "{{ item }}"
      loop: "{{ apps_found | json_query('files[].path') }}"
      register: slurped_files

    - name: Display result
      debug:
        msg: "Found application {{ item.item | win_dirname | win_basename }} with contents: {{ item.content | b64decode }}"
      loop: "{{ slurped_files.results }}"