【问题标题】:Ansible: find files and copy themAnsible:查找文件并复制它们
【发布时间】:2018-02-16 17:00:35
【问题描述】:

我正在使用 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 致命:失败! => {“失败”:真,“味精”: "'dict object' 没有属性 'files'"}

建议??

【问题讨论】:

    标签: ansible ansible-2.x


    【解决方案1】:

    when 条件中的主机不匹配,因此您在未运行第一个任务 (find) 的主机上运行第二个任务 (copy)。

    scripts_on_demand_api 在所有主机上注册,files 仅在主机not in groups['remoteHosts'] 上注册。


    要遍历本地文件,有一个更简单的方法,with_fileglob loop

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多