ansible playbook 中的幂等性 [英] Idempotency in ansible playbook

查看:26
本文介绍了ansible playbook 中的幂等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ansible playbook 配置服务器.我的剧本在第一次执行时正常工作,但是当我再次运行相同的剧本时,它会在服务器上的配置文件中创建重复行.我正在使用 lineinfile 模块.每次运行剧本时,以下任务示例都会添加该行.

I am configuring a server using Ansible playbook. My playbook works properly at the very first execution but when I run the same playbook again it creates the duplicates lines in the configuration file on the server. I am using lineinfile module. Example following task adds the line each time I run the playbook.

- lineinfile: dest=/etc/pam_ldap.conf line="ssl off"

有没有办法避免这种情况,并保持幂等性.

Is there a way to avoid this, and maintain idempotency.

推荐答案

理论上 lineinfile 应该像你期望的那样工作.仅当文件中不存在一行时才添加一行.

Theoretically lineinfile should work as you expect it. A line only is added if it is not already present in the file.

文件是符号链接吗?我看不出 Ansible 不应该遵循该链接的原因,但也许这可能是它无法识别该行的原因.

Is the file a symlink? I don't see a reason why Ansible shouldn't follow that link, but maybe that might be a reason why it fails to identify the line.

您是否尝试添加 regexp 参数?无论如何,涵盖已经存在像 ssl on 这样的行的情况.

Did you try to add a regexp parameter? It would make sense anyway, to cover cases where a line like ssl on already is present.

- lineinfile: dest=/etc/pam_ldap.conf
              line="ssl off"
              regexp="^ssl\s+"

这篇关于ansible playbook 中的幂等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