1. copy模块

功能:从 ansible 服务端主控端复制文件到远程主机;

copy模块的主要参数如下:

参数 说明
src 复制的源文件路径,若源文件为目录,默认进行递归复制,如果路劲以“/”结尾,仅会复制目录下的内容,该目录本身不会复制,如果路径不带“/”,目录本身和目录下的内容会一并复制过去。
dest 目标绝对路径,如果源是文件夹,目标也必须是文件夹,不存在将创建
backup 如果目标主机已经有源文件,会事先备份,防止覆盖
mode 文件复制到远程并设定权限,默认file=644,directory=755
owner 文件复制到远程并设定属主,默认为root
group 文件复制到远程并设定属组,默认为root
content 将目标文件的内容,指定为content所带的字符串
  • 示例一:把/data/nginx/html/web01/index.html复制到被控主机/tmp目录下,属主属组为nginx,权限为644

    [root@xuzhichao ~]# ansible NginxWebs -m copy -a "src=/data/nginx/html/web01/index.html dest=/tmp owner=nginx group=nginx mode=644"
    192.168.20.22 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "b346efabe3aa64027fd74cceabfe9548989cea00",
    "dest": "/tmp/index.html",
    "gid": 887,
    "group": "nginx",
    "md5sum": "022cb99565535954e448519329778662",
    "mode": "0644",
    "owner": "nginx",
    "size": 7,
    "src": "/root/.ansible/tmp/ansible-tmp-1627787786.2-6672-67801950378810/source",
    "state": "file",
    "uid": 887
    }
    192.168.20.23 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "b346efabe3aa64027fd74cceabfe9548989cea00",
    "dest": "/tmp/index.html",
    "gid": 887,
    "group": "nginx",
    "md5sum": "022cb99565535954e448519329778662",
    "mode": "0644",
    "owner": "nginx",
    "size": 7,
    "src": "/root/.ansible/tmp/ansible-tmp-1627787786.2-6673-128530778543575/source",
    "state": "file",
    "uid": 887
    } [root@nginx03 ~]# ll /tmp/index.html
    -rw-r--r-- 1 nginx nginx 7 Aug 1 11:16 /tmp/index.html
  • 示例二:再次复制上例文件,并对原文件备份:

    [root@xuzhichao ~]# ansible NginxWebs -m copy -a "src=/data/nginx/html/web01/index.html dest=/tmp owner=nginx group=nginx mode=644 backup=yes"
    192.168.20.23 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup_file": "/tmp/index.html.6043.2021-08-01@11:20:39~",
    "changed": true,
    "checksum": "f8834b5232582035f785a3dc77a6303e339d1157",
    "dest": "/tmp/index.html",
    "gid": 887,
    "group": "nginx",
    "md5sum": "747df44881df1860170c66321f38ce4c",
    "mode": "0644",
    "owner": "nginx",
    "size": 10,
    "src": "/root/.ansible/tmp/ansible-tmp-1627788037.97-6797-37444206141079/source",
    "state": "file",
    "uid": 887
    }
    192.168.20.22 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "backup_file": "/tmp/index.html.7789.2021-08-01@11:20:39~",
    "changed": true,
    "checksum": "f8834b5232582035f785a3dc77a6303e339d1157",
    "dest": "/tmp/index.html",
    "gid": 887,
    "group": "nginx",
    "md5sum": "747df44881df1860170c66321f38ce4c",
    "mode": "0644",
    "owner": "nginx",
    "size": 10,
    "src": "/root/.ansible/tmp/ansible-tmp-1627788037.98-6796-83776884129851/source",
    "state": "file",
    "uid": 887
    } [root@nginx03 ~]# ll /tmp/index.html*
    -rw-r--r-- 1 nginx nginx 10 Aug 1 11:20 /tmp/index.html
    -rw-r--r-- 1 nginx nginx 7 Aug 1 11:16 /tmp/index.html.6043.2021-08-01@11:20:39~ <==备份的原文件
  • 往远程的主机文件中写入内容,如果文件不存在则创建:

    [root@xuzhichao ~]# ansible NginxWebs -m copy -a "content="Http_Server\n" dest=/var/www/html/index.html"
    192.168.20.23 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "50d275e32df9f317f802475b11627144db2f5d3e",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "4ab65dcd3bf954b6e54e9ff439721ae8",
    "mode": "0644",
    "owner": "root",
    "size": 11,
    "src": "/root/.ansible/tmp/ansible-tmp-1627788165.69-6855-262538185402060/source",
    "state": "file",
    "uid": 0
    }
    192.168.20.22 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "50d275e32df9f317f802475b11627144db2f5d3e",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "4ab65dcd3bf954b6e54e9ff439721ae8",
    "mode": "0644",
    "owner": "root",
    "size": 11,
    "src": "/root/.ansible/tmp/ansible-tmp-1627788165.71-6854-248696166101546/source",
    "state": "file",
    "uid": 0
    } [root@nginx03 ~]# cat /var/www/html/index.html
    Http_Server
  • 示例四:复制目录到目标主机:

    [root@xuzhichao ~]# ansible NginxWebs -m copy -a "src=/root/test dest=/root"
    192.168.20.23 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "feba0582dfea7fc4fd236536b6a1173b19228388",
    "dest": "/root/test/nginx.conf",
    "gid": 0,
    "group": "root",
    "md5sum": "482efd688106eebebda54c12e52ddd01",
    "mode": "0644",
    "owner": "root",
    "size": 3258,
    "src": "/root/.ansible/tmp/ansible-tmp-1627788373.43-6930-280408452171512/source",
    "state": "file",
    "uid": 0
    }
    192.168.20.22 | CHANGED => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "checksum": "feba0582dfea7fc4fd236536b6a1173b19228388",
    "dest": "/root/test/nginx.conf",
    "gid": 0,
    "group": "root",
    "md5sum": "482efd688106eebebda54c12e52ddd01",
    "mode": "0644",
    "owner": "root",
    "size": 3258,
    "src": "/root/.ansible/tmp/ansible-tmp-1627788373.42-6929-226478301245663/source",
    "state": "file",
    "uid": 0
    } [root@nginx03 ~]# ll /root/test/
    total 4
    -rw-r--r-- 1 root root 3258 Aug 1 11:26 nginx.conf

