gvij
gvij

Reputation: 447

Ansible: Changing permissions of files inside a directory in .yml file

Suppose there's a directory /dir/tools. tools contains a bunch of scripts, say, a.sh, b.sh, c.sh.

I need to set the permissions of a.sh, b.sh, and c.sh to 0775.

I've currently done it in the following manner:

- name: Fix 'support_tools' permissions
  file: path={{ item }} owner=abc group=abc mode=0775 
  with_items:
    - /dir/tools/a.sh
    - /dir/tools/b.sh
    - /dir/tools/c.sh

Btw, 'file: path=/dir/tools owner=abc group=abc mode=0775' sets the permission of tools directory but not of the files inside it.

Is there a better way of achieving this?

Upvotes: 10

Views: 66769

Answers (5)

Frank Autenrieth
Frank Autenrieth

Reputation: 141

This is a another way to do it, using ansible modules without "shell" or "command"

- name: lookup files with a certain pattern
  find:
    paths: /dir/tools/
    file_type: file
    patterns: "*.sh"
  register: filelist

- name: change permissions
  file:
    path: "{{ item.path }}"
    state: file
    owner: abc
    group: abc
    mode: "0775"
  with_items: "{{ filelist.files }}"

Upvotes: 9

Lakshmikandan
Lakshmikandan

Reputation: 4647

Try it with below lines with "with_items" and -maxdepth 5

 vars:
    your_path: "/home/username/support_tools"
  tasks:
    - name: Fix 'support_tools' permissions
      command: "{{ item }}"
      with_items:
        - find {{ your_path }} -maxdepth 5 -type d ! -perm 0755 -exec chmod 0755 {} \;
        - find {{ your_path }} -maxdepth 5 -type f ! -perm 0644 -exec chmod 0644 {} \;

Upvotes: 6

Roman Valchuk
Roman Valchuk

Reputation: 19

Try:

- name: Fix 'support_tools' permissions
   shell: 'chmod a+x /dir/tools/*'

Upvotes: 0

R Sac
R Sac

Reputation: 11

You can skip specifying group as well. Then check your yml file by running it with the --check parameter.

Upvotes: 1

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68289

Try:

- name: Fix 'support_tools' permissions
  file: path=/dir/tools owner=abc group=abc mode=0775 state=directory recurse=yes

Upvotes: 19

Related Questions