在正则表达式中的Ansible escape *

时间:2015-09-18 15:36:31

标签: regex ansible

我尝试使用Ansible的lineinfile module来确保文件中存在以*开头的行。但是,每次任务都会失败并出现以下错误

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.

任务如下

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

鉴于典型的正则表达式的转义,我假设它会被\转义,但我只是得到了上述错误。

Ansible是1.9.2

$ ansible --version
$ ansible 1.9.2

1 个答案:

答案 0 :(得分:7)

转义(大多数)字符的一种确定方法是将其包装在字符类中:

regexp: "^[*] soft nofile"