ansible模块--file模块

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

path 指定文件 如果远程主机上没有该文件,则进行创建
state 创建类型 touch 文件 directory 目录
state=absent 删除文件或者目录https://www.cndba.cn/hbhe0316/article/20967

https://www.cndba.cn/hbhe0316/article/20967

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

以下三个参数,既可以修改,也可以自动添加
mod:权限 可以在添加时设置特殊权限,前提要有执行权限( set 粘滞位)
owner:属主
group:属组https://www.cndba.cn/hbhe0316/article/20967

1.创建一个普通文件https://www.cndba.cn/hbhe0316/article/20967https://www.cndba.cn/hbhe0316/article/20967

[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.创建一个目录https://www.cndba.cn/hbhe0316/article/20967

[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.创建一个文件,并设置权限,属主,属组https://www.cndba.cn/hbhe0316/article/20967https://www.cndba.cn/hbhe0316/article/20967

[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.修改一个文件得权限,属主,属组https://www.cndba.cn/hbhe0316/article/20967

[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.删除文件 / 目录https://www.cndba.cn/hbhe0316/article/20967

[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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值