65rew111 发表于 2016-12-29 11:16:29

ansile(2)模块之file

ansbile file模块大体功能同copy模块,下面看看参数


参数
选项
必须说明
followyes/no
no如果原来的文件是link,拷贝后依旧是link
followyes/nono强制文件操作
recurseyes/no
no递归设置目录的属性
groupyes/nono设定一个群组拥有拷贝到远程节点的文件权限
mode yes/nono等同于chmod,参数可以为“u+rwx or u=rw,g=r,o=r”
owneryes/nono设定一个用户拥有拷贝到远程节点的文件
pathyes/noyes目标路径,也可以用dest,name代替
srcyes/noyes要被链接的源文件的路径,只应用于state=link的情况


statefile/link/directory/hard/touch/absent nofile:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件

不对之处请看官方文档
1.1.修改文件属性示例
源文件:

1
2
3
# ll
total 4
-rw-r--r-- 1 root root 17 Dec 28 04:52 2.txt




操作:

1
2
3
4
5
6
7
8
9
10
11
12
# ansible dbserver -m file -a "follow=yes group='ckl' mode='640' owner='ckl' path=/app/2.txt state=file force=yes"
172.16.110.49 | SUCCESS => {
    "changed": true,
    "gid": 1000,
    "group": "ckl",
    "mode": "0640",
    "owner": "ckl",
    "path": "/app/2.txt",
    "size": 17,
    "state": "file",
    "uid": 1000
}





查看文件:

1
2
3
# ll
total 4
-rw-r----- 1 ckl ckl 17 Dec 28 04:52 2.txt





1.2.创建链接文件示例
查看源文件

1
2
# ll mf.txt
-rw-r--r-- 1 root root 21 Dec 28 04:56 mf.txt





# ansible dbserver -m file -a "src=/data/mf.txt dest=/app/kk state=link"
172.16.110.49 | SUCCESS => {
    "changed": true,
    "dest": "/app/kk",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 12,
    "src": "/data/mf.txt",
    "state": "link",
    "uid": 0
}

1
2
3
4
# ll /app/
total 4
-rw-r----- 1 cklckl17 Dec 28 04:52 2.txt
lrwxrwxrwx 1 root root 12 Dec 28 05:06 kk -> /data/mf.txt





1.3.文件删除示例

1
2
3
4
5
6
# ansible dbserver -m file -a "path=/data/mf.txt force=yes state=absent"
172.16.110.49 | SUCCESS => {
    "changed": true,
    "path": "/data/mf.txt",
    "state": "absent"
}





1
2
# ll /data/
total 0



页: [1]
查看完整版本: ansile(2)模块之file