ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
一)第一部分查看file模块支持哪些方法 [admin@master salt]$ sudo salt 'node2.51yuki.cn' sys.list_state_functions file node2.51yuki.cn: - file.absent - file.accumulated - file.append - file.blockreplace - file.comment - file.copy - file.directory - file.exists - file.managed - file.missing - file.mknod - file.mod_run_check_cmd - file.patch - file.prepend - file.recurse - file.rename - file.replace - file.serialize - file.symlink - file.touch - file.uncomment 二)常用的方法 1)file.managed 文件管理 常见参数: * name: 节点上需要管理的文件 * source: 节点的源文件(源文件可以有多个),例如 file_override_example: file.managed: - source: - salt://file_that_does_not_exist - salt://file_that_exists * user: 节点上文件属主 * group: 节点上文件属组 * mode: 节点上文件权限 * template: 支持jinja * context:覆盖文件上下文参数 * backup: minion 会备份到minion下的/var/cache/salt/minion/backup_files目录下 例如: 案例1: [admin@master init]$ sudo vim dns.sls /etc/resolv.conf: file.managed: - source: salt://init/files/resolv.conf - user: root - group: root - mode: 644 - backup: minion - template: jinja ~ 2)file.append 文件内容的追加 常用参数: * text 要追加的内容 (支持填写多个内容) 案例2: /etc/motd: file.append: - text: - Trust no one unless you have eaten much salt with him. - "Salt is born of the purest of parents: the sun and the sea." 3) file.directory 功能:目录管理 常用参数: name: 目录的位置(要用绝对路径,也可以省略) user: 目录的属主 group: 目录的属组 recurse:递归地执行用户/组所有权和目录模式 dir_mode: 目录权限 file_mode: 文件权限 makedirs: 如果父目录不存在,则创建父目录 案例1: /test/als: file.directory: - user: admin - group: admin - dir_mode: 755 - file_mode: 644 - makedirs: True - recurse: - user - group - mode 4)file.absent 功能:删除文件或目录 案例: [admin@master study]$ vim absentfile.sls /test/als: file.absent: - name: /test/als ~~~ [admin@master study]$ sudo salt 'node2*' state.sls study.absentfile node2.51yuki.cn: ---------- ID: /test/als Function: file.absent Result: True Comment: Removed directory /test/als Started: 13:13:57.275015 Duration: 8.979 ms Changes: ---------- removed: /test/als Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1 ~~~