ansible--copy与fetch模块-阿里云开发者社区

开发者社区> 云计算> 正文

ansible--copy与fetch模块

简介: copy模块--传输文件本机将test1文件传输至指定用户组的目录中,src为本机目录,dest为目标机器目录,默认传输文件为644权限,不管传输前的权限如何;[root@10-15-190-167 ~]# ll-rwxr----- 1 root root 9 Nov 21 14:...

copy模块--传输文件
本机将test1文件传输至指定用户组的目录中,src为本机目录,dest为目标机器目录,默认传输文件为644权限,不管传输前的权限如何;
[root@10-15-190-167 ~]# ll
-rwxr----- 1 root root 9 Nov 21 14:39 test1

[root@10-15-190-167 ~]# ansible nginx2 -m copy -a "src=/root/test1 dest=/tmp "
后面也可以跟上mode直接指定文件权限
10.15.100.55 | SUCCESS => {

"changed": false, 
"checksum": "14de3ab563574b5711c56ad55d8b7c0f9edba6e4", 
"dest": "/tmp/test1", 
"gid": 0, 
"group": "root", 
"mode": "0644", 
"owner": "root", 
"path": "/tmp/test1", 
"size": 9, 
"state": "file", 
"uid": 0

}
[root@10-15-100-55 tmp]# ll
-rw-r--r-- 1 root root 9 Nov 21 14:40 test1
file模块---修改文件组权限
ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
创建文件及删除文件
ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"
ansible webservers -m file -a "dest=/path/to/c state=absent"
[root@10-15-190-167 ~]# ansible nginx2 -m file -a "dest=/tmp/test1 mode=777"

将nginx2组机器的/tmp/test1文件权限改为777

10.15.100.55 | SUCCESS => {

"changed": true, 
"gid": 0, 
"group": "root", 
"mode": "0777", 
"owner": "root", 
"path": "/tmp/test1", 
"size": 9, 
"state": "file", 
"uid": 0

}
创建文件stater的值为:ile,directory,link,hard,touch,absent
注意:指定创建文件时必需存在真正的用户和组否则会出错,默认改到root用户755的权限
[root@10-15-190-167 ~]# ansible test -m file -a "dest=/tmp/test2 mode=744 owner=test group=app state=directory"
10.15.195.231 | FAILED! => {

"changed": false, 
"failed": true, 
"gid": 0, 
"group": "root", 
"mode": "0755", 
"msg": "chown failed: failed to look up user test", 
"owner": "root", 
"path": "/tmp/test2", 
"size": 6, 
"state": "directory", 
"uid": 0

}
10.15.66.32 | SUCCESS => {

"changed": true, 
"gid": 1000, 
"group": "app", 
"mode": "0744", 
"owner": "test", 
"path": "/tmp/test2", 
"size": 6, 
"state": "directory", 
"uid": 1002

}
以下为错误示例
[root@10-15-190-167 ~]# ansible nginx2 -m file -a "dest=/tmp/test2 mode=744 owner=test group=app state=directory"
10.15.100.55 | FAILED! => {

"changed": false, 
"failed": true, 
"gid": 0, 
"group": "root", 
"mode": "0755", 
"msg": "chown failed: failed to look up user test", 
"owner": "root", 
"path": "/tmp/test2", 
"size": 4096, 
"state": "directory", 
"uid": 0

}
删除test2目录,此删除为递归删除下面所有子文件及目录均被删除
[root@10-15-190-167 ~]# ansible test -m file -a "dest=/tmp/test2 state=absent"
10.15.66.32 | SUCCESS => {

"changed": true, 
"path": "/tmp/test2", 
"state": "absent"

}
10.15.195.231 | SUCCESS => {

"changed": true, 
"path": "/tmp/test2", 
"state": "absent"

}
fetch模块--远端复制到本机
[root@10-15-76-70 ~]# ansible test -m fetch -a "src=/root/test.txt dest=/root"
10.15.66.32 | SUCCESS => {

"changed": true, 
"checksum": "83f73ea502e8eeb539b5703ec2873b552949322e", 
"dest": "/root/10.15.66.32/root/test.txt", 
"md5sum": "ecf98f9207fc85a5537558b85e44d87d", 
"remote_checksum": "83f73ea502e8eeb539b5703ec2873b552949322e", 
"remote_md5sum": null

}
10.15.195.231 | SUCCESS => {

"changed": false, 
"file": "/root/test.txt", 
"msg": "the remote file does not exist, not transferring, ignored"

}
自动以目标机器IP地址在本地生成文件夹
[root@10-15-76-70 ~]# ls
10.15.66.32 adduser.sh kernel-4.1.0-8.el7.ucloud
4.1.0-8.el7.ucloud.x86_64.tar.gz itms.tar.gz
[root@10-15-76-70 root]# ls
test.txt
[root@10-15-76-70 root]# pwd
/root/10.15.66.32/root

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

其他文章