ansible遍历远端文件的方法

2021-08-03  本文已影响0人  mini鱼

使用 with_fileglob 只能用于遍历执行ansible命令的机器上的文件,不能用于遍历远程服务器上的文件。
我们可以使用 find 模块来生成一个远端服务器上的文件列表:

- name: get rpm in remote /tmp/ipsan/x86_rpms
  find:
    paths: "/tmp/ipsan/x86_rpms"
    file_type: file
    patterns: '*.rpm'
  register: rpm_list

# show data structure result- debug: msg="{{rpm_list}}"
- debug:
    msg: "{{ rpm_list }}"

输出结果:

    "msg": {
        "changed": false,
        "examined": 13,
        "failed": false,
        "files": [
            {
                "atime": 1627956584.573,
                "ctime": 1627956584.573,
                "dev": 64768,
                "gid": 0,
                "gr_name": "root",
                "inode": 13946694,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "mode": "0755",
                "mtime": 1622468825.0,
                "nlink": 1,
                "path": "/tmp/ipsan/x86_rpms/device-mapper-multipath-0.8.4-5.el8.x86_64.rpm",
                "pw_name": "root",
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 196272,
                "uid": 0,
                "wgrp": false,
                "woth": false,
                "wusr": true,
                "xgrp": true,
                "xoth": true,
                "xusr": true
            },
            {
                "atime": 1627956584.573,
                "ctime": 1627956584.573,
                "dev": 64768,
                "gid": 0,
                "gr_name": "root",
                "inode": 13946695,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "mode": "0755",
                "mtime": 1622468824.0,
                "nlink": 1,
                "path": "/tmp/ipsan/x86_rpms/device-mapper-multipath-libs-0.8.4-5.el8.x86_64.rpm",
                "pw_name": "root",
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 323492,
                "uid": 0,
                "wgrp": false,
                "woth": false,
                "wusr": true,
                "xgrp": true,
                "xoth": true,
                "xusr": true
            },
        "matched": 12,
        "msg": ""
    }
}

可以看到文件列表在 .files 里, .path 提供了远程文件的全路径,可以使用下面的任务来拷贝所有的rpm文件

- name: copy each rpm to other directory
  copy:
    remote_source: yes
    src: "{{item.path}}"
    dest: "/var/www/html"
  loop: "{{ rpm_list.files }}"

如有需要,可以使用 basename 过滤器来只获取文件名

{{ item.path | basename }}

参考: Ansible: find module to create glob of remote files

上一篇 下一篇

猜你喜欢

热点阅读