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

Tor*_*lam 5 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
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我想向 tomcat 授予位于 tomcat/bin 目录中的所有 .sh 文件的权限。我已经创建了一个 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"}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

kor*_*tef 5

文件模块不支持在其参数中使用通配符您可以使用findfilepath模块的组合,如博客文章中所述

- 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 }}"
Run Code Online (Sandbox Code Playgroud)


Pau*_*ges 3

我最近在这里给出了相关建议。

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
Run Code Online (Sandbox Code Playgroud)

只是要小心测试一下。with_fileglob有问题,但.../*.sh可能有效。我实际上推荐使用 find/register 或 shell 的方法。只需添加此选项即可。:)

  • 您的建议仅适用于本地计算机,请参阅 /sf/answers/3893541011/ 了解详细信息 (2认同)