Ansible 查找模块无法打印找到匹配模式的文件
Ansible find module fails to print files found matching pattern
我的目标服务器主目录有如下文件:
$ cd ~
$ ls -la
total 84
drwxr--r-- 7 wluser wluser 4096 Jun 11 02:56 .
drwxr-xr-x. 32 root root 4096 Jun 10 03:20 ..
drwx------ 4 wluser wluser 25 May 26 12:17 .ansible
-rw------- 1 wluser wluser 34681 Jun 11 03:29 .bash_history
-rw-r--r-- 1 wluser wluser 18 Aug 21 2019 .bash_logout
-rw-r--r-- 1 wluser wluser 193 Aug 21 2019 .bash_profile
-rw-r--r-- 1 wluser wluser 231 Aug 21 2019 .bashrc
drwxrwxr-x 4 wluser wluser 27 Feb 17 04:48 .cache
drwxrwxr-x 4 wluser wluser 28 Feb 17 08:17 .config
-rw-r--r-- 1 wluser wluser 172 Feb 17 2020 .kshrc
-rw------- 1 wluser wluser 40 May 28 11:59 .lesshst
drwxr----- 3 wluser wluser 18 Feb 17 08:17 .pki
-rw------- 1 wluser wluser 1204 Jul 5 10:05 .sh_history
drwxr--r-- 2 wluser wluser 76 May 28 10:59 .ssh
-rw------- 1 wluser wluser 13314 Jun 11 02:56 .viminfo
我使用下面的 ansible play 来查找所有文件匹配模式:
121 - find:
122 paths: "/home/wluser/"
123 recurse: no
124 file_type: any
125 patterns:
126 - "*.sh*"
127 - "*.bash*"
128 - "*.ksh*"
129 - "*.profile*"
130 - ".ssh"
131 register: to_copy
132
133
134 - debug:
135 msg: "FOUNDDDXX {{ to_copy}}"
当我希望它工作并显示主目录中的所有文件时,输出显示它没有匹配任何内容。
TASK [find] ***************************************************************************************************
task path: /web/playbooks/ansibleuser/va_action.yml:121
ok: [remotehost1] => {
"changed": false,
"examined": 72,
"files": [],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"contains": null,
"file_type": "any",
"follow": false,
"get_checksum": false,
"hidden": false,
"paths": [
"/home/wluser/"
],
"patterns": [
"*.sh*",
"*.bash*",
"*.ksh*",
"*.profile*",
".ssh"
],
"recurse": false,
"size": null,
"use_regex": false
}
},
"matched": 0,
"msg": ""
}
TASK [debug] **************************************************************************************************
task path: /web/playbooks/ansibleuser/va_action.yml:134
ok: [remotehost1] => {
"msg": "FOUNDDDXX {u'files': [], u'changed': False, 'failed': False, u'examined': 72, u'msg': u'', u'matched': 0}"
}
模式匹配可能不正确,但 .ssh
文件夹应该肯定匹配,但是也找不到。
$ ansible --version
ansible 2.4.2.0
config file = /home/ansbladm/.ansible.cfg
python version = 2.7.5
你能建议我如何显示主目录下匹配模式的所有文件吗?
这对我有用
tasks:
- name: find files
find:
paths: /home/myuserId
patterns:
- "(.*bash.*)$"
- "(.*ssh.*)$"
use_regex: yes
hidden: yes
register: list_of_files
- name: print files
debug:
msg: "{{ list_of_files.files }}"
请注意 use_regex: true
和 hidden: true
。没有那个论点,它对我也不起作用。我看到您在代码中遗漏了该参数。试试看,让我知道你看到了什么
我的目标服务器主目录有如下文件:
$ cd ~
$ ls -la
total 84
drwxr--r-- 7 wluser wluser 4096 Jun 11 02:56 .
drwxr-xr-x. 32 root root 4096 Jun 10 03:20 ..
drwx------ 4 wluser wluser 25 May 26 12:17 .ansible
-rw------- 1 wluser wluser 34681 Jun 11 03:29 .bash_history
-rw-r--r-- 1 wluser wluser 18 Aug 21 2019 .bash_logout
-rw-r--r-- 1 wluser wluser 193 Aug 21 2019 .bash_profile
-rw-r--r-- 1 wluser wluser 231 Aug 21 2019 .bashrc
drwxrwxr-x 4 wluser wluser 27 Feb 17 04:48 .cache
drwxrwxr-x 4 wluser wluser 28 Feb 17 08:17 .config
-rw-r--r-- 1 wluser wluser 172 Feb 17 2020 .kshrc
-rw------- 1 wluser wluser 40 May 28 11:59 .lesshst
drwxr----- 3 wluser wluser 18 Feb 17 08:17 .pki
-rw------- 1 wluser wluser 1204 Jul 5 10:05 .sh_history
drwxr--r-- 2 wluser wluser 76 May 28 10:59 .ssh
-rw------- 1 wluser wluser 13314 Jun 11 02:56 .viminfo
我使用下面的 ansible play 来查找所有文件匹配模式:
121 - find:
122 paths: "/home/wluser/"
123 recurse: no
124 file_type: any
125 patterns:
126 - "*.sh*"
127 - "*.bash*"
128 - "*.ksh*"
129 - "*.profile*"
130 - ".ssh"
131 register: to_copy
132
133
134 - debug:
135 msg: "FOUNDDDXX {{ to_copy}}"
当我希望它工作并显示主目录中的所有文件时,输出显示它没有匹配任何内容。
TASK [find] ***************************************************************************************************
task path: /web/playbooks/ansibleuser/va_action.yml:121
ok: [remotehost1] => {
"changed": false,
"examined": 72,
"files": [],
"invocation": {
"module_args": {
"age": null,
"age_stamp": "mtime",
"contains": null,
"file_type": "any",
"follow": false,
"get_checksum": false,
"hidden": false,
"paths": [
"/home/wluser/"
],
"patterns": [
"*.sh*",
"*.bash*",
"*.ksh*",
"*.profile*",
".ssh"
],
"recurse": false,
"size": null,
"use_regex": false
}
},
"matched": 0,
"msg": ""
}
TASK [debug] **************************************************************************************************
task path: /web/playbooks/ansibleuser/va_action.yml:134
ok: [remotehost1] => {
"msg": "FOUNDDDXX {u'files': [], u'changed': False, 'failed': False, u'examined': 72, u'msg': u'', u'matched': 0}"
}
模式匹配可能不正确,但 .ssh
文件夹应该肯定匹配,但是也找不到。
$ ansible --version
ansible 2.4.2.0
config file = /home/ansbladm/.ansible.cfg
python version = 2.7.5
你能建议我如何显示主目录下匹配模式的所有文件吗?
这对我有用
tasks:
- name: find files
find:
paths: /home/myuserId
patterns:
- "(.*bash.*)$"
- "(.*ssh.*)$"
use_regex: yes
hidden: yes
register: list_of_files
- name: print files
debug:
msg: "{{ list_of_files.files }}"
请注意 use_regex: true
和 hidden: true
。没有那个论点,它对我也不起作用。我看到您在代码中遗漏了该参数。试试看,让我知道你看到了什么