在目录结构上递归更改权限需要太多时间

问题描述 投票:0回答:1

我想在目录树中递归地更改文件的权限。文件数量有数千个。

我现在在我的剧本上做了以下事情

- name: find all the files inside the wordpress directory
  find:
    path: /home/vagrant/wordpress
    file_type: file
    recurse: true 
  register: files_found 

- name: Ensure permissions on folders are in 644
  file:
    path: "{{ item['path'] }}" 
    mode: '0644'   
  loop:   "{{ files_found['files'] }}"

由于目录结构中存在大量文件,这需要“无限”的时间。

  • 有没有更好的选择,特别是在执行速度方面?
  • 执行
    command
    find
    命令的 Ansible
    exec
    模块真的是最好的选择吗?
ansible
1个回答
0
投票

由于具有

file
模块和
loop

的解决方案

不可行,因为

目前只剩下一个使用

command
模块,就像最小的例子中那样

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Recursively ensure file permissions in a directory
    ansible.builtin.command:
      cmd: find {{ path }} -type f -printf "%p\n" -exec chmod {{ mode }} {} \;
    vars:
      path: /home/ansible_user/test
      mode: '0644'
    register: files

  - debug:
      var: files.stdout_lines

  • 用 Bash 或 Python 编写自定义脚本或自定义模块
© www.soinside.com 2019 - 2024. All rights reserved.