Ansible 剧本 - 正则表达式 |无法更改括号和引号中的数据

Ansible playbook - regexp | unable to change data in brackets and quotes

我一直在努力完成部署新服务器的剧本。我正在努力通过 lineinfile 和正则表达式更改包含引号的括号内的数据:

- name: "Configuring: filebeat agent - configuring output to logstash"
  lineinfile:
    dest: "/etc/filebeat/filebeat.yml"
    regexp: '#hosts: ["localhost:5044"]'
    line: 'hosts: ["elk.home:5044"]'
  tags: application

剧本执行后,需要的行:

#hosts: ["localhost:5044"]

更新以反映:

hosts: ["elk.home:5044"]

我想要实现的是:

#hosts: ["localhost:5044"] 替换为 hosts: ["elk.home:5044"]

没有生成错误。我试过改变 "' 以及转义符 \,但我无法正确表达。任何建议将不胜感激!

感谢 seshadri_c 和 β.εηοιτ.βε!

我能够通过以下几行达成解决方案:

- name: "Configuring: filebeat agent - enabling logstash output hosts"
  lineinfile:
    dest: "/etc/filebeat/filebeat.yml"
    regexp: '#hosts: \["localhost:5044"\]'
    line: 'hosts: ["elk.home:5044"]'
  tags: 
    - configuration
    - application
    - filebeat

完成剧本后,我遇到了空格问题。我添加了两个正确修改行的空格

- name: "Configuring: filebeat agent - enabling logstash output hosts"
  lineinfile:
    dest: "/etc/filebeat/filebeat.yml"
    regexp: '#hosts: \["localhost:5044"\]'
    line: '  hosts: ["elk.home:5044"]'
  tags: 
    - configuration
    - application
    - filebeat