gpt4 book ai didi

使用文件模块幂等改变权限

转载 作者:行者123 更新时间:2023-12-05 01:17:10 24 4
gpt4 key购买 nike

我每晚都会通过 cron 作业来运行我的 ansible 剧本,我会时不时地添加它们。

我希望每个模块的输出仅在实际发生更改时才显示 changed=(num),但一些特定模块在根本未更改时显示已更改。

例如:

- name: (name)
file:
state: touch
path: (path)
group: (group)
mode: (perms)

任务运行前的路径包含与请求的权限和组相同的组和权限。所以当任务运行时它们不会改变,但结果 ansible 说它已经“改变”了。

我查看了这个问题并找到了 https://github.com/ansible/ansible/issues/21124但我正在运行 ansible 2.2.1.0。所以我的理解是他们表明文件模块是幂等的。

是否有任何使其幂等的建议?

最佳答案

为了使用 ansible 中的文件模块幂等地创建文件,您必须分别检查文件是否存在、创建它和设置任何权限(因为您希望正确设置这些文件是否已经存在) .

这可以使用以下 3 个任务来完成:

- name: Check if file exists
stat:
path: /tmp/file
register: tmp_file

- name: Create file
file:
path: /tmp/file
state: touch
when: tmp_file.stat.exists == false

- name: Set perms of file
file:
path: /tmp/file
state: file
owner: user
group: group
mode: 0600

关于使用文件模块幂等改变权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51308786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com