Vis*_*ota 6 linux ansible devops
我们的应用程序在/etc/services. 我们将services所有这些定义保存在一个方便的文件中,以便我们可以/etc/services像这样将它们通过管道传输:
cp /etc/services /etc/services.stock
cat /path/to/build/services >> /etc/services
Run Code Online (Sandbox Code Playgroud)
它可以工作,但它不是幂等的,即重复运行这些命令将导致服务文件再次附加信息。
当我研究我们的 Ansible 剧本时,我试图找出如何做到这一点。我可以这样:
- command: "cat /path/to/build/services >> /etc/services"
Run Code Online (Sandbox Code Playgroud)
但我不希望每次运行剧本时它都运行。
另一种选择是执行以下操作:
- name: add services
lineinfile:
state: present
insertafter: EOF
dest: /etc/services
line: "{{ item }}"
with_items:
- line 1
- line 2
- line 3
- line 4
- ...
Run Code Online (Sandbox Code Playgroud)
但这真的很慢,因为它单独执行每一行。
有没有更好的办法?模板没有帮助,因为它们会完全覆盖服务文件,这看起来有点粗鲁。
blockinfile是一个本机幂等模块,用于确保文件中存在(不存在)一组指定的行。
例子:
- name: add services
blockinfile:
state: present
insertafter: EOF
dest: /etc/services
marker: "<!-- add services ANSIBLE MANAGED BLOCK -->"
content: |
line 1
line 2
line 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12994 次 |
| 最近记录: |