Edw*_*vey 75 file-permissions ansible
在ansible中,我可以这样做:
file: dest=/foo/bar/somedir owner=root group=apache mode=0775 recurse=yes
Run Code Online (Sandbox Code Playgroud)
并且它递归地将该路径中的所有目录和文件的所有者、组和权限设置为 0775。但是我想将目录设置为 0775,将文件设置为 0664。有什么方法可以让 ansible 做到这一点?
小智 70
file: dest=/foo/bar/somedir owner=root group=apache mode=u=rwX,g=rX,o=rX recurse=yes
Run Code Online (Sandbox Code Playgroud)
将目录设置为 755,文件设置为 644。
Zul*_*kis 28
Ansible 文件/复制模块没有为您提供基于文件类型指定权限的粒度,因此您很可能需要通过执行以下操作来手动执行此操作:
Run Code Online (Sandbox Code Playgroud)- name: Ensure directories are 0755 command: find {{ path }} -type d -exec chmod 0755 {} \; - name: Ensure files are 0644 command: find {{ path }} -type f -exec chmod 0644 {} \;这些将具有递归的效果,
{{ path }}并将每个文件或目录的权限更改为指定的权限。
来源:https : //stackoverflow.com/a/28782805/1306186
gma*_*gin 12
如果你想在ansible中使用模块文件,你可以:
文件:dest=/foo/bar/somedir owner=root group=apache mode=0644 recurse=yes
文件:dest=/foo/bar/somedir owner=root group=apache mode=0775
使用此方法,您首先将所有文件 (recurse=yes) 设置为“644”,然后将 /foo/bar/somedir 设置为“775”。
这并不完美,因为它会在您每次播放剧本时更改您的目录权限。但至少它是幂等的,不像模块命令。
如果您不想拥有“已更改”状态,则可以使用模块 stat。它将列出 /foo/bar/somedir 中的所有文件和目录,以便您注册答案,然后仅对这些文件进行循环。
仅在需要时更改模组:
- name: make dirs 0755
command: find {{ your_path }} -type d ! -perm 0755 -exec chmod 0755 {} \;
- name: make files 0644
command: find {{ your_path }} -type f ! -perm 0644 -exec chmod 0644 {} \;
Run Code Online (Sandbox Code Playgroud)
我不确定将目录设置为 0775 ( rwxrwxr-x) 并将文件设置为 0644 ( ) 有多大意义rw-r--r--:组可写目录但不是文件?
如果您打算将 files 设置为 0664 ( rw-rw-r--) 以确保在目录可遍历时文件不可执行,那么有一个优雅的解决方案,只涉及一个chmod命令:
chmod -c -R ug=rw,o=r,a-x+X "{{top_dir}}"
Run Code Online (Sandbox Code Playgroud)
以下是它在Ansible 中的使用方法:
- name: recursive chmod example
command: |
chmod -c -R ug=rw,o=r,a-x+X "{{item}}"
register: chmod_status
changed_when: chmod_status.stdout != ""
with_items:
- "/home/user/sample/dir"
Run Code Online (Sandbox Code Playgroud)
chmod -c打印我们可以方便地用于在Ansible 中填充“已更改”状态的所有更改。我希望这是有道理的。
| 归档时间: |
|
| 查看次数: |
150433 次 |
| 最近记录: |