【问题标题】:Ansible - find and set permissions, including sticky bitAnsible - 查找和设置权限,包括粘性位
【发布时间】:2017-01-26 16:22:03
【问题描述】:

使用 Ansible 2.1.4.0

是否可以在 1 个任务中设置 sticky bit 和文件夹权限?

示例;

# Shell is used over find module cause symlink breaks and performance

- name: Find directories in /tmp which are not valid
  shell: find
    /tmp/test -type d
    \( ! -user root -o ! -group root -o ! -perm 775 \)
  register: find1

- name: Set 775 for found directories
  file:
    path: "{{ item }}"
    owner: root
    group: vagrant
    mode: 0775
    state: directory
  with_items: "{{ findPermission1.stdout_lines | default([]) }}"


- name: Find directories in /tmp which have no sticky bit
  shell: find
    /tmp/test -type d
    \! -perm /1000
  changed_when: false
  register: find2

- name: Set permissions for found directories
  file:
    path: "{{ item }}"
    owner: root
    group: vagrant
    mode: g+s
    state: directory
    recurse: no #cause it already found recurse
  with_items: "{{ find.stdout_lines | default([]) }}"

现在,我必须有 2 个不同的任务来设置权限。但是它们会相互覆盖。

目标:在一项任务中将权限设置为 775 和 g+s。

【问题讨论】:

    标签: permissions ansible


    【解决方案1】:

    找到了,可以用官方file module

    - name: Set sticky bit + 775 for directory
      file:
        path: /tmp/test
        owner: root
        group: vagrant
        mode: u=rwx,g=rwx,o=rx,g+s
        # mode: '02775' # also works
        # mode: ug=rwx,o=rx,g+s # also works
        state: directory
    

    【讨论】:

    • 顺便说一句,可以将它们分组:mode: ug=rwx,o=rx,g+s
    • @Kevin C,sticky bit + 775 for directory 可以用一条语句来设置:02775
    • 顺便说一句:问题询问“粘性位”,但 OP 使用符号“s”,它不是“粘性位”而是“在执行时设置用户或组 ID”。根据chmod(1) manpage,“t”代表“粘性位”。但答案很相似,只需使用 `mode: "01775" 而不是 "02775"。
    【解决方案2】:

    目标:在一项任务中设置权限为775和g+s。

    - name: Set permissions for found directories
      file:
        path: "{{ item }}"
        owner: root
        group: vagrant
        mode: 02775
        state: directory
        recurse: no #cause it already found recurse
      with_items: ____
    

    但我不明白您为什么要在代码中检查 SUID (-perm /1000) 并设置 SGID (g+s)。我也不知道find的值是多少,因为你注册了find1find2,但没有注册find

    我也认为不需要为 find 指定条件,因为 Ansible 模块是幂等/声明性的,并且您希望所有目录具有相同的权限,因此您可以依赖 Ansible。

    【讨论】:

      猜你喜欢
      • 2013-03-01
      • 2021-04-17
      • 2016-12-08
      • 2018-04-01
      • 2018-03-03
      • 2019-10-28
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多