Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible escape * in regex

Tags:

regex

ansible

I'm trying to use Ansible's lineinfile module to ensure that a line beginning with a * is present in a file. However, the task fails every time with the following error

TASK: [nginx | Persist soft ulimit] *******************************************
failed: [aa.bb.cc.dd] => {"failed": true, "parsed": false}
BECOME-SUCCESS-xbfifannrufpkcmmfmtdigorggzvhzsx
Traceback (most recent call last):
  File "/home/ubuntu/.ansible/tmp/ansible-tmp-1442590356.11-253796220854159/lineinfile", line 2210, in <module>
    main()
  File "/home/ubuntu/.ansible/tmp/ansible-tmp-1442590356.11-253796220854159/lineinfile", line 394, in main
    ins_aft, ins_bef, create, backup, backrefs)
  File "/home/ubuntu/.ansible/tmp/ansible-tmp-1442590356.11-253796220854159/lineinfile", line 208, in present
    mre = re.compile(regexp)
  File "/usr/lib/python2.7/re.py", line 190, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.7/re.py", line 244, in _compile
    raise error, v # invalid expression
sre_constants.error: nothing to repeat
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /Users/xyz/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: auto-mux: Trying existing master
debug1: mux_client_request_session: master session id: 2
Shared connection to aa.bb.cc.dd closed.

The task is as follows

- name: Persist soft ulimit
  lineinfile:
    dest: /etc/security/limits.conf
    regexp: "^\* soft nofile"
    line: "* soft nofile 9000" 

Given typical escaping of regex, I'd assumed that it'd be escaped with \, but I just get the above error.

Ansible is 1.9.2

$ ansible --version
$ ansible 1.9.2
like image 933
Ben Swinburne Avatar asked Sep 18 '15 15:09

Ben Swinburne


People also ask

How do you escape the special characters in Ansible?

Yes, you need to escape '$' sign with '\', and it's executed in the server, it just won't show in the resulting output. Because in some cases like 'awk' with 'print' not working with Ansible ad-hoc command and need to utilize playbooks. It will spit out the result you want.

What is regex escape?

Regex. Escape is there to "escape" a string that may contain characters that have special meaning in a Regex. For example (a simple example): Let's say I wanted to search a string based on user input. One would assume I could write a regex like ".


1 Answers

One sure-fire way of escaping (most) characters is to wrap it in a character class:

regexp: "^[*] soft nofile"
like image 67
Bohemian Avatar answered Nov 08 '22 06:11

Bohemian