创建或者和删除远程主机上的文件或者目录

path 指定文件 如果远程主机上没有该文件,则进行创建
state 创建类型 touch 文件 directory 目录

state=absent 删除文件或者目录

link 软连接 src=源文件名 path=目标链接文件名
hard 硬链接 src=源文件名 path=目标链接文件名

以下三个参数,既可以修改,也可以自动添加
mod:权限 可以在添加时设置特殊权限,前提要有执行权限( set 粘滞位)
owner:属主

group:属组

1.创建一个普通文件

[root@ansible ~]# ansible mysql -m file -a 'path=/tmp/test.txt state=touch'
192.168.56.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/test.txt",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"state": "file",
"uid": 0
}

2.创建一个目录

[root@ansible ~]# ansible mysql -m file -a 'path=/tmp/test state=directory'
192.168.56.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/test",
"size": 6,
"state": "directory",
"uid": 0
}

3.创建一个软连接文件

[root@ansible tmp]# ansible mysql -m file -a 'src=/tmp/test state=link path=/tmp-test-ln'
192.168.56.88 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"dest": "/tmp-test-ln",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 9,
"src": "/tmp/test",
"state": "link",
"uid": 0
}

4.创建一个硬链接文件

[root@ansible tmp]# ansible mysql -m file -a 'src=/tmp/test state=hard path=/tmp-test-ln-hard'
192.168.56.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp-test-ln-hard",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"size": 0,
"src": "/tmp/test",
"state": "hard",
"uid": 0
}

5.创建一个文件,并设置权限,属主,属组​

[root@ansible tmp]# ansible mysql -m file -a 'state=touch path=/tmp/test mode=777 owner=mysql group=mysql'
192.168.56.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/test",
"gid": 1000,
"group": "mysql",
"mode": "0777",
"owner": "mysql",
"size": 0,
"state": "file",
"uid": 1000
}

6.修改一个文件得权限,属主,属组

[root@ansible tmp]# ansible mysql -m file -a 'path=/tmp/test mode=000'
192.168.56.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1000,
"group": "mysql",
"mode": "0000",
"owner": "mysql",
"path": "/tmp/test",
"size": 0,
"state": "file",
"uid": 1000
}
[root@ansible tmp]# ansible mysql -m file -a 'path=/tmp/test owner=root'
192.168.56.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1000,
"group": "mysql",
"mode": "0000",
"owner": "root",
"path": "/tmp/test",
"size": 0,
"state": "file",
"uid": 0
}
[root@ansible tmp]# ansible mysql -m file -a 'path=/tmp/test group=root'
192.168.56.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0000",
"owner": "root",
"path": "/tmp/test",
"size": 0,
"state": "file",
"uid": 0
}

7.删除文件 / 目录

[root@ansible tmp]# ansible mysql -m file -a 'state=absent path=/tmp/test'
192.168.56.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/test",
"state": "absent"
}

删除文件夹

[root@ansible tmp]# ansible mysql -m file -a 'state=absent path=/tmp/directory'
192.168.56.88 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/directory",
"state": "absent"
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

ansible