假设有一个目录/dir/tools。工具包含一堆脚本,例如a.sh、b.sh、c.sh。
我需要将a.sh、b.sh和c.sh的权限设置为0775。
我目前以以下方式完成:
- name: Fix 'support_tools' permissions
file: path={{ item }} owner=abc group=abc mode=0775
with_items:
- /dir/tools/a.sh
- /dir/tools/b.sh
- /dir/tools/c.sh
顺便说一句,"file:path=/dir/tools-owner=abc group=abc mode=0775"设置工具目录的权限,但不设置其中文件的权限。
有没有更好的方法来实现这一点?
尝试:
- name: Fix 'support_tools' permissions
file: path=/dir/tools owner=abc group=abc mode=0775 state=directory recurse=yes
这是另一种方法,使用不带"shell"或"command"的ansible模块
- name: lookup files with a certain pattern
find:
paths: /dir/tools/
file_type: file
patterns: "*.sh"
register: filelist
- name: change permissions
file:
path: "{{ item.path }}"
state: file
owner: abc
group: abc
mode: "0775"
with_items: "{{ filelist.files }}"
用下面带有"with_items"和-maxdepth 5 的行试试
vars:
your_path: "/home/username/support_tools"
tasks:
- name: Fix 'support_tools' permissions
command: "{{ item }}"
with_items:
- find {{ your_path }} -maxdepth 5 -type d ! -perm 0755 -exec chmod 0755 {} ;
- find {{ your_path }} -maxdepth 5 -type f ! -perm 0644 -exec chmod 0644 {} ;
您也可以跳过指定组。然后通过使用--check参数运行yml文件来检查它。
尝试:
- name: Fix 'support_tools' permissions
shell: 'chmod a+x /dir/tools/*'