Regex Ansible lineinfile模块删除文件开头的空间

Regex Ansible lineinfile模块删除文件开头的空间,regex,ansible,Regex,Ansible,我在这里发现了许多关于lineinfle模块的类似问题,但没有一个能解决我的问题。 我正在尝试将elasticsearch主机添加到metricbeatYAML配置文件中 该文件包含以下部分: # Configure what output to use when sending the data collected by the beat. #-------------------------- Elasticsearch output ------------------------

我在这里发现了许多关于
lineinfle
模块的类似问题,但没有一个能解决我的问题。 我正在尝试将elasticsearch主机添加到
metricbeat
YAML配置文件中

该文件包含以下部分:

 # Configure what output to use when sending the data collected by the beat.
 
 #-------------------------- Elasticsearch output ------------------------------
 output.elasticsearch:
   # Array of hosts to connect to.
 
   hosts: ["**ELASTICHOSTS**"]
我为插入主机ip而编写的ansible代码语句是:

 - name: Customize  metricbeat.yml hosts for PROD
   lineinfile:
     path: /etc/metricbeat/metricbeat.yml
     regexp: 'hosts:'
     line: "hosts: Db.es-dev.com:9200"
我希望该文件呈现如下所示:

 # Configure what output to use when sending the data collected by the beat.
 
 #-------------------------- Elasticsearch output ------------------------------
 output.elasticsearch:
   # Array of hosts to connect to.
 
   hosts: Db.es-dev.com:9200
 # Configure what output to use when sending the data collected by the beat.
 
 #-------------------------- Elasticsearch output ------------------------------
 output.elasticsearch:
   # Array of hosts to connect to.
 
 hosts: ["**ELASTICHOSTS**"]
但是上面的代码删除了hosts之前的空格,并呈现如下:

 # Configure what output to use when sending the data collected by the beat.
 
 #-------------------------- Elasticsearch output ------------------------------
 output.elasticsearch:
   # Array of hosts to connect to.
 
   hosts: Db.es-dev.com:9200
 # Configure what output to use when sending the data collected by the beat.
 
 #-------------------------- Elasticsearch output ------------------------------
 output.elasticsearch:
   # Array of hosts to connect to.
 
 hosts: ["**ELASTICHOSTS**"]
即使使用
regexp:'\s*hosts:'
regexp:'^\s*hosts:'
也没有得到预期的结果

要保留我使用以下代码在线路上播放的尾随空格:

 - name: Customize  metricbeat.yml hosts for PROD
   lineinfile:
     path: /etc/metricbeat/metricbeat.yml
     regexp: 'hosts:'
     line: "  hosts: Db.es-dev.com:9200"
是否有更好和/或更优雅的方法使用
regexp
属性将这些尾随空格保留在文件的开头,而不是像我上面那样强制使用
属性中的空格?

使用

 - name: Customize  metricbeat.yml hosts for PROD
   replace:
     path: /etc/metricbeat/metricbeat.yml
     regexp: '^(\s*)hosts:.*'
     replace:'\1hosts: Db.es-dev.com:9200'
     backrefs: yes

表达解释

--------------------------------------------------------------------------------
^字符串的开头
--------------------------------------------------------------------------------
(组和捕获到\1:
--------------------------------------------------------------------------------
\s*空格(\n、\r、\t、\f和“”)(0
或更多次(与最大金额匹配)
(可能的)
--------------------------------------------------------------------------------
)结束\1
--------------------------------------------------------------------------------
主持人:“主持人:”
--------------------------------------------------------------------------------
.*除\n(0次或多次)以外的任何字符
(匹配尽可能多的金额)

再解释一下:
regexp
参数只告诉lineinfle要替换哪一行。然后用
参数的内容替换整行。
backrefs
参数也存在于lineinfle模块中,因此您也可以使用它,如答案所示。