企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
功能:创建一个文件或目录,并指定文件属性 ~~~ [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" } ~~~