Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use wild card while giving file permission using Ansible?

Tags:

ansible

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?

like image 853
Torikul Alam Avatar asked Oct 17 '22 18:10

Torikul Alam


1 Answers

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 }}"
like image 73
kortef Avatar answered Oct 21 '22 04:10

kortef