ansible copy 模块详解
ansible 模块 copy
one、概述
- copy 模块的作用就是拷贝文件,它与之前介绍过的 fetch 模块类似,不过,fetch 模块是从远程主机中拉取文件到 ansible 管理主机,而 copy 模块是将 ansible 管理主机上的文件拷贝到远程主机中。
two、常用参数
src参数 :用于指定需要copy的文件或目录。
dest参数 :用于指定文件将被拷贝到远程主机的哪个目录中,dest为必须参数。
content参数 :当不使用src指定拷贝的文件时,可以使用content直接指定文件内容,src与content两个参数必有其一,否则会报错。
force参数 : 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否强制覆盖,可选值有yes和no,默认值为yes,表示覆盖,如果设置为no,则不会执行覆盖拷贝操作,远程主机中的文件保持不变。
backup参数 : 当远程主机的目标路径中已经存在同名文件,并且与ansible主机中的文件内容不同时,是否对远程主机的文件进行备份,可选值有yes和no,当设置为yes时,会先备份远程主机中的文件,然后再将ansible主机中的文件拷贝到远程主机。
owner参数 : 指定文件拷贝到远程主机后的属主,但是远程主机上必须有对应的用户,否则会报错。
group参数 : 指定文件拷贝到远程主机后的属组,但是远程主机上必须有对应的组,否则会报错。
mode参数 : 指定文件拷贝到远程主机后的权限,如果你想将权限设置为”rw-r--r--“,则可以使用mode=0644表示,如果你想要在user对应的权限位上添加执行权限,则可以使用mode=u+x表示。
thsree、示例
1.将 ansible 管理主机中 /testdir/copytest 文件复制到远程主机的 /testdir 目录下。
ansible 管理主机中的文件内容为:
[root@ansible-manager ~]# cat /testdir/copytest
123
456123
执行 copy:
[root@ansible-manager ~]# ansible ansible-demo3 -m copy -a "src=/testdir/copytest dest=/testdir/"
ansible-demo3 | FAILED! => {
"changed": false,
"checksum": "47ce3ac56f911ac44537d6a3802b72ceed71e152",
"msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
}123456
$根据报错信息,ansible-demo3 主机需要安装 libselinux-python。 $
[root@ansible-demo3 ~]# yum install libselinux-python -y1
安装完毕后,再次执行:
[root@ansible-manager ~]# ansible ansible-demo3 -m copy -a "src=/testdir/copytest dest=/testdir/"
ansible-demo3 | SUCCESS => {
"changed": true,
"checksum": "47ce3ac56f911ac44537d6a3802b72ceed71e152",
"dest": "/testdir/copytest",
"gid": 0,
"group": "root",
"md5sum": "c010aff9dc6276fdb7efefd1a2757658",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:default_t:s0",
"size": 8,
"src": "/root/.ansible/tmp/ansible-tmp-1526023671.74-99930404286274/source",
"state": "file",
"uid": 0
}12345678910111213141516
成功执行后,看看 ansible-demo3 的 /testdir/copytest 文件:
[root@ansible-demo3 ~]# cat /testdir/copytest
123
456123
2.在远程主机的 /testdir 目录下生成文件 testfile1,testfile1 文件中有两行文本,第一行文本为 aaa,第二行为 bbb,当使用 content 指定文件内容时,dest 参数对应的值必须是一个文件,而不能是一个路径。
[root@ansible-manager ~]# ansible ansible-demo3 -m copy -a 'content="aaa\nbbb\n" dest=/testdir/testfile1'
ansible-demo3 | SUCCESS => {
"changed": true,
"checksum": "90c206af0bfefa95541d3e724efe1dbc1ed3877f",
"dest": "/testdir/testfile1",
"gid": 0,
"group": "root",
"md5sum": "8b652b8c79f357694a04bd793f533c96",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:default_t:s0",
"size": 8,
"src": "/root/.ansible/tmp/ansible-tmp-1526113265.26-187047800648668/source",
"state": "file",
"uid": 0
}12345678910111213141516
- 因为 ansible-demo3 主机上面之前已经存在 /testdir/testfile1 文件,所以执行成功后返回 “changed” 为 true。
可以看到,文件内容已经改写:
执行前
[root@ansible-demo3 ~]# cat /testdir/testfile11
执行后
[root@ansible-demo3 ~]# cat /testdir/testfile1
aaa
bbb123
3.将 ansible 主机中 /testdir/copytest 文件复制到远程主机的 /testdir 目录中时,如果远程主机中已经存在 /testdir/copytest 文件,并且文件内容与 ansible 主机中的 copytest 文件的内容不一致,则不执行拷贝操作,远程主机中的 /testdir/copytest 文件内容不会被改变。
[root@ansible-manager ~]# ansible ansible-demo3 -m copy -a "src=/testdir/copytest dest=/testdir/ force=no"
ansible-demo3 | SUCCESS => {
"changed": false,
"dest": "/testdir/",
"src": "/testdir/copytest"
}123456
4.将 ansible 主机中 /testdir/copytest 文件复制到远程主机的 /testdir 目录中时,如果远程主机中已经存在 /testdir/copytest 文件,并且文件内容与 ansible 主机中的 /testdir/copytest 文件的内容不一致,会执行拷贝操作,但是在执行拷贝操作之前,会将远程主机中的原文件重命名,以作备份,然后再进行拷贝操作。
先修改 /testdir/copytest 文件
[root@ansible-manager ~]# cat /testdir/copytest
123
456
[root@ansible-manager ~]# vi /testdir/copytest
[root@ansible-manager ~]# cat /testdir/copytest
123
456
78912345678
执行 copy
[root@ansible-manager ~]# ansible ansible-demo3 -m copy -a "src=/testdir/copytest dest=/testdir/ backup=yes"
ansible-demo3 | SUCCESS => {
"backup_file": "/testdir/copytest.20579.2018-05-12@16:31:27~",
"changed": true,
"checksum": "a8d207d098d939cb0dc9df1f3a2b986d6d4499b2",
"dest": "/testdir/copytest",
"gid": 0,
"group": "root",
"md5sum": "a2ef74a76b2bfcfe14817a27c511759c",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:default_t:s0",
"size": 12,
"src": "/root/.ansible/tmp/ansible-tmp-1526113886.45-35720740584873/source",
"state": "file",
"uid": 0
}1234567891011121314151617
查看 ansible-demo3 主机 /testdir 下面的文件
copytest.20579.2018-05-12@16:31:27~ 为备份的文件
[root@ansible-demo3 ~]# ls /testdir/
copytest copytest.20579.2018-05-12@16:31:27~ test testfile1 testfile2
[root@ansible-demo3 ~]# cat /testdir/copytest
123
456
789
[root@ansible-demo3 ~]# cat /testdir/copytest.20579.2018-05-12@16:31:27~
123
456123456789
5.拷贝文件时,指定文件的属主,需要注意,远程主机上必须存在对应的用户。
[root@ansible-manager ~]# ansible ansible-demo3 -m copy -a "src=/testdir/copytest dest=/testdir/ owner=ding"
ansible-demo3 | SUCCESS => {
"changed": true,
"checksum": "a8d207d098d939cb0dc9df1f3a2b986d6d4499b2",
"dest": "/testdir/copytest",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "ding",
"path": "/testdir/copytest",
"secontext": "system_u:object_r:default_t:s0",
"size": 12,
"state": "file",
"uid": 1000
}123456789101112131415
可以看到,执行前后的变化
6.拷贝文件时,指定文件的属组,需要注意,远程主机上必须存在对应的组。
[root@ansible-manager ~]# ansible ansible-demo3 -m copy -a "src=/testdir/copytest dest=/testdir/ group=ding"
ansible-demo3 | SUCCESS => {
"changed": true,
"checksum": "a8d207d098d939cb0dc9df1f3a2b986d6d4499b2",
"dest": "/testdir/copytest",
"gid": 1000,
"group": "ding",
"mode": "0644",
"owner": "ding",
"path": "/testdir/copytest",
"secontext": "system_u:object_r:default_t:s0",
"size": 12,
"state": "file",
"uid": 1000
}123456789101112131415
可以看到执行前后的变化
7.拷贝文件时,指定文件的权限。
[root@ansible-manager ~]# ansible ansible-demo3 -m copy -a "src=/testdir/copytest dest=/testdir/ mode=0640"
ansible-demo3 | SUCCESS => {
"changed": true,
"checksum": "a8d207d098d939cb0dc9df1f3a2b986d6d4499b2",
"dest": "/testdir/copytest",
"gid": 1000,
"group": "ding",
"mode": "0640",
"owner": "ding",
"path": "/testdir/copytest",
"secontext": "system_u:object_r:default_t:s0",
"size": 12,
"state": "file",
"uid": 1000
}123456789101112131415
可以看到执行前后的变化
ansible copy 模块详解的更多相关文章
- Ansible 常用模块详解
经过前面的介绍,我们已经熟悉了 Ansible 的一些常识性的东西和如何编译安装Ansible,从本章开始我们将全面介绍 Ansible 的各种生产常用模块,这些也是我们使用 Ansible 的过程中 ...
- ansible常用模块详解(三)
1.模块介绍 明确一点:模块的执行就类似是linux命令的一条命令,就单单的是为了执行一条语句,不是批量的操作,批量操作需要用到playbook内类似shell编写脚本进行批量. 1.1 模块的使用方 ...
- python标准库介绍——9 copy模块详解
==copy 模块== ``copy`` 模块包含两个函数, 用来拷贝对象, 如 [Example 1-64 #eg-1-64] 所示. ``copy(object) => object`` 创 ...
- Ansible lineinfile模块详解
目录 简介 修改匹配行 在匹配行前或后添加内容 在匹配行前添加 在匹配行后添加 修改文件内容及权限 删除一行内容 文件存在则添加一行内容 如果有匹配的行则修改该行,如果不匹配则添加 参数backref ...
- Ansible安装部署及常用模块详解
Ansible命令使用 Ansible语法使用ansible <pattern_goes_here> -m <module_name> -a <arguments> ...
- ansible中常用模块详解
ansible中常用的模块详解: file模块 ansible内置的可以查看模块用法的命令如下: [root@docker5 ~]# ansible-doc -s file - name: Sets ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
- 铁乐学python_shelve模块详解
python序列化模块-shelve模块详解 shelve:vt. 将(书等)放置在架子上:搁置,将某事放到一旁不予考虑:将-搁在一边:装搁架于: 个人感觉有点像字典缓存?暂时搁置到一旁的意思? 研究 ...
- (转)python之os,sys模块详解
python之sys模块详解 原文:http://www.cnblogs.com/cherishry/p/5725184.html sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和 ...
随机推荐
- lua-nginx-module模块里ngx_lua的所有指令以及可用ngx所有方法
http://www.04007.cn/article/430.html
- .NET Core微服务之基于Steeltoe使用Hystrix熔断保护与监控
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
- 【设计模式+原型理解】第一章:使用Javascript来巧妙实现经典的设计模式
刚开始学习设计模式之前,我是没想说要学习设计模式的,我只是因为想学习JS中的原型prototype知识,一开始我想JS中为什么要存在原型这个东西?于是慢慢通过原型而接触到设计模式,后来发现我这个过程是 ...
- ado.net的简单数据库操作(三)——简单增删改查的实际应用
果然,在犯困的时候就该写写博客,写博客就不困了,哈哈! 上篇我记录了自己的SqlHelper的开发过程,今天记录一下如何使用这个sqlhelper书写一个具有简单增删改查的小实例啦. 实例描述:在数据 ...
- jq切换面板
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- Lucene配置步骤详解
Lucene配置步骤说明: 1.搭建环境: 2.创建索引库: 3搜索索引库. Lucene配置步骤: 第一部分:搭建环境(创建环境导入jar包) 前提:已经创建好了数据库(直接导入book.sql文件 ...
- DRDS分布式SQL引擎—执行计划介绍
摘要: 本文着重介绍 DRDS 执行计划中各个操作符的含义,以便用户通过查询计划了解 SQL 执行流程,从而有针对性的调优 SQL. DRDS分布式SQL引擎 — 执行计划介绍 前言 数据库系统中,执 ...
- Effective Java目录
创建和销毁对象 考虑用静态工厂方法代替构造器 遇到多个构造器参数时要考虑用构建器 用私有构造器或者枚举类型强化Singleton属性 通过私有构造器强化不可实例化能力 避免创建不必要的对象 消除过期的 ...
- jQueryMobile 網頁使用 ASP.NET Web API 服務
微軟的 ASP.NET Web API 框架,能以 RESTful 輕量級的架構風格,建立 HTTP 服務,讓多種不同的用戶端,如: 手機.平板.電腦(PC),透過 HTTP 的 GET.POST.P ...
- VR一体机如何退出FFBM
Fast Factory Boot Mode(FFBM)是一种半开机的模式,它的主要目的是方便工厂测试,提高生产效率.正常情况下终端用户是不会碰到的.但售后的同学最近连续收到几台客户退 ...