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

目录
相关文章
|
3月前
|
JavaScript Unix Linux
⨯ cannot execute cause=fork/exec...pnpm\bin\pnpm.cjs: %1 is not a valid Win32 application.
⨯ cannot execute cause=fork/exec...pnpm\bin\pnpm.cjs: %1 is not a valid Win32 application.
120 0
|
3月前
|
Linux iOS开发 MacOS
pnpm全局安装报错:Run “pnpm setup“ to create it automatically, or set the global-bin-dir setting, or the PN
pnpm全局安装报错:Run “pnpm setup“ to create it automatically, or set the global-bin-dir setting, or the PN
490 0
|
缓存 开发工具 git
报错:Git上传代码报错 will not add file alias already exists in index
报错:Git上传代码报错 will not add file alias already exists in index
389 0
|
5月前
|
JavaScript
命令行 npm config set legacy-peer-deps true 的作用
命令行 npm config set legacy-peer-deps true 的作用
77 0
|
5月前
|
Python
【python读取nc文件】报错:ValueError: unrecognized engine netcdf4 must be one of: [‘store‘]
【python读取nc文件】报错:ValueError: unrecognized engine netcdf4 must be one of: [‘store‘]
134 0
|
10月前
|
开发工具 git
git submodule update --init --recursive失败的处理办法
git submodule update --init --recursive失败的处理办法
861 0
|
开发工具 git
Can‘t find third_party/gtest. Run git submodule update --init, or disable with CMake -DBUILD_TEST
Can‘t find third_party/gtest. Run git submodule update --init, or disable with CMake -DBUILD_TEST
101 0
|
iOS开发 MacOS
如何解决npm命令报错Missing write access to /usr/local/lib/node_modules
如何解决npm命令报错Missing write access to /usr/local/lib/node_modules
如何解决npm命令报错Missing write access to /usr/local/lib/node_modules
LXJ
09-copy模块Ansible常用模块
09-copy模块Ansible常用模块
LXJ
124 0
09-copy模块Ansible常用模块