简体   繁体   English

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

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

I've been trying to finish up a playbook for deploying a new server.我一直在尝试完成部署新服务器的剧本。 I'm struggling with changing data within brackets containing quotes via lineinfile and a regex:我正在努力通过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

After the playbook is executed, the desired line:执行剧本后,所需的行:

#hosts: ["localhost:5044"]

is not updated to reflect:更新以反映:

hosts: ["elk.home:5044"]

What I'm trying to achieve is:我想要实现的是:

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

There are no errors generated.没有产生错误。 I've tried varying " and ' along with escapes \ , but I can't get the expression correct. Any suggestions would be greatly appreciated!我已经尝试过改变"'以及转义\ ,但我无法正确表达。任何建议将不胜感激!

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

I was able to reach a resolution with the following lines:我能够通过以下几行达成解决方案:

- 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

After completing the playbook, I had an issue with whitespace.完成剧本后,我遇到了空白问题。 I added two spaces that correctly modified the line我添加了两个正确修改行的空格

- 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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