功能:創建一個文件或目錄,并指定文件屬性
~~~
[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設置