desfocado desfocado - 1 year ago 186
YAML Question

How to escape 1 or more whitespaces in a regex in the lineinfile module of Ansible?

I am using Ansible to set some variables on a file using the

The problem I'm bumping into is how to have a regular expression that is flexible enough to have some spaces in the middle of the string. Please see below:

- name: Set DB IP in db conn file
regexp="{{ item.regexp }}"
line="{{ item.line }}"
- { regexp: "^.?dbschema_audit=",
line: "$dbschema_audit=\'{{ db_schema_audit }}\';" }
- { regexp: "^.?dbschema_audit_trail\s\*=",
line: "$dbschema_audit_trail=\'{{ db_schema_audit_trail }}\';" }

The file I'm trying to change has lines like these:

$dbschema_audit_trail ='a';

I have tried different variations of
and nothing seems to work. Can I have some wisdom from out there?

Answer Source

Ansible playbooks are in YAML format. In a double quoted scalar, character sequences starting with the \ character are escape sequences, but the YAML specification says \s is a not a valid escape sequence.

In a double quoted scalar, you would have to write


Alternatively, in a single quoted scalar, the \ character has no special meaning, so you can write

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download