ansible

Ansible - How to set execute bit recursively, but just for scripts?


I have an archive file which I push to my clients using unarchive module. There is a bunch of files in multiple directories and there are some scripts that I need to run later. Is there a nice and easy method to set all scripts - a.k.a *.sh files - executable?

I can run find {{ targetdir }} -type f -name "*.sh" on the clients and register output into a variable and then use it in a loop with file module, but I hope there is a better way.

Yes I could use -exec chmod +x with the find, but I would like to see in the log if there were a change or not.


Solution

  • Q: "Is there a nice and easy method ... I would like to see in the log if there were a change or not."

    In order to have only a single tasks and not multiple different, have a shell task idempotent and supporting check_mode, one could combine different approaches. A minimal example playbook

    ---
    - hosts: localhost
      become: false
      gather_facts: false
    
      vars:
    
        targetdir: scripts
    
      tasks:
    
        - name: find exec chmod
          shell:
            cmd: find {{ targetdir }} -type f -name "*.sh" -not -executable {% if not ansible_check_mode %}-exec echo {} \; -exec chmod +x {} \;{% endif %}
          check_mode: false # to force run
          register: result
          changed_when: result.stdout_lines | length | int > 0 # files were found
    
        - debug:
            var: result
    

    will show the requested behavior.

    Since such approach will work out-of-box, it can prevent multiple tasks and operations (IOps), writing and maintaining own Custom Module, prevent complexity somewhere else, etc. ...

    Thanks To