5

there. I am new to ansible. I was trying to give some permission to multiple file using ansible. I aleardy tried follwoing code:

- hosts: all tasks: - name: Giving file permission to tomcat/bin sh file file: path=/tomcat/apache-tomcat-8.5.23/bin/*.sh owner=tomcat group=tomcat mode=755 

in the above code i want to give permission to tomcat for all .sh file located in tomcat/bin directory.I have already created a tomcat user. When i run this playbook i get this error:

 {"changed": false, "msg": "file (/tomcat/apache-tomcat-8.5.23/bin/*.sh) is absent, cannot continue", "path": "tomcat/apache-tomcat-8.5.23/bin/*.sh", "state": "absent"} 

How can i do that?

4 Answers 4

5

The file module does not support the use of wildcards in its path parameter. You can use a combination of the find and file module as described in a blog post here:

- hosts: all tasks: - name: Find files find: paths: /tomcat/apache-tomcat-8.5.23/bin patterns: "*.sh" register: files_to_change - name: Modify the file permissions file: path: "{{ item.path }}" owner: tomcat group: tomcat mode: 755 with_items: "{{ files_to_change.files }}" 
Sign up to request clarification or add additional context in comments.

Comments

3

I recently gave a relevant suggestion here.

tmp/tst: ls -ln total 8 -r----x--- 1 521 100 16 May 9 09:40 hosts -rwx---rwx 1 521 100 183 May 9 09:44 tst.yml /tmp/tst: cat hosts [tst] localhost /tmp/tst: cat tst.yml --- - name: test hosts: tst tasks: - name: tweak permissions file: dest: "{{ item }}" mode: u+rw,g+r,o-w with_fileglob: - '/tmp/tst/*' /tmp/tst: ansible-playbook -i hosts tst.yml PLAY [test] ******************************************************************** TASK [Gathering Facts] ********************************************************* ok: [localhost] TASK [tweak permissions] ******************************************************* changed: [localhost] => (item=/tmp/tst/hosts) changed: [localhost] => (item=/tmp/tst/tst.yml) PLAY RECAP ********************************************************************* localhost : ok=2 changed=1 unreachable=0 failed=0 /tmp/tst: ls -ln total 8 -rw-r-x--- 1 521 100 16 May 9 09:40 hosts -rwxr--r-x 1 521 100 183 May 9 09:44 tst.yml 

Just be careful to test it. with_fileglob has had issues, but .../*.sh might work. I'd actually recommend the methods with find/register or shell. Just adding this for options. :)

3 Comments

Comments never required for a downvote - but it will help people learn what's wrong with a post and not to do it if you use http://idownvotedbecau.se, which only takes a second if you bookmark it. Personally, I'd always like clarification, but c'est la vie.
Your suggestion works only for local machine, see stackoverflow.com/a/55622014 for details
Good point to note. c.f. docs.ansible.com/ansible/2.8/plugins/lookup/fileglob.html - remote systems require a more elaborated approach.
1

I am not sure that we can use regex in the file path. But you can achieve the same using with_items or shell module . with_items is painful one.

- file : path : "{{ item }}" owner : tomcat group : tomcat mode : 755 with_items: - /tomcat/apache-tomcat-8.5.23/bin/a.sh - /tomcat/apache-tomcat-8.5.23/bin/b.sh 

or the Shell Module

- name: Giving file permission to tomcat/bin sh file shell : | chown tomcat:tomcat /tomcat/apache-tomcat-8.5.23/bin/*.sh chmod 0755 /tomcat/apache-tomcat-8.5.23/bin/*.sh 

Comments

-4

After spending few days for searching for optimized answer i came with the following answer which is worked for me.

- file: path: '{{ item }}' owner: tomcat group: tomcat mode: 0755 with_fileglob: - "/tomcat/apache-tomcat-8.5.23/bin/*.sh" 

1 Comment

this expands locally

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.