美文网首页
ansible遍历远端文件的方法

ansible遍历远端文件的方法

作者: mini鱼 | 来源:发表于2021-08-03 10:29 被阅读0次

使用 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

相关文章

  • ansible遍历远端文件的方法

    使用 with_fileglob 只能用于遍历执行ansible命令的机器上的文件,不能用于遍历远程服务器上的文件...

  • ansible安装配置

    Ansible 维运自动化工具,基于ssh操作远端服务器。Ansible ,此名取自 Ansible 作者最喜爱的...

  • Ansible基础知识

    Ansible主配置文件:/etc/ansible/ansible.cfgInventory配置文件:/etc/a...

  • ansible配置文件解析

    文章来自于:ansible配置文件解析 ansible配置文件只有一个,即ansible.cfg。ansible....

  • Ansible配置主机名

    本文章分享ansible配置Linux主机名,以及/etc/hosts文件的方法 编辑文件inventory 2....

  • node遍历文件夹并读取文件内容

    先引入node原生方法 先选择要遍历的文件夹 读取文件列表 遍历每个文件 获取文件的本地路径 读取文件信息 判断是...

  • 轻松使用Ansible

    Ansible是一种自动化工具,通过SSH管理远端的服务器。Ansible也是一种编程语言,可以通过这种语言来描述...

  • XMLHTTPRequest状态status完整列表

    AJAX中请求远端文件、或在检测远端文件是否掉链时,都需要了解到远端服务器反馈的状态以确定文件的存在 与否。 当然...

  • Ansible第一篇:基础

    一、配置文件 ansible.cfg /etc/ansible/ansible.cfg 是ansible安装好后...

  • 自动化运维-ansible

    目录 十五、ansible介绍十六、ansible安装十七、ansible远程执行命令十八、ansible拷贝文件...

网友评论

      本文标题:ansible遍历远端文件的方法

      本文链接:https://www.haomeiwen.com/subject/ytnsvltx.html