Ansible – Find and Change Permissions with Ansible

ansible

I am scratching my head to why my code is not registering files in the dict

something wrong with my code in the playbook but cannot see what

  vars:
    file_vars:
     - { spath: /var/log/ctm, type: file, mode: o-w, pattern: "^.*?\\.(?:sh|fp|so|xml)$" }

  tasks:
  - name: Find files depending on REGEX pattern
    find:
     paths: "{{ item.spath }}"
     file_type: "{{ item.type }}"
     recurse: yes
     patterns: "{{ item.pattern }}"
     use_regex: yes
    with_items:
     - "{{ file_vars }}"
    register: change_mode

  - debug:
     var: change_mode

  - name: remove world writable permissions
    file:
     path:  "{{ item.path }}"
     mode:  o-w
    with_items:
     -  "{{ change_mode.files }}"

FAILED! => {"msg": "'dict object' has no attribute 'files'"}

any help greatly appreciated

Best Answer

If you read the output of your task debug you'd notice change_mode is a dictionary containing results list, and not files. That's because of the with_items loop in your Find files depending on REGEX pattern task.

Iterate over change_mode.results with subelements files:

- name: remove world writable permissions
  file:
    path:  "{{ item.1.path }}"
    mode:  o-w
  loop: "{{ change_mode.results | subelements('files') }}"

With your current data the loop in Find files depending on REGEX pattern task in unnecessary, so you can just remove it, but I assume you want file_vars to be extendible.