ansible: register find module return values in a l

2020-06-30 03:49发布

问题:

I am trying to find all '.vim*' files for a list of users in their home directory and remove them later on.

What I do:

vars:
  vim_users:
    - user1
    - user2

tasks:
- name: finding old vim configs in ~/ for all users
  find:
    file_type: any
    paths: ~/
    patterns: '.vim*'
    hidden: yes
  register: user_vim_config_files
  become: yes
  become_user: "{{ item }}"
  with_items: "{{ vim_users }}"

- debug:
    msg: "{{ item.files.path }}"
  with_items:
    - "{{ user_vim_config_files.results}}"

But getting Debug module failed:

TASK [debug] *******************************************************************************************************************************
fatal: [rpi]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined.
The error was: 'list object' has no attribute 'path'\n\nThe error appears to have been in '/Users/drew/CloudStation/rpi-scripts/minibian_start/ansible/test.yml': line 51, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n"}
to retry, use: --limit @/Users/drew/CloudStation/rpi-scripts/minibian_start/ansible/test.retry

Although if I remove ".path" ( path - this is what I need later for removing files with file module) and do the same but with:

...
- debug:
    msg: "{{ item.files }}"
  with_items:
    - "{{ user_vim_config_files.results}}"

Then I get this:

TASK [debug] *******************************************************************************************************************************
ok: [rpi] => (item={u'files': [{u'uid': 1000, u'woth': False, u'mtime': 1506392506.3046005, u'inode': 9017, u'isgid': False, u'size': 951, u'roth': False, u'isuid': False, u'isreg': True, u'gid': 1000, u'ischr': False, u'wusr': True, u'xoth': False, u'rusr': True, u'nlink': 1, u'issock': False, u'rgrp': False, u'path': u'/home/drew/.viminfo', u'xusr': False, u'atime': 1506392506.3046005, u'isdir': False, u'ctime': 1506392506.3046005, u'wgrp': False, u'xgrp': False, u'dev': 45826, u'isblk': False, u'isfifo': False, u'mode': u'0600', u'islnk': False}, {u'uid': 0, u'woth': False, u'mtime': 1506872972.448914, u'inode': 118, u'isgid': False, u'size': 0, u'roth': True, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'rusr': True, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/home/drew/.vimrc', u'xusr': False, u'atime': 1506872972.448914, u'isdir': False, u'ctime': 1506872972.448914, u'wgrp': False, u'xgrp': False, u'dev': 45826, u'isblk': False, u'isfifo': False, u'mode': u'0644', u'islnk': False}, {u'uid': 0, u'woth': False, u'mtime': 1506872982.078843, u'inode': 140, u'isgid': False, u'size': 4096, u'roth': True, u'isuid': False, u'isreg': False, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': True, u'rusr': True, u'nlink': 2, u'issock': False, u'rgrp': True, u'path': u'/home/drew/.vim', u'xusr': True, u'atime': 1506872982.078843, u'isdir': True, u'ctime': 1506872982.078843, u'wgrp': False, u'xgrp': True, u'dev': 45826, u'isblk': False, u'isfifo': False, u'mode': u'0755', u'islnk': False}], '_ansible_parsed': True, u'changed': False, '_ansible_no_log': False, '_ansible_item_result': True, 'item': u'drew', u'examined': 9, u'msg': u'', u'invocation': {u'module_args': {u'paths': [u'~/'], u'file_type': u'any', u'age': None, u'contains': None, u'recurse': False, u'age_stamp': u'mtime', u'patterns': [u'.vim*'], u'get_checksum': False, u'use_regex': False, u'follow': False, u'hidden': True, u'size': None}}, u'matched': 3}) => {
"item": {
    "changed": false,
    "examined": 9,
    "files": [
        {
            "atime": 1506392506.3046005,
            "ctime": 1506392506.3046005,
            "dev": 45826,
            "gid": 1000,
            "inode": 9017,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0600",
            "mtime": 1506392506.3046005,
            "nlink": 1,
            "path": "/home/user1/.viminfo",
            "rgrp": false,
            "roth": false,
            "rusr": true,
            "size": 951,
            "uid": 1000,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1506872972.448914,
            "ctime": 1506872972.448914,
            "dev": 45826,
            "gid": 0,
            "inode": 118,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1506872972.448914,
            "nlink": 1,
            "path": "/home/user1/.vimrc",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1506872982.078843,
            "ctime": 1506872982.078843,
            "dev": 45826,
            "gid": 0,
            "inode": 140,
            "isblk": false,
            "ischr": false,
            "isdir": true,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": false,
            "issock": false,
            "isuid": false,
            "mode": "0755",
            "mtime": 1506872982.078843,
            "nlink": 2,
            "path": "/home/user1/.vim",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 4096,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": true,
            "xoth": true,
            "xusr": true
        }
    ],
    "invocation": {
        "module_args": {
            "age": null,
            "age_stamp": "mtime",
            "contains": null,
            "file_type": "any",
            "follow": false,
            "get_checksum": false,
            "hidden": true,
            "paths": [
                "~/"
            ],
            "patterns": [
                ".vim*"
            ],
            "recurse": false,
            "size": null,
            "use_regex": false
        }
    },
    "item": "user1",
    "matched": 3,
    "msg": ""
},
"msg": [
    {
        "atime": 1506392506.3046005,
        "ctime": 1506392506.3046005,
        "dev": 45826,
        "gid": 1000,
        "inode": 9017,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "mode": "0600",
        "mtime": 1506392506.3046005,
        "nlink": 1,
        "path": "/home/user1/.viminfo",
        "rgrp": false,
        "roth": false,
        "rusr": true,
        "size": 951,
        "uid": 1000,
        "wgrp": false,
        "woth": false,
        "wusr": true,
        "xgrp": false,
        "xoth": false,
        "xusr": false
    },
    {
        "atime": 1506872972.448914,
        "ctime": 1506872972.448914,
        "dev": 45826,
        "gid": 0,
        "inode": 118,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "mode": "0644",
        "mtime": 1506872972.448914,
        "nlink": 1,
        "path": "/home/user1/.vimrc",
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 0,
        "uid": 0,
        "wgrp": false,
        "woth": false,
        "wusr": true,
        "xgrp": false,
        "xoth": false,
        "xusr": false
    },
    {
        "atime": 1506872982.078843,
        "ctime": 1506872982.078843,
        "dev": 45826,
        "gid": 0,
        "inode": 140,
        "isblk": false,
        "ischr": false,
        "isdir": true,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": false,
        "issock": false,
        "isuid": false,
        "mode": "0755",
        "mtime": 1506872982.078843,
        "nlink": 2,
        "path": "/home/user1/.vim",
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 4096,
        "uid": 0,
        "wgrp": false,
        "woth": false,
        "wusr": true,
        "xgrp": true,
        "xoth": true,
        "xusr": true
    }
]
}

