如何在使用Ansible授予文件权限时使用通配符?

如何在使用Ansible授予文件权限时使用通配符?,ansible,Ansible,在那里。我不熟悉ansible。我试图使用ansible为多个文件授予一些权限。我尝试了以下代码: - 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 在上面的代码中,我想对tomcat/bin目录中的所有.sh

在那里。我不熟悉ansible。我试图使用ansible为多个文件授予一些权限。我尝试了以下代码:

- 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
在上面的代码中,我想对tomcat/bin目录中的所有.sh文件授予tomcat权限。我已经创建了一个tomcat用户。当我运行此剧本时,会出现以下错误:

 {"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"}

我如何才能做到这一点?

我不确定我们是否可以在文件路径中使用regex。但是,您可以使用与_itemsshell模块实现相同的功能带着_物品是一件痛苦的事情

- 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
外壳模块

- 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

文件模块不支持在其
路径
参数中使用通配符。 您可以结合使用查找文件模块,如博客文章中所述:


我最近提出了一个相关的建议


小心测试一下<带有_fileglob的code>出现了问题,但是
../*.sh
可能会起作用。实际上,我推荐使用find/register或shell的方法。仅为选项添加此选项。:)

在花了几天时间搜索优化的答案后,我得到了以下对我有用的答案

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

这扩展了否决票从不需要的本地内容,但它将帮助人们了解帖子的错误,并且在使用时不要这样做,如果你将其添加到书签中,这只需要一秒钟。就我个人而言,我一直希望得到澄清,但这是人生。你的建议只适用于本地机器,详情请参阅。c、 f.-远程系统需要更详细的方法。
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
- file:
      path: '{{ item }}'
      owner: tomcat
      group: tomcat
      mode: 0755
    with_fileglob:
      - "/tomcat/apache-tomcat-8.5.23/bin/*.sh"