2. fetch模块

作用:从客户端取文件(只能是文件,不支持目录)至服务器端的目录里,与copy相反,如果一定要拉取目录,可以先将目录tar,再拉取。

主要参数如下:

参数 说明
src 复制的源文件路径,源文件只能是文件
dest 目标绝对路径
  • 示例一:把被控主机的/etc/nginx/nginx/conf配置文件拷贝到本机的/root/nginx目录下:

    [root@xuzhichao ~]# ansible NginxWebs -m fetch -a 'src=/etc/nginx/nginx.conf dest=/root/nginx'
    192.168.20.22 | CHANGED => {
    "changed": true,
    "checksum": "feba0582dfea7fc4fd236536b6a1173b19228388",
    "dest": "/root/nginx/192.168.20.22/etc/nginx/nginx.conf",
    "md5sum": "482efd688106eebebda54c12e52ddd01",
    "remote_checksum": "feba0582dfea7fc4fd236536b6a1173b19228388",
    "remote_md5sum": null
    }
    192.168.20.23 | CHANGED => {
    "changed": true,
    "checksum": "b9671ff7a350dbf83543ec585e776500d45dccf0",
    "dest": "/root/nginx/192.168.20.23/etc/nginx/nginx.conf",
    "md5sum": "ee4c9a5f234057114b6454055df5f3a5",
    "remote_checksum": "b9671ff7a350dbf83543ec585e776500d45dccf0",
    "remote_md5sum": null
    } #注意:把远程主机文件拷贝到本机时,会为每个远程主机建立一个文件夹,名称就是该远程主机的ip地址,然后把文件分别放到对应主机的目录下;
    [root@xuzhichao ~]# tree /root/nginx
    /root/nginx
    ├── 192.168.20.22
    │   └── etc
    │   └── nginx
    │   └── nginx.conf
    └── 192.168.20.23
    └── etc
    └── nginx
    └── nginx.conf 6 directories, 2 files
  • 示例二:拷贝远程主机的目录到本机

    #1.首先需要把远程主机的目录打包,默认打包到远程主机的家目录下(/root)
    [root@xuzhichao ~]# ansible NginxWebs -m shell -a 'tar jcf log.tar.bzip2 /var/log/nginx/access*'
    [WARNING]: Consider using the unarchive module rather than running 'tar'. If you need to use command because unarchive is insufficient you can add 'warn:
    false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
    192.168.20.23 | CHANGED | rc=0 >>
    tar: Removing leading `/' from member names
    192.168.20.22 | CHANGED | rc=0 >>
    tar: Removing leading `/' from member names #2.把打包文件取回本机:
    [root@xuzhichao ~]# ansible NginxWebs -m fetch -a 'src=/root/log.tar.bzip2 dest=/root/nginx'
    192.168.20.23 | CHANGED => {
    "changed": true,
    "checksum": "832b6b9863baf60970f0972c749df8a26221ab26",
    "dest": "/root/nginx/192.168.20.23/root/log.tar.bzip2",
    "md5sum": "86c762c3ab6f3253c6b6be1739a95ac8",
    "remote_checksum": "832b6b9863baf60970f0972c749df8a26221ab26",
    "remote_md5sum": null
    }
    192.168.20.22 | CHANGED => {
    "changed": true,
    "checksum": "c66537be74f74e1c03254be3400ce7af4a1fe4bb",
    "dest": "/root/nginx/192.168.20.22/root/log.tar.bzip2",
    "md5sum": "74671b6a38285ae2d576451b3b248f3e",
    "remote_checksum": "c66537be74f74e1c03254be3400ce7af4a1fe4bb",
    "remote_md5sum": null
    } [root@xuzhichao ~]# tree /root/nginx
    /root/nginx
    ├── 192.168.20.22
    │   ├── etc
    │   │   └── nginx
    │   │   └── nginx.conf
    │   └── root
    │   └── log.tar.bzip2
    └── 192.168.20.23
    ├── etc
    │   └── nginx
    │   └── nginx.conf
    └── root
    └── log.tar.bzip2 8 directories, 4 files

