Ansible:如何通过在主机上附加文件的任务实现幂等性(不恢复到初始状态)
我很难了解如何创建遵循文档中最佳实践的 Ansible 角色。我正在研究的以下用例是在主机上启用 Filebeat。可以通过将模块定义放置在 /etc/filebeat/modules.d
文件夹中来配置 Filebeat。
当我添加模块时它工作得很好。幂等性每次运行时都会起作用,在角色(剧本)的每次运行中都会启用一组给定的模块。
但是,当我决定不再需要某个给定模块时,我应该做什么?我将其从角色中删除,重新运行剧本,以便启用所有其他模块。 但是:之前的运行启用了一个模块,在更改后我没有直接使用角色安装该模块。因此,我的服务器状态仍然以与角色本身强加的方式不同的方式改变。
我的问题是:我应该在应用模块之前删除模块,以便我总是从新鲜状态开始吗?
例如:
- name: Remove modules
file:
dest: "/etc/filebeat/modules.d/{{ item }}"
state: absent
loop:
- "module1.yml"
- "module2.yml"
- "module3.yml" # It was being installed in previous role, but not now
- name: Enable modules via 'modules.d' directory
template:
src: "modules.d/{{ item }}"
dest: "/etc/filebeat/modules.d/{{ item }}"
mode: '0644'
loop:
- "module1.yml"
- "module2.yml"
所以我删除了 module3.yml
,因为我记得我之前安装过它,并安装了 module1.yml
和 module2.yml
。
而不是只安装我需要的东西,无论之前安装了什么:
- name: Enable modules via 'modules.d' directory
template:
src: "modules.d/{{ item }}"
dest: "/etc/filebeat/modules.d/{{ item }}"
mode: '0644'
loop:
- "module1.yml"
- "module2.yml"
给我留下 module1.yml
和 module2.yml
(所需),不幸的是:module3.yml(来自之前的角色)。
如何管理以避免此类情况?并避免将服务器视为一台大型有状态机器,即使我运行一个角色,输出也与预期不同,因为之前已经完成了一些操作,我在当前的 Ansible 角色代码中看不到。
您是否在 Ansible 工作流程中编写revert
playbook 以便在需要时恢复到初始状态?
我很好奇。预先感谢您的回复。
I am having a hard time getting to know how to create Ansible roles that are following the best practices according to documentation. The following use-case which I am looking at is e.g. enabling Filebeat on host. Filebeat can be configured by placing a module definition in /etc/filebeat/modules.d
folder.
It works fine when I am adding modules. Idempotence is working, everytime, on each run of the role (playbook), a given set of modules is enabled.
But what I should do when I decide that a given module is not longer needed? I remove it from role, rerun a playbook, so that all other modules are enabled. But: the previous run enabled a module that I am not installing directly with role after changes. So my server state is still altered in a way that is different than the role is imposing itself.
My question is: should I take care of removing modules before I apply them so I always start from, let's say, fresh state?
E.g.:
- name: Remove modules
file:
dest: "/etc/filebeat/modules.d/{{ item }}"
state: absent
loop:
- "module1.yml"
- "module2.yml"
- "module3.yml" # It was being installed in previous role, but not now
- name: Enable modules via 'modules.d' directory
template:
src: "modules.d/{{ item }}"
dest: "/etc/filebeat/modules.d/{{ item }}"
mode: '0644'
loop:
- "module1.yml"
- "module2.yml"
So I remove module3.yml
, because I remember that I've installed it before, and install module1.yml
and module2.yml
.
Instead of just installing what I need, no matter what has been installed before:
- name: Enable modules via 'modules.d' directory
template:
src: "modules.d/{{ item }}"
dest: "/etc/filebeat/modules.d/{{ item }}"
mode: '0644'
loop:
- "module1.yml"
- "module2.yml"
Leaving me with module1.yml
and module2.yml
(desired) and, unfortunately: module3.yml
(from previous role).
How to manage that to avoid such situations? And avoid treating server as one big stateful machine that even if I run a role, the output is different than desired, because something has been done before that I cannot see in current Ansible role code.
Do you code revert
playbooks in your Ansible workflow to revert to initial state when needed?
I am curious. Thanks in advance for your reply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想保证安全,请删除所有未列出的模块。
简短回答:查看该行下方示例中的“删除模块”块。
详细信息: 给定远程主机上用于测试的
目录 声明模块的路径以及要配置的模块列表
创建模块的模板
并为单个远程主机创建模块 test_11在此示例中
要删除列表中没有的所有模块 fb_modules 查找目录 fb_modules_path 中的所有文件 声明
找到的模块列表以及应删除的模块列表删除
给予
删除模块
查看结果
用于测试的完整剧本示例
Remove all modules not listed if you want to stay safe.
Short answer: Look at the block 'Remove modules' in the example below the line.
Details: Given the directory on the remote host for testing
Declare the path to the modules and the list of the modules you want to configure
Create the templates for the modules
and create the modules for a single remote host test_11 in this example
To remove all modules that are not in the list fb_modules find all files in the directory fb_modules_path
Declare the list of found modules and the list of the modules that should be removed
give
Remove the modules
Take a look at the result
Example of a complete playbook for testing
简而言之:
注意:我在示例的剧本中声明了该变量,但该变量很可能应该进入您的库存(组或主机级别),并且肯定不是在某个角色中(除了文档的默认值)
In a nutshell:
Note: I declared the variable inside the playbook for the example but that one one should most probably go inside your inventory (group or host level), and certainly not in a role (except in defaults for documentation)