-1

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:

- 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"]

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!

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
stuber
  • 1
  • 3
  • 1
    I could guess (could be wrong) that you are using the `lineinfile` module? But answers cannot be based on guesses. You'll need to update the question with relevant portions of your playbook, what is happening, and what you expect. – seshadri_c Nov 23 '21 at 07:11
  • The `filebeat.yml` configuration file can contain `#hosts: ["localhost:5044"]` at many places. You could try a different `regexp` with `insertbefore` or `insertafter` options to target the exact setting. You could also consider the [replace module](https://docs.ansible.com/ansible/2.9/modules/replace_module.html). – seshadri_c Nov 24 '21 at 06:51
  • Also, `[` and `]` do have a meaning in a regex, they delimit a character class, if you want to match them as literal, you need to escape those two characters: `regexp: '#hosts: \["localhost:5044"\]'` – β.εηοιτ.βε Nov 24 '21 at 21:35

1 Answers1

0

Thanks seshadri_c and β.εηοιτ.βε!

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
stuber
  • 1
  • 3