管理文件中的配置块

时间:2019-02-14 14:22:09

标签: ansible

我是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

2 个答案:

答案 0 :(得分:1)

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

Jinja模板模块也支持循环

模板模块的用法:

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

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

答案 1 :(得分:0)

与其将failed_when: "conf_host.rc == 2"设置为ignore_errors: true,而不是将其设置为conf_host.stdout == "",以继续执行ansible剧本,并且将when条件改为- 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 ,以更新文件。

下面是修改后的代码。我已经在本地尝试过,并且可以正常工作。

any()