Reputation: 11
I'm a beginner in Ansible and I'm looking to manage configuration blocks in my configuration files.
I try to add a configuration block when it is not present in my file but the solution I found does not work as I would like
Indeed, if the block is already present but is different from a space for example, Ansible will add the block at the end of the file so there will be twice the configuration block
Here are my different files (configuration file, playbook,...)
current configuration file with configuration block and additional spaces:
# 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
extract of my playbook:
- 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
result due to excess space:
# 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 -->
expected result:
# 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
or:
# 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 -->
I don't know if my request is understandable or if you have a better solution.
Thanks
KR
Upvotes: 1
Views: 452
Reputation: 2823
You should use ansible jinja module. Jinja will basically create a configuration file for you based on certain parameters.
Jinja template module supports loops as well
Usage of template module:
template:
src: configuration.j2
dest: <path>/configuration
configuration.j2 will hold all your file content
Upvotes: 1
Reputation: 674
Instead of setting failed_when: "conf_host.rc == 2"
, change it to ignore_errors: true
to continue executing ansible playbook and change the when condition to have conf_host.stdout == ""
to update the file.
Below is the modified code. I have tried it locally and it works.
- 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
Upvotes: 0