签到成功

知道了

CNDBA社区CNDBA社区

ansible模块--file模块

2021-11-14 16:07 1026 0 原创 ansible
作者: hbhe0316

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

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

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

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

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

1.创建一个普通文件

http://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.创建一个目录

[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.创建一个软连接文件

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

[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.创建一个硬链接文件

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

[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.修改一个文件得权限,属主,属组

http://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.删除文件 / 目录http://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"
}

删除文件夹http://www.cndba.cn/hbhe0316/article/20967

[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

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
hbhe0316

hbhe0316

关注

1.只有承认无知,才能装下新的东西; 2.进步来自一点点滴滴的积累; 3.广博让你更优秀,而专业让你无法替代; 4.挫折和失败能够转换为一种财富。

  • 889
    原创
  • 1
    翻译
  • 13
    转载
  • 24
    评论
  • 访问:1038944次
  • 积分:1523
  • 等级:核心会员
  • 排名:第6名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