使用 Ansible 查找模块根据权限进行搜索

Using the Ansible find module to search based on permissions

如何在 Ansible 中完成此操作?

find . -name "*.sh" -type f  ! -perm 754

因此,使用 Ansible 查找模块根据权限进行搜索。

您不能将其烘焙到 find 模块本身。

但是您可以获得所有与您的模式匹配的文件,然后根据您想要的权限过滤结果。

因此,这将是一个包含两个任务的解决方案:

- find:
    paths: /usr/local/ansible
    patterns: '*.sh'
  register: find

- debug:
    var: find.files | selectattr('mode', '!=', '0754')

给定剧本(与 shell 个任务进行比较):

- hosts: localhost
  gather_facts: no

  tasks:
    - find:
        paths: /usr/local/ansible
        patterns: '*.sh'
      register: find

    - shell: find /usr/local/ansible/*.sh  -type f
      register: shell
    - name: All shell files via shell
      debug:
        var: shell.stdout
    - name: All shell files via find
      debug:
        var: find.files | map(attribute="path")

    - shell: find /usr/local/ansible/*.sh  -type f  ! -perm 754
      register: shell
    - name: All shell files having 754 perm via shell
      debug:
        var: shell.stdout
    - name: All shell files having 754 perm via find
      debug:
        var: >
          find.files
          | selectattr('mode', '!=', '0754')
          | map(attribute="path")

这产生:

TASK [shell] **************************************************************
changed: [localhost]

TASK [All shell files via shell] ******************************************
ok: [localhost] => 
  shell.stdout: |-
    /usr/local/ansible/a.sh
    /usr/local/ansible/b.sh

TASK [All shell files via find] *******************************************
ok: [localhost] => 
  find.files | map(attribute="path"):
  - /usr/local/ansible/a.sh
  - /usr/local/ansible/b.sh

TASK [shell] **************************************************************
changed: [localhost]

TASK [All shell files having 754 perm via shell] **************************
ok: [localhost] => 
  shell.stdout: /usr/local/ansible/a.sh

TASK [All shell files having 754 perm via find] ***************************
ok: [localhost] => 
  find.files | selectattr('mode', '!=', '0754') | map(attribute="path"):
  - /usr/local/ansible/a.sh