ok: [rpi] => (item={u'files': [{u'uid': 0, u'woth': False, u'mtime': 1506876109.5467849, u'inode': 20, u'isgid': False, u'size': 0, u'roth': True, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'rusr': True, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/home/pi/.vimrc', u'xusr': False, u'atime': 1506876109.5467849, u'isdir': False, u'ctime': 1506876109.5467849, u'wgrp': False, u'xgrp': False, u'dev': 45826, u'isblk': False, u'isfifo': False, u'mode': u'0644', u'islnk': False}, {u'uid': 0, u'woth': False, u'mtime': 1506876112.2067666, u'inode': 145, u'isgid': False, u'size': 0, u'roth': True, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'rusr': True, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/home/pi/.vimrca', u'xusr': False, u'atime': 1506876112.2067666, u'isdir': False, u'ctime': 1506876112.2067666, u'wgrp': False, u'xgrp': False, u'dev': 45826, u'isblk': False, u'isfifo': False, u'mode': u'0644', u'islnk': False}, {u'uid': 0, u'woth': False, u'mtime': 1506876114.9767478, u'inode': 146, u'isgid': False, u'size': 0, u'roth': True, u'isuid': False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': False, u'rusr': True, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': u'/home/pi/.vim', u'xusr': False, u'atime': 1506876114.9767478, u'isdir': False, u'ctime': 1506876114.9767478, u'wgrp': False, u'xgrp': False, u'dev': 45826, u'isblk': False, u'isfifo': False, u'mode': u'0644', u'islnk': False}], '_ansible_parsed': True, u'changed': False, '_ansible_no_log': False, '_ansible_item_result': True, 'item': u'pi', u'examined': 8, u'msg': u'', u'invocation': {u'module_args': {u'paths': [u'~/'], u'file_type': u'any', u'age': None, u'contains': None, u'recurse': False, u'age_stamp': u'mtime', u'patterns': [u'.vim*'], u'get_checksum': False, u'use_regex': False, u'follow': False, u'hidden': True, u'size': None}}, u'matched': 3}) => {
"item": {
    "changed": false,
    "examined": 8,
    "files": [
        {
            "atime": 1506876109.5467849,
            "ctime": 1506876109.5467849,
            "dev": 45826,
            "gid": 0,
            "inode": 20,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1506876109.5467849,
            "nlink": 1,
            "path": "/home/user2/.vimrc",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1506876112.2067666,
            "ctime": 1506876112.2067666,
            "dev": 45826,
            "gid": 0,
            "inode": 145,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1506876112.2067666,
            "nlink": 1,
            "path": "/home/user2/.vimrca",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        },
        {
            "atime": 1506876114.9767478,
            "ctime": 1506876114.9767478,
            "dev": 45826,
            "gid": 0,
            "inode": 146,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mode": "0644",
            "mtime": 1506876114.9767478,
            "nlink": 1,
            "path": "/home/user2/.vim",
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 0,
            "wgrp": false,
            "woth": false,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    ],
    "invocation": {
        "module_args": {
            "age": null,
            "age_stamp": "mtime",
            "contains": null,
            "file_type": "any",
            "follow": false,
            "get_checksum": false,
            "hidden": true,
            "paths": [
                "~/"
            ],
            "patterns": [
                ".vim*"
            ],
            "recurse": false,
            "size": null,
            "use_regex": false
        }
    },
    "item": "user2",
    "matched": 3,
    "msg": ""
},
"msg": [
    {
        "atime": 1506876109.5467849,
        "ctime": 1506876109.5467849,
        "dev": 45826,
        "gid": 0,
        "inode": 20,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "mode": "0644",
        "mtime": 1506876109.5467849,
        "nlink": 1,
        "path": "/home/user2/.vimrc",
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 0,
        "uid": 0,
        "wgrp": false,
        "woth": false,
        "wusr": true,
        "xgrp": false,
        "xoth": false,
        "xusr": false
    },
    {
        "atime": 1506876112.2067666,
        "ctime": 1506876112.2067666,
        "dev": 45826,
        "gid": 0,
        "inode": 145,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "mode": "0644",
        "mtime": 1506876112.2067666,
        "nlink": 1,
        "path": "/home/user2/.vimrca",
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 0,
        "uid": 0,
        "wgrp": false,
        "woth": false,
        "wusr": true,
        "xgrp": false,
        "xoth": false,
        "xusr": false
    },
    {
        "atime": 1506876114.9767478,
        "ctime": 1506876114.9767478,
        "dev": 45826,
        "gid": 0,
        "inode": 146,
        "isblk": false,
        "ischr": false,
        "isdir": false,
        "isfifo": false,
        "isgid": false,
        "islnk": false,
        "isreg": true,
        "issock": false,
        "isuid": false,
        "mode": "0644",
        "mtime": 1506876114.9767478,
        "nlink": 1,
        "path": "/home/user2/.vim",
        "rgrp": true,
        "roth": true,
        "rusr": true,
        "size": 0,
        "uid": 0,
        "wgrp": false,
        "woth": false,
        "wusr": true,
        "xgrp": false,
        "xoth": false,
            "xusr": false
        }
    ]
}

So as I understand I can access the {{ item.files }} and see the "path": "/home/user#/.vim" for all users in the list, but can not access item.files.path property. Why? How would you suggest to extract this information? Or maybe there is a better way to do it?

UPDATE

Tried:

- name: removing old vim configuration
  file: path="{{ item.files | map(attribute='path') | list }}" state=absent
  become: yes
  with_items: "{{ user_vim_confg_files.results }}"

Result: The files are still there and were not removed. Green output (instead of expected yellow changes)

My guess is that path now is a list of paths... so how would I organize some sort of nested loop in this case? Or am I wrong?

UPDATE

Tried to flatten as suggested:

- name: removing old vim configuration
  file:
    path: "{{ user_vim_config_files.results | sum(attribute='files', start=[]) | map(attribute='path') | list }}"
    state: absent
  become: yes

Result is green instead of expected yellow - none of matching files is removed. What can be wrong here?

回答1:

[ ] can not access item.files.path property. Why?

Because it does not exist. files is a list, so item.files[0].path, item.files[1].path etc. do exist, but there is no path key in item.files.

How would you suggest to extract this information?

For example:

{{ item.files | map(attribute='path') | list }}

Or maybe there is a better way to do it?

You can flatten the whole results-dictionary to get a list of paths in one fell swoop:

{{ user_vim_confg_files.results | sum(attribute='files', start=[]) | map(attribute='path') | list }}


回答2:

- name: Recursively find config.yml in /etc
  find:
    paths:  /etc
    patterns: 'config.yml'
    recurse: yes
  register: filelist


- name: use shell to print list of file paths
  shell: "echo {{ item }}"
  with_items: "{{ filelist.files|map(attribute='path')|list }}"



回答3:

Posting the applied solution here:

vars:
  vim_users:
    - user1
    - user2

tasks:
- name: finding old vim configs in ~/ for all users
  find:
    file_type: any
    paths: ~/
    patterns: '.vim*'
    hidden: yes
  register: user_vim_config_files
  become: yes
  become_user: "{{ item }}"
  with_items: "{{ vim_users }}"

  - name: removing old vim configuration
    file:
      path: "{{ item }}"
      state: absent
    become: yes
    with_items:
      - "{{ user_vim_config_files.results | sum(attribute='files', start=[]) | map(attribute='path') | list }}"

I know it is kind of ugly solution, but at least it works... lol

Explanation:

"{{ user_vim_config_files.results | sum(attribute='files', start=[]) | map(attribute='path') | list }}"

returns the list of paths, extracted from user_vim_config_files.results

file module iterates over each path and removes all files.



标签: ansible