Ansible "replace" regexp (one OR the other)

Viewed 54

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).

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'
2 Answers

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'

If the configuration file should contain every time a certain debug level you could probably declare just that the line exists independent of comment and value. To do so

- name: Replace "DebugLevel" variable-value"
  lineinfile:
    path: /etc/zabbix/zabbix_proxy.conf
    regexp: 'DebugLevel'
    line: 'DebugLevel=3'

It will make sure that the debug level is set with the given value and active.

Documentation

Related