简体   繁体   中英

Ansible lineinfile regexp line with spaces

I have a line in a config file, to which I would like to modify the line. I use the below code

- name: insert line
  lineinfile:
    path: /etc/config
    regexp: '^(.*)password root(.*)$'
    line: password root {{ hello }}

so in above case, when even i run the ansible task it just inserts a new line, but never modifies the line. How do i avoid the space of the string in the regexp? Ive tried "\s" and "/\s" but couldnt fix it.

the output file /etc/config looks like below for example.

password root alpha

next time I run the code it becomes

password root beta

here in my case the {{ hello }} variables keeps changing so I want to replace instead of adding new line.

The problem identified in this case is, since the output line (ie in /etc/config) starts with no spaces,

regexp: '^password root(.*)$'

the regex should be as above and no need of \s to overcome space between password and root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM