Fabrice Chapuis
Fabrice Chapuis

Reputation: 498

Using ansible find module to get hidden folders

How to use the ansible find module to get folder that start with dot (eg .git). The following example does not work.

 - name: Locating all files in files dir    
   find:
     path: "/tmp/test"
     patterns: ".git"
#     recurse: yes
#     use_regex: true    register: cleanup

Upvotes: 0

Views: 1475

Answers (2)

Vladimir Botka
Vladimir Botka

Reputation: 68104

Set parameter hidden (default=no). For example, given one hidden directory .git, two other directories, and two files in /tmp/test

shell> ls -1a /tmp/test/
.
..
dir1
dir2
file1
file2
.git

the task below finds all items in the directory except the hidden one

    - find:
        path: /tmp/test
        file_type: any
      register: result
    - debug:
        var: result.files|map(attribute='path')

gives

  result.files|map(attribute='path'):
  - /tmp/test/file2
  - /tmp/test/file1
  - /tmp/test/dir2
  - /tmp/test/dir1

When you set hidden=yes

    - find:
        path: /tmp/test
        file_type: any
        hidden: yes
      register: result
    - debug:
        var: result.files|map(attribute='path')

the hidden items will be included in the list

  result.files|map(attribute='path'):
  - /tmp/test/file2
  - /tmp/test/file1
  - /tmp/test/dir2
  - /tmp/test/dir1
  - /tmp/test/.git

Upvotes: 1

larsks
larsks

Reputation: 311880

If you look at the documentation for the find module, you'll note that by default it only finds files. Because .git is a directory it will never match. You can modify this behavior using the file_type option.

It also explicitly excludes "hidden" files; you can control this behavior with the hidden option.

That gives you:

- name: Locating all files in files dir    
  find:
    path: "/tmp/test"
    patterns: ".git"
    hidden: true
    file_type: any

This will find files or directories named .git. If you're looking for anything that starts with ., you would write:

- name: Locating all files in files dir    
  find:
    path: "/tmp/test"
    patterns: ".*"
    hidden: true
    file_type: any

If I have a file named /tmp/test/.foo and a directory named /tmp/test/.bar, running this playbook:

- hosts: localhost
  gather_facts: false
  tasks:
    - name: Locating all files in files dir
      register: res
      find:
        path: "/tmp/test"
        patterns: ".*"
        hidden: true
        file_type: any

    - debug:
        var: res

Results in:

TASK [Locating all files in files dir] *****************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "res": {
        "changed": false,
        "examined": 2,
        "failed": false,
        "files": [
            {
                "atime": 1628171545.1114912,
                "ctime": 1628171545.1114912,
                "dev": 44,
                "gid": 1000,
                "gr_name": "lars",
                "inode": 9302,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "mode": "0664",
                "mtime": 1628171545.1114912,
                "nlink": 1,
                "path": "/tmp/test/.foo",
                "pw_name": "lars",
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 0,
                "uid": 1000,
                "wgrp": true,
                "woth": false,
                "wusr": true,
                "xgrp": false,
                "xoth": false,
                "xusr": false
            },
            {
                "atime": 1628171547.4475772,
                "ctime": 1628171547.4475772,
                "dev": 44,
                "gid": 1000,
                "gr_name": "lars",
                "inode": 9303,
                "isblk": false,
                "ischr": false,
                "isdir": true,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": false,
                "issock": false,
                "isuid": false,
                "mode": "0775",
                "mtime": 1628171547.4475772,
                "nlink": 2,
                "path": "/tmp/test/.git",
                "pw_name": "lars",
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 40,
                "uid": 1000,
                "wgrp": true,
                "woth": false,
                "wusr": true,
                "xgrp": true,
                "xoth": true,
                "xusr": true
            }
        ],
        "matched": 2,
        "msg": ""
    }
}

Upvotes: 2

Related Questions