我正在尝试为其主目录中的用户列表找到所有'.vim *'文件,稍后将其删除。
我的所作所为:
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}}"
但是调试模块失败了:
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
虽然如果我删除“。path”(路径 - 这是我以后需要删除带有文件模块的文件)并执行相同操作但是:
...
- debug:
msg: "{{ item.files }}"
with_items:
- "{{ user_vim_config_files.results}}"
然后我明白了:
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
}
]
}
据我了解,我可以访问 {{item.files}} 并查看“路径”:“/ home / user#/。vim”列表中的所有用户,但无法访问 item.files.path 属性。为什么?您如何建议提取此信息?或者也许还有更好的方法吗?
更新
尝试:
- name: removing old vim configuration
file: path="{{ item.files | map(attribute='path') | list }}" state=absent
become: yes
with_items: "{{ user_vim_confg_files.results }}"
结果:文件仍然存在且未被删除。 绿色输出(而不是预期的黄色变化)
我的猜测是路径现在是一个路径列表......那么在这种情况下我如何组织某种嵌套循环?或者我错了吗?
更新
试图按照建议展平:
- name: removing old vim configuration
file:
path: "{{ user_vim_config_files.results | sum(attribute='files', start=[]) | map(attribute='path') | list }}"
state: absent
become: yes
结果是绿色而不是预期的黄色 - 没有匹配的文件被删除。这可能有什么问题?
答案 0 :(得分:0)
[]无法访问
#include <stdio.h> // printf(), scanf(), fprintf(), stderr //#include <math.h> <<-- contents not used, so do not include #include <stdlib.h> // exit(), EXIT_FAILURE int main( void ) { int tam; printf( "Ingrese el tamano del array que va a crear:\n" ); if( 1 != scanf( "%d", &tam ) ) { fprintf( stderr, "scanf for 'tam' failed\n" ); exit( EXIT_FAILURE ); } // implied else, scanf successful int arr[tam]; printf( "Ingrese los elementos del array:\n" ); for( int i=0; i < tam; i++ ) { printf( "Elemento arr%d", i+1 ); if( 1 != scanf( "%d", &arr[i] ) ) { fprintf( stderr, "scanf for array entry %d failed\n", i+1 ); exit( EXIT_FAILURE ); } // implied else, scanf successful. } for( int j=0; j < tam; j++ ) { for( int comp=0; comp < (tam-1); comp++ ) // note correction and data scope limiting { if( arr[comp] < arr[comp+1] ) { int var = arr[comp]; // note data scope limiting arr[comp] = arr[comp + 1]; arr[comp + 1] = var; } } } printf( "La matriz en orden descendente es:\n" ); for( int i=0; i < tam; i++ ) { printf( "%d ", arr[i] ); } }
属性。为什么呢?
因为它不存在。 item.files.path
是一个列表,因此files
,item.files[0].path
等确实存在,但item.files[1].path
中没有path
个键。
您如何建议提取此信息?
例如:
item.files
或许有更好的方法可以做到这一点?
您可以将整个结果字典展平,以便一举获得路径列表:
{{ item.files | map(attribute='path') | list }}
答案 1 :(得分:0)
- 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 }}"
答案 2 :(得分:0)
Json_query功能强大且易于使用:
loop: "{{ user_vim_config_files.results | json_query('[*].files[*].path') }}"
答案 3 :(得分:-1)
在此处发布已应用的解决方案:
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 }}"
我知道这是一种丑陋的解决方案,但至少它有效......哈哈
<强>解释强>
"{{ user_vim_config_files.results | sum(attribute='files', start=[]) | map(attribute='path') | list }}"
返回路径的列表,摘自user_vim_config_files.results
文件模块遍历每个路径并删除所有文件。