ansible(6)--ansible的copy和fetch模块的更多相关文章

  1. 利用Ansible模块copy和fetch进行主机间文件的传递

    场景: java应用程序和Ansible不在同一台机子,要读取的文件又在另一台主机. 主机a不能保存文件,可以临时保存. 文件都在主机b上保存. 需求: 需要将文件从主机c传到主机b,再从主机b传到主 ...

  2. ansible模块之command、shell、script、file、copy、fetch

    前戏 ansible 批量在远程主机上执行命令 openpyxl 操作excel表格 puppet ansible slatstack ansible epel源 第一步: 下载epel源 wget ...

  3. Ansible系列(二):选项和常用模块

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  4. ansible笔记(4):常用模块之文件操作

    前文中,我们已经介绍了怎样使用模块,而且我们知道,ansible有很多模块,每个模块都有自己的功能,"模块"涉及到的方向比较多,所以对于个人来说,并没有必要了解所有的模块,我们只需 ...

  5. Ansible自动化搭建及工具集和常见模块、命令详情(重点)

    一.ansible介绍 1.ansible简介 官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具. Ansible跟其他IT自动化技术的区别 ...

  6. Ansible自动化运维工具及其常用模块

    Ansible自动化运维工具及其常用模块 目录 Ansible自动化运维工具及其常用模块 一.Ansible简介 1. Ansible概述 2. Ansible作用 3. Ansible的工作模块 4 ...

  7. ansible基础-ansible角色的使用

    ansible基础-ansible角色的使用 作者:尹正杰  版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们建议把多个节点都会用到的功能将其定义模块,然后谁要用到该模块就直接调用即可!而在a ...

  8. 什么是 Ansible - 使用 Ansible 进行配置管理

    [注]本文译自:https://www.edureka.co/blog/what-is-ansible/   Ansible 是一个开源的 IT 配置管理.部署和编排工具.它旨在为各种自动化挑战提供巨 ...

  9. ansible配置文件 ansible.cfg的一点说明

    ansible配置文件 ansible.cfg的一点说明 > ansible --version ansible 2.1.1.0 config file = /etc/ansible/ansib ...

  10. copy模块与fetch模块

    copy:将本地机器上的文件拷贝到远程机器 fetch:将远程机器上的文件拷贝到本地机器 [root@localhost zabbix]# ansible-doc -s copy - name: Co ...

随机推荐

  1. archlinux xfce未中文化 goldendict不能显示中文

    下载个中文字体包就好了 https://wiki.archlinuxcn.org/wiki/简体中文本地化

  2. Python爬取国家统计局2009至2020统计用区划和城乡划分代码(省市区/县三级)并存入mysql数据库

    国家统计局->统计标准网址:http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/ 获取资源请关注公众号 [靠谱杨阅读人生]回复[城乡分类]获取 流程 ...

  3. Wasm软件生态系统安全分析

    演讲嘉宾 | 王浩宇 回顾整理 | 廖   涛 排版校对 | 李萍萍 嘉宾简介 王浩宇,华中科技大学教授,博士生导师,华中科技大学OpenHarmony技术俱乐部主任.研究关注于新兴软件系统中的安全. ...

  4. 本周二晚19:00战码先锋第5期直播丨深入理解OpenHarmony系统启动,轻松踏上设备软件开发之旅

    OpenAtom OpenHarmony(以下简称"OpenHarmony")工作委员会首度发起「OpenHarmony开源贡献者计划」,旨在鼓励开发者参与OpenHarmony开 ...

  5. HMS Core Discovery第15期直播预告|构筑立体世界,共造沉浸式营销

    [导读] AR技术,是一种将真实世界信息和虚拟世界信息"无缝"衔接的技术,现如今AR技术受到日益广泛的关注,在我们生活中发挥着重要的作用,并显示出巨大的潜力--它是如何改变我们观察 ...

  6. Python将依赖包导出到requirements.txt文件

    代码 # 查询环境中已经安装的库 pip list # 将所有依赖库导出到 requirements.txt 文件 pip freeze > requirements.txt

  7. Unity-PC 端调用SpVoice语音 (文字转语音)

    第一步引用文件 在VS当中 点击项目->添加引用-> 搜索Microsoft Speech Objecet Library 然后选中前面的白色方块点击确定就行了 插入之后 你的引用库中会多 ...

  8. HarmonyOS Codelab样例—弹窗基本使用

    一.介绍 本篇 Codelab 主要基于 dialog 和 button 组件,实现弹窗的几种自定义效果,具体效果有: 1.  警告弹窗,点击确认按钮弹窗关闭. 2.  确认弹窗,点击取消按钮或确认按 ...

  9. centos7搭建vsftpd环境详解[亲测成功]

    centos7搭建vsftpd环境详解(亲测) 标签: centos7vsftpd   分类: linux相关(关于centos)(2)  版权声明:本文为博主原创文章,未经博主允许不得转载. 初学L ...

  10. 重学c#系列——什么是性能[外篇性能篇一]

    前言 简单写一下性能的简介. 正文 什么是性能,很多时候有一个问题,那就很多人喜欢说.这个服务有很多访问,我们需要这样设计. 这是一个无法验证的指标,访问次数是多少? 响应时间是多少. 我把这归纳为自 ...