Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify case insensitive mode in ansible lineinfile regexp?

Tags:

regex

ansible

Simple question. I'm trying to match "UseDns", "usedns" and other variations.

- name: Disable DNS checking on login (huge speedup)
  sudo: true
  lineinfile:
    dest:   "/etc/ssh/sshd_config"
    regexp:   "^[# \t]*[Uu][Ss][Ee][Dd][Nn][Ss] "
    # how does one specify case insensitive regexp in lineinfile?
    line:   "UseDNS no"
    state:  "present"
    create:  true
    insertafter: EOF
  notify:
    - sshd restart
like image 897
JoaoCC Avatar asked Dec 18 '22 23:12

JoaoCC


1 Answers

Ansible uses Python re module. You can use inline modifiers, such as (?ism) in your pattern. Use the i for case-insensitive matching:

regexp:   "(?i)^[# \t]*usedns "

Inline modifiers apply to the part of the regular experssion to the right of the modifier, and can be disabled with a - e.g. (?-i). This can be applied to implement case-insensitivity to only a part of a regular expression.

For example, the regex (?i)use(?-i)DNS should match useDNS and UseDNS, but not useDns or USEdns.

like image 75
Mariano Avatar answered May 09 '23 14:05

Mariano