管理文件中的配置块

问题描述 投票:1回答:2

我是Ansible的初学者,我正在寻找管理配置文件中的配置块。

我尝试在我的文件中不存在时添加配置块,但我找到的解决方案不能正常工作

实际上,如果块已经存在但与空间不同,例如,Ansible将在文件末尾添加块,因此将有两倍的配置块

这是我的不同文件(配置文件,剧本,......)

带配置块和附加空格的当前配置文件:

# Example of configuration file
#
############################################################
# Lorem ipsum dolor sit amet, consectetur adipiscing elit.
# Aliquam tempor tortor et sodales suscipit. 
# Nulla a turpis vel purus ultricies sodales a quis est.
# Maecenas tincidunt nunc et ex tincidunt dictum.
############################################################
# Block of conf
Here is
My Block
of conf      

我的剧本摘录:

- name: Get conf block of file
  shell: cat /my/conf/file.cfg | grep -a2 "of conf"
  register: conf_host
  failed_when: "conf_host.rc == 2" # Avoid error when grep return is empty

- name: Check conf block
  blockinfile:
    path: /my/conf/file.cfg
    marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
    block: |
      # Block of conf
      Here is
      My Block
      of conf
    when: conf_host.stdout != expected_conf
    vars:
      expected_conf: |-
        Here is
        My Block
        of conf

由于空间过大导致的结果:

# Example of configuration file
#
############################################################
# Lorem ipsum dolor sit amet, consectetur adipiscing elit.
# Aliquam tempor tortor et sodales suscipit. 
# Nulla a turpis vel purus ultricies sodales a quis est.
# Maecenas tincidunt nunc et ex tincidunt dictum.
############################################################
# Block of conf
Here is
My Block
of conf  
<!-- START ANSIBLE MANAGED BLOCK -->
# Block of conf
Here is
My Block
of conf
<!-- END ANSIBLE MANAGED BLOCK -->

预期结果:

# Example of configuration file
#
############################################################
# Lorem ipsum dolor sit amet, consectetur adipiscing elit.
# Aliquam tempor tortor et sodales suscipit. 
# Nulla a turpis vel purus ultricies sodales a quis est.
# Maecenas tincidunt nunc et ex tincidunt dictum.
############################################################
# Block of conf
Here is
My Block
of conf      

要么:

# Example of configuration file
#
############################################################
# Lorem ipsum dolor sit amet, consectetur adipiscing elit.
# Aliquam tempor tortor et sodales suscipit. 
# Nulla a turpis vel purus ultricies sodales a quis est.
# Maecenas tincidunt nunc et ex tincidunt dictum.
############################################################
<!-- START ANSIBLE MANAGED BLOCK -->
# Block of conf
Here is
My Block
of conf
<!-- END ANSIBLE MANAGED BLOCK -->      

我不知道我的请求是否可以理解,或者您是否有更好的解决方案。

谢谢

KR

ansible
2个回答
1
投票

你应该使用ansible jinja模块。 Jinja将基本上根据某些参数为您创建配置文件。

Jinja模板模块也支持循环

模板模块的用法:

template:
  src: configuration.j2
  dest: <path>/configuration

configuration.j2将保存您的所有文件内容


0
投票

而不是设置failed_when: "conf_host.rc == 2",将其更改为ignore_errors: true以继续执行ansible playbook并更​​改when条件以使conf_host.stdout == ""更新文件。

以下是修改后的代码。我在当地尝试过,它有效。

- name: Get conf block of file
  shell: cat /my/conf/file.cfg | grep -a2 "of conf"
  register: conf_host
  ignore_errors: true # Ignore when grep return is empty

- name: Check conf block
  blockinfile:
    path: /my/conf/file.cfg
    marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
    block: |
      # Block of conf
      Here is
      My Block
      of conf
    when: conf_host.stdout == ""
    vars:
      expected_conf: |-
        Here is
        My Block
        of conf
© www.soinside.com 2019 - 2024. All rights reserved.