Ansible:查找文件并复制它们
Ansible: find files and copy them
我正在使用 Ansible 2.3 查找具有特定模式的文件并根据它们的路径复制它们。
所以我的搜索任务是这样的:
- name: find onDemand scripts of APIs
find:
paths: "{{WORKSPACE}}/dollaru-scripts/Docker/scripts_on_demand/"
patterns:
- 'run_api*.ksh'
- '*_all_containers.ksh'
- '*_docker_engine.ksh'
register: scripts_on_demand_api
when:
- ansible_host not in groups['remoteHosts']
然后,为了显示结果,我使用了一个 debug
任务并获得了这样的结果,在 Jenkins 下 运行 之后:
ok: [localhost] => {
17:51:36 "changed": false,
17:51:36 "msg": {
17:51:36 "changed": false,
17:51:36 "examined": 13,
17:51:36 "files": [
17:51:36 {
17:51:36 "atime": 1518791011.147691,
17:51:36 "ctime": 1518791009.4396753,
17:51:36 "dev": 64782,
17:51:36 "gid": 15001,
17:51:36 "inode": 301837,
17:51:36 "isblk": false,
17:51:36 "ischr": false,
17:51:36 "isdir": false,
17:51:36 "isfifo": false,
17:51:36 "isgid": false,
17:51:36 "islnk": false,
17:51:36 "isreg": true,
17:51:36 "issock": false,
17:51:36 "isuid": false,
17:51:36 "mode": "0644",
17:51:36 "mtime": 1518791009.4396753,
17:51:36 "nlink": 1,
17:51:36 "path": "/opt/jenkins/workspace/ANSIBLE_DEPLOY_HP-ALL/dollaru-scripts/Docker/scripts_on_demand/run_api_backend.ksh",
17:51:36 "rgrp": true,
17:51:36 "roth": true,
17:51:36 "rusr": true,
17:51:36 "size": 470,
17:51:36 "uid": 30000,
17:51:36 "wgrp": false,
17:51:36 "woth": false,
17:51:36 "wusr": true,
17:51:36 "xgrp": false,
17:51:36 "xoth": false,
17:51:36 "xusr": false
17:51:36 },
{
17:51:36 "atime": 1518791011.147691,
17:51:36 "ctime": 1518791009.4396753,
17:51:36 "dev": 64782,
17:51:36 "gid": 15001,
17:51:36 "inode": 301853,
17:51:36 "isblk": false,
17:51:36 "ischr": false,
17:51:36 "isdir": false,
17:51:36 "isfifo": false,
17:51:36 "isgid": false,
17:51:36 "islnk": false,
17:51:36 "isreg": true,
17:51:36 "issock": false,
17:51:36 "isuid": false,
17:51:36 "mode": "0644",
17:51:36 "mtime": 1518791009.4396753,
17:51:36 "nlink": 1,
17:51:36 "path": "/opt/jenkins/workspace/ANSIBLE_DEPLOY_HP-ALL/dollaru-scripts/Docker/scripts_on_demand/run_api_tracking.ksh",
17:51:36 "rgrp": true,
17:51:36 "roth": true,
17:51:36 "rusr": true,
17:51:36 "size": 476,
17:51:36 "uid": 30000,
17:51:36 "wgrp": false,
17:51:36 "woth": false,
17:51:36 "wusr": true,
17:51:36 "xgrp": false,
17:51:36 "xoth": false,
17:51:36 "xusr": false
17:51:36 }
17:51:36 ],
17:51:36 "matched": 9,
17:51:36 "msg": ""
17:51:36 }
17:51:36 }
现在我想在复制任务中使用我创建的文件的路径;我是这样做的:
- name: Copy foundedfiles
copy:
src: "{{item.path}}"
dest: "/opt/application/i99/sh/onDemand/"
mode: 0755
with_items:
- "{{scripts_on_demand_api.files}}"
when:
- ansible_host in groups['api']
奇怪的是,报错说没有 konwnb "files" 属性!!!
17:51:37 fatal:FAILED! => {"failed": true, "msg":
"'dict object' has no attribute 'files'"}
建议??
您的 when
条件中的主机不匹配,因此您 运行 第二个任务 (copy
) 在没有 运行 第一个任务的主机上(find
).
scripts_on_demand_api
在所有主机上注册,files
仅在主机 not in groups['remoteHosts']
.
上注册
要遍历本地文件,有一种更简单的方法 with_fileglob
loop。
我正在使用 Ansible 2.3 查找具有特定模式的文件并根据它们的路径复制它们。
所以我的搜索任务是这样的:
- name: find onDemand scripts of APIs
find:
paths: "{{WORKSPACE}}/dollaru-scripts/Docker/scripts_on_demand/"
patterns:
- 'run_api*.ksh'
- '*_all_containers.ksh'
- '*_docker_engine.ksh'
register: scripts_on_demand_api
when:
- ansible_host not in groups['remoteHosts']
然后,为了显示结果,我使用了一个 debug
任务并获得了这样的结果,在 Jenkins 下 运行 之后:
ok: [localhost] => {
17:51:36 "changed": false,
17:51:36 "msg": {
17:51:36 "changed": false,
17:51:36 "examined": 13,
17:51:36 "files": [
17:51:36 {
17:51:36 "atime": 1518791011.147691,
17:51:36 "ctime": 1518791009.4396753,
17:51:36 "dev": 64782,
17:51:36 "gid": 15001,
17:51:36 "inode": 301837,
17:51:36 "isblk": false,
17:51:36 "ischr": false,
17:51:36 "isdir": false,
17:51:36 "isfifo": false,
17:51:36 "isgid": false,
17:51:36 "islnk": false,
17:51:36 "isreg": true,
17:51:36 "issock": false,
17:51:36 "isuid": false,
17:51:36 "mode": "0644",
17:51:36 "mtime": 1518791009.4396753,
17:51:36 "nlink": 1,
17:51:36 "path": "/opt/jenkins/workspace/ANSIBLE_DEPLOY_HP-ALL/dollaru-scripts/Docker/scripts_on_demand/run_api_backend.ksh",
17:51:36 "rgrp": true,
17:51:36 "roth": true,
17:51:36 "rusr": true,
17:51:36 "size": 470,
17:51:36 "uid": 30000,
17:51:36 "wgrp": false,
17:51:36 "woth": false,
17:51:36 "wusr": true,
17:51:36 "xgrp": false,
17:51:36 "xoth": false,
17:51:36 "xusr": false
17:51:36 },
{
17:51:36 "atime": 1518791011.147691,
17:51:36 "ctime": 1518791009.4396753,
17:51:36 "dev": 64782,
17:51:36 "gid": 15001,
17:51:36 "inode": 301853,
17:51:36 "isblk": false,
17:51:36 "ischr": false,
17:51:36 "isdir": false,
17:51:36 "isfifo": false,
17:51:36 "isgid": false,
17:51:36 "islnk": false,
17:51:36 "isreg": true,
17:51:36 "issock": false,
17:51:36 "isuid": false,
17:51:36 "mode": "0644",
17:51:36 "mtime": 1518791009.4396753,
17:51:36 "nlink": 1,
17:51:36 "path": "/opt/jenkins/workspace/ANSIBLE_DEPLOY_HP-ALL/dollaru-scripts/Docker/scripts_on_demand/run_api_tracking.ksh",
17:51:36 "rgrp": true,
17:51:36 "roth": true,
17:51:36 "rusr": true,
17:51:36 "size": 476,
17:51:36 "uid": 30000,
17:51:36 "wgrp": false,
17:51:36 "woth": false,
17:51:36 "wusr": true,
17:51:36 "xgrp": false,
17:51:36 "xoth": false,
17:51:36 "xusr": false
17:51:36 }
17:51:36 ],
17:51:36 "matched": 9,
17:51:36 "msg": ""
17:51:36 }
17:51:36 }
现在我想在复制任务中使用我创建的文件的路径;我是这样做的:
- name: Copy foundedfiles
copy:
src: "{{item.path}}"
dest: "/opt/application/i99/sh/onDemand/"
mode: 0755
with_items:
- "{{scripts_on_demand_api.files}}"
when:
- ansible_host in groups['api']
奇怪的是,报错说没有 konwnb "files" 属性!!!
17:51:37 fatal:FAILED! => {"failed": true, "msg": "'dict object' has no attribute 'files'"}
建议??
您的 when
条件中的主机不匹配,因此您 运行 第二个任务 (copy
) 在没有 运行 第一个任务的主机上(find
).
scripts_on_demand_api
在所有主机上注册,files
仅在主机 not in groups['remoteHosts']
.
要遍历本地文件,有一种更简单的方法 with_fileglob
loop。