Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ansible "replace" regexp (one OR the other)

I’m trying to replace a value in a config file, using the replace module.
However, I was wondering if there is an OR function or similar.

Currently, I have the following play:

- name: Replace "DebugLevel" variable-value"
  become: yes
  replace:
    path: /etc/zabbix/zabbix_proxy.conf
    regexp: '^# DebugLevel=3'
    replace: 'DebugLevel=3'

This play uncomments DebugLevel=3, but when the playbook is run a second time the replace wont work because the regex does not match (value already uncommeted).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to always replace the value even if DebugLevel=3 already was uncommented.

This will make any manual changes made by a person to be overwritten and the Ansible playbook sets it back to original config.

By creating a new play with a regex that is using the value that is already uncommented, I can accomplish this, but is there a shorter version by using an "OR" after the first regex value or something similar?

Example of what I mean:

- name: Replace "DebugLevel" variable-value"
  become: yes
  replace:
    path: /etc/zabbix/zabbix_proxy.conf
    regexp: '^# DebugLevel=3' OR '^DebugLevel=.*'
    replace: 'DebugLevel=3'

>Solution :

if you want to use regex the or is |

- name: Replace "DebugLevel" variable-value"
  replace:
    path: /etc/zabbix/zabbix_proxy.conf
    regexp: '^# DebugLevel=3|^DebugLevel=.*'
    replace: 'DebugLevel=3'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading