在ansible中找到的目录上添加粘性位

  • 本文关键字:添加 ansible ansible
  • 更新时间 :
  • 英文 :

---
- name: world writable directories
hosts: all
become: true
tasks:
- name: sction 3.5.7
command: find / -perm -0002 -a ! -perm 1000 -type d -print
register: output
- debug: var=output.stdout_lines

使用上面的代码,我在输出中得到了一些目录。我需要为所有找到的目录设置粘性位。有人能帮我做到这一点吗?

加起来@p。。。答:使用模块并没有与find命令等效的ansible,所以我知道使用shell/command没有解决方法。

同时,您可以使用file模块设置粘性位。您还应该确保command任务不会返回changed状态,因为它只查询信息。

以下是一个完整的说明手册:

---
- name: World writable directories
hosts: all
become: true
tasks:
- name: Find world writable directories
command: find / -perm -0002 -a ! -perm 1000 -type d -print
register: output
changed_when: false
- name: Set sticky bit on found directories
file:
path: "{{ item }}"
mode: "+t"
loop: "{{ output.stdout_lines }}"

下面的任务将打印向全局可写目录添加粘性位。这只是原始find命令的扩展,使用exec{}(查找输出的占位符(上执行chmod命令

- name: "Find and add sticky bit to world writable directories"
shell: find / -perm -0002 -a ! -perm 1000 -type d -print -exec chmod  +t {} + ||true
register: output

- debug: var=output.stdout_lines

如果您不想在单个任务中进行打印和粘性位添加,您可以使用:

- name: "Find the world writable directories"
shell: find / -perm -0002 -a ! -perm 1000 -type d -print ||true
register: output
- name: "Add sticky bit to world writable directories"
shell: chmod +t "{{ item }}"
loop: "{{ output.stdout_lines }}"

最新更新