Reputation: 549
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?
Upvotes: 5
Views: 7074
Reputation: 15418
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. :)
Upvotes: 3
Reputation: 549
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"
Upvotes: -4
Reputation: 51
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 }}"
Upvotes: 5
Reputation: 415
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
Upvotes: 1