阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Ansible-file模块

162次阅读
没有评论

共计 4946 个字符,预计需要花费 13 分钟才能阅读完成。

一、file 模块 (重点)

file 模块用于对文件或文件夹相关的操作,主要用来设置文件、链接、目录的属性,或者移除文件、链接、目录,很多其他的模块也会包含这种作用,例如 copy,assemble 和 template。

https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module

参数 说明
path 文件绝对路径
state 操作 (touch 文件新建、absent 删除、link 软连接、hard 硬链接、directory 目录创建)
owner 设置所有者
group 设置所属的组
mode 权限 0000
recurse 递归 yes or no

文件的创建

在所有的业务机器的 /tmp 下创建一个文件:zutuanxue

[root@manage01 ~]# ansible -m file group1 -a "path=/tmp/zutuanxue state=touch" 192.168.98.202 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "dest": "/tmp/zutuanxue", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 } 192.168.98.203 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "dest": "/tmp/zutuanxue", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 } 192.168.98.201 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "dest": "/tmp/zutuanxue", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 }

文件的删除

将 node1(192.168.98.201) 机器的 /tmp/zutuanxue 文件删除

[root@manage01 ~]# ansible -m file 192.168.98.201 -a "path=/tmp/zutuanxue state=absent" 192.168.98.201 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "path": "/tmp/zutuanxue", "state": "absent" }

文件权限

修改 node2 机器文件 /tmp/zutuanxue:

所有者:sko

所属组:nobody

权限:600

[root@manage01 ~]# ansible -m file 192.168.98.202 -a "path=/tmp/zutuanxue owner=sko group=nobody mode=0600" 192.168.98.202 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 65534, "group": "nobody", "mode": "0600", "owner": "sko", "path": "/tmp/zutuanxue", "size": 0, "state": "file", "uid": 1001 } ### 执行前提:192.168.98.202 有 sko 用户

创建链接文件 [软连接、硬链接]

为 node2 机器的 /tmp/zutuanxue 文件创建以下链接

软连接 /tmp/zutuanxue_com

硬链接 /tmp/zutuanxue_com_cn

# 软连接 [root@manage01 ~]# ansible -m file 192.168.98.202 -a "src=/tmp/zutuanxue path=/tmp/zutuanxue_com state=link" 192.168.98.202 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "dest": "/tmp/zutuanxue_com", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 13, "src": "/tmp/zutuanxue", "state": "link", "uid": 0 } # 硬链接 [root@manage01 ~]# ansible -m file 192.168.98.202 -a "src=/tmp/zutuanxue path=/tmp/zutuanxue_com_cn state=hard" 192.168.98.202 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "dest": "/tmp/zutuanxue_com_cn", "gid": 65534, "group": "nobody", "mode": "0600", "owner": "sko", "size": 0, "src": "/tmp/zutuanxue", "state": "hard", "uid": 1001 }

创建一个目录

为所有的业务机器创建一个目录: /tmp/zutuanxue123

[root@manage01 ~]# ansible -m file group1 -a "path=/tmp/zutuanxue123 state=directory" 192.168.98.203 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/zutuanxue123", "size": 6, "state": "directory", "uid": 0 } 192.168.98.202 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/zutuanxue123", "size": 6, "state": "directory", "uid": 0 } 192.168.98.201 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/zutuanxue123", "size": 6, "state": "directory", "uid": 0 }

修改目录及子文件权限

设置业务机器的 /tmp/zutuanxue123 目录及子文件的权限

所有者设置为 sko

权限为 2775

[root@manage01 ~]# ansible -m file group1 -a "path=/tmp/zutuanxue123 owner=sko mode=2755 recurse=yes" 192.168.98.203 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 0, "group": "root", "mode": "02755", "owner": "sko", "path": "/tmp/zutuanxue123", "size": 19, "state": "directory", "uid": 1000 192.168.98.202 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 0, "group": "root", "mode": "02755", "owner": "sko", "path": "/tmp/zutuanxue123", "size": 19, "state": "directory", "uid": 1001 } 192.168.98.201 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "gid": 0, "group": "root", "mode": "02755", "owner": "sko", "path": "/tmp/zutuanxue123", "size": 19, "state": "directory", "uid": 1001 }

删除一个目录 [包括子文件全部删除]

删除所有业务机器的 /tmp/zutuanxue123 目录

[root@manage01 ~]# ansible -m file group1 -a "path=/tmp/zutuanxue123 state=absent" 192.168.98.203 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "path": "/tmp/zutuanxue123", "state": "absent" } 192.168.98.202 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "path": "/tmp/zutuanxue123", "state": "absent" } 192.168.98.201 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": true, "path": "/tmp/zutuanxue123", "state": "absent" }

二、学习视频

视频:file 模块

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-06-09发表,共计4946字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中