Ansible replace a value in big config file

1.5k Views Asked by At

I have a config file with more than 3000 lines, where i need to change/replace only few parameters. since the config file is huge. am unable to use the template.

Need help in replacing the below parameters.


  gateway-config {
    enable = true
    host-name = "car-cache"
    port = 202
    batch-size = 100
    patterns = ["^((test))"]
    type = LINE
    prefix = "stats."${auth}".service"
  }


  k9-config {
    enable = true

    send-enable = false

    host-name = ${auth}

    connection-timeout = 120000

    read-timeout = 60000

    proxy = ""

    project = "Networking"

    period = 120

I need to replace the enable = false to enable = true only on some-config but when i use replace module the whole enable = false is replaced in the config file.

2

There are 2 best solutions below

10
On BEST ANSWER

You can actually use the replace module with the after and before parameters:

- name: Replace between the expressions (requires Ansible >= 2.4)
  replace:
    path: /path/to/your/file
    after: 'gateway-config {'
    before: '}'
    regexp: '^(\s*enable = )false$'
    replace: '\g<1>true'
1
On

can you use replace module:

---
- name: Replace variable
  replace:
   path: "/etc/repli.conf"
   after: "hite-config {"
   regexp: "enable = false"
   replace: "enable = true"