功能:创建一个文件或目录,并指定文件属性
~~~
[admin@node1 tmp]$ ansible-doc -s file
模块选项
force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group:定义文件/目录的属组
mode:定义文件/目录的权限
owner:定义文件/目录的属主
path:必选项,定义文件/目录的路径
recurse:递归的设置文件的属性,只对目录有效
src:要被链接的源文件的路径,只应用于state=link的情况
dest:被链接到的路径,只应用于state=link的情况
state:
directory:如果目录不存在,创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件
~~~
案例1:
* 创建一个文件,并指定权限位640,属主为admin,属组为admin
~~~
admin@node1 ~]$ ansible webserver -m file -a "state=touch path='/tmp/bb.txt' owner='admin' group='admin' mode='640'" -b --ask-sudo-pass
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password:
192.168.20.138 | SUCCESS => {
"changed": true,
"dest": "/tmp/bb.txt",
"gid": 1010,
"group": "admin",
"mode": "0640",
"owner": "admin",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 0,
"state": "file",
"uid": 1010
}
192.168.20.137 | SUCCESS => {
"changed": true,
"dest": "/tmp/bb.txt",
"gid": 1010,
"group": "admin",
"mode": "0640",
"owner": "admin",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 0,
"state": "file",
"uid": 1010
}
[admin@node1 ~]$ ansible webserver -a "ls -l /tmp/bb.txt"
192.168.20.138 | SUCCESS | rc=0 >>
-rw-r-----. 1 admin admin 0 Mar 17 16:09 /tmp/bb.txt
192.168.20.137 | SUCCESS | rc=0 >>
-rw-r-----. 1 admin admin 0 Mar 17 16:09 /tmp/bb.txt
~~~
* 创建软链接文件
~~~
[admin@node1 ~]$ ansible webserver -m file -a "path=/tmp/fstab.symlink state=link src=/tmp/fstab" -b --ask-sudo-pass
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password:
192.168.20.138 | SUCCESS => {
"changed": true,
"dest": "/tmp/fstab.symlink",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 10,
"src": "/tmp/fstab",
"state": "link",
"uid": 0
}
192.168.20.137 | SUCCESS => {
"changed": true,
"dest": "/tmp/fstab.symlink",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 10,
"src": "/tmp/fstab",
"state": "link",
"uid": 0
}
[admin@node1 ~]$ ansible webserver -a "ls -l /tmp/fstab.symlink"
192.168.20.138 | SUCCESS | rc=0 >>
lrwxrwxrwx. 1 root root 10 Mar 17 16:11 /tmp/fstab.symlink -> /tmp/fstab
192.168.20.137 | SUCCESS | rc=0 >>
lrwxrwxrwx. 1 root root 10 Mar 17 16:11 /tmp/fstab.symlink -> /tmp/fstab
~~~
* 创建目录
[admin@node1 ~]$ ansible webserver -m file -a "state=directory path=/web " -b --ask-sudo-pass
* 删除文件
~~~
[admin@node1 ~]$ ansible webserver -m file -a "path=/tmp/bb.txt state=absent" -b --ask-sudo-pass
[DEPRECATION WARNING]: The sudo command line option has been deprecated in favor of the "become" command line arguments. This feature will be removed in version 2.6. Deprecation warnings can be disabled by
setting deprecation_warnings=False in ansible.cfg.
SUDO password:
192.168.20.138 | SUCCESS => {
"changed": true,
"path": "/tmp/bb.txt",
"state": "absent"
}
192.168.20.137 | SUCCESS => {
"changed": true,
"path": "/tmp/bb.txt",
"state": "absent"
}
~~~
- 第一章:Ansible基础入门
- 第二章:Ansible系列手册
- 第一节:Ansible系列之主机清单
- 第二节:Ansible系列之变量
- 第三节:Ansible系列之YAML
- 第四节:Ansible系列之条件判断
- 第五节:Ansible系列之循环
- 第六节: Ansible系列之tags
- 第七节:Ansible系列之Jinja2
- 第三章:Ansible系列之模块
- 第一节:user模块
- 第二节:group模块
- 第三节:cron模块
- 第四节:copy模块
- 第五节: file模块
- 第六节:yum模块
- 第七节:service模块
- 第八节:shell模块
- 第九节:script模块
- 第十节:setup模块
- 第十一节:filesystem和mount模块
- 第十二节:synchronize模块
- 第十三节: get_url模块
- 第十四节: package模块
- 第十五节:stat模块
- 第十六节:unarchive模块
- 第十七节: commang模块
- 第四章:Ansible-playbook介绍
- 第五章:Ansible系统环境
- 第一节:Ansible Role 系统环境之epel设置