【发布时间】:2022-01-04 21:54:23
【问题描述】:
我有这个工作正常的剧本,但不是我需要的,我找不到问题。 该剧本应该允许我以递归方式更改文件系统的权限,或者更改特定文件的权限。
- name: Playbook to change file and directory permissions
hosts: '{{ target_hosts }}'
vars:
PATH: '{{ target_path }}'
PERMISSIONS: '{{ number }}'
OWNER: '{{ target_owner }}'
GROUP: '{{ target_group }}'
tasks:
- name: Checking that it is not a system mount point
fail:
msg: "Changing permissions on system fs is not allowed"
when: PATH in ["/etc", "/var", "/tmp", "/usr", "/", "/opt", "/home", "/boot"]
- name: Checking if the path is a file or a filesystem
stat:
path: '{{ PATH }}'
register: path_status
- name: Applying permissions on the filesystem
block:
- name: Report if directory exists
debug:
msg: "Directory {{ PATH }} is present on the server"
when: path_status.stat.exists
- name: Applying permissions recursively
file:
path: '{{ PATH }}'
mode: '0{{ PERMISSIONS }}'
owner: '{{ OWNER }}'
group: '{{ GROUP }}'
recurse: yes
when: path_status.stat.isdir is defined and path_status.stat.isdir
- name: Applying permissions on the file
block:
- name: Report if file exists
debug:
msg: "File {{ PATH }} is present on the server"
when: path_status.stat.exists
- name: Applying permissions
file:
path: '{{ PATH }}'
state: file
mode: '0{{ PERMISSIONS }}'
owner: '{{ OWNER }}'
group: '{{ GROUP }}'
when: path_status.stat.isreg is defined and path_status.stat.isreg
前 2 个任务
- 验证它不是系统文件系统
- 使用 Ansible stat 模块,我注册了正在运行的路径 作为 PATH 变量的参数传递
当我执行时只传递一个文件系统,如下例所示
ansible-playbook change_fs_permissions.yml -e "target_hosts=centoslabs target_path=/etc number=755 target_owner=root target_group=testing"
执行结束,因为它是一个系统挂载点。 (我期待什么)
但是,如果我输入类似 /tmp/somefile.txt 之类的内容作为 PATH 变量的参数,我的想法是 playbook 将再次失败,因为它无法更改该文件系统中的任何内容,但确实如此不继续执行并更改权限。
他们会看到我使用 BLOCK 模块,因为在我看来它是最好的,因此如果将文件系统传递给它,它会执行这些任务,如果它是一个文件,它会执行其他任务。
你能给我一些关于如何解决这个问题的想法吗?
【问题讨论】:
-
您正在检查两个块上的
path_status.stat.*isdir*...第二个应该是isfile,不是吗? -
这是真的,我没有意识到这一点。我改成了
path_status.stat.isreg,因为文档说要表明它是否是一个常规文件,我应该把isreg
标签: linux automation ansible redhat