Skip to content

Instantly share code, notes, and snippets.

@ssato
Last active December 10, 2019 15:05
Show Gist options
  • Save ssato/41b9aa67de737c493e4eeda76c9c686c to your computer and use it in GitHub Desktop.
Save ssato/41b9aa67de737c493e4eeda76c9c686c to your computer and use it in GitHub Desktop.
listing_txt_files_under_given_dir
ssato@x1-carbon-gen6% tree /tmp/4
/tmp/4
├── a
│   └── 0.txt
├── b
│   └── 1.txt
└── c
└── d
└── 3.txt
4 directories, 3 files
ssato@x1-carbon-gen6% cat /tmp/t.yml
- name: ansible hino kokyu - enbu playbook
hosts: localhost
gather_facts: no
connection: local
tasks:
- name: find files
find:
paths: /tmp/4
recurse: true
file_type: file
use_regex: true
patterns:
- '.*.txt$'
register: res
- debug:
msg: >-
res.files = {{ res.files }}
dirs_having_txt_files =
{{ res.files |
selectattr('mode', 'equalto', '0644') |
map(attribute='path') |
map('regex_replace', "(.*)(/.*.txt)", "\1") |
list |
unique |
to_nice_json }}
# vim:sw=2:ts=2:et:
ssato@x1-carbon-gen6% ansible-playbook /tmp/t.yml
PLAY [ansible hino kokyu - enbu playbook] *****************************************************************************************************************************
TASK [find files] *********************************************************************************************************************************************
ok: [localhost]
TASK [debug] **************************************************************************************************************************************************
ok: [localhost] => {
"msg": "res.files = [{'path': '/tmp/4/c/d/3.txt', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1000, 'gid': 1000, 'size': 0, 'inode': 53453683, 'dev': 36, 'nlink': 1, 'atime': 1575988052.1095393, 'mtime': 1575988052.1095393, 'ctime': 1575989570.930588, 'gr_name': 'ssato', 'pw_name': 'ssato', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False}, {'path': '/tmp/4/b/1.txt', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1000, 'gid': 1000, 'size': 0, 'inode': 53453682, 'dev': 36, 'nlink': 1, 'atime': 1575988052.1095393, 'mtime': 1575988052.1095393, 'ctime': 1575989570.930588, 'gr_name': 'ssato', 'pw_name': 'ssato', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False}, {'path': '/tmp/4/a/0.txt', 'mode': '0644', 'isdir': False, 'ischr': False, 'isblk': False, 'isreg': True, 'isfifo': False, 'islnk': False, 'issock': False, 'uid': 1000, 'gid': 1000, 'size': 0, 'inode': 53453681, 'dev': 36, 'nlink': 1, 'atime': 1575988052.1095393, 'mtime': 1575988052.1095393, 'ctime': 1575989570.930588, 'gr_name': 'ssato', 'pw_name': 'ssato', 'wusr': True, 'rusr': True, 'xusr': False, 'wgrp': False, 'rgrp': True, 'xgrp': False, 'woth': False, 'roth': True, 'xoth': False, 'isuid': False, 'isgid': False}]\ndirs_having_txt_files = [\n \"/tmp/4/c/d\",\n \"/tmp/4/b\",\n \"/tmp/4/a\"\n]"
}
PLAY RECAP ****************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ssato@x1-carbon-gen6%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment