Home > Enterprise >  How to insert block at specific line number using ansible
How to insert block at specific line number using ansible

Time:08-18

I want to add below docker log rotation specs into daemon.json file using ansible-playbook

"log-driver": "json-file",
"log-opts": {
  "max-size": "1m",
  "max-file": "4"
}

What if daemon.json is already present on the node which I am applying the playbook to. I dont want to mess up existing configuration. How do I add above block at line no. 2 ( that is after '{' or before last line i.e. '}' ) ?

CodePudding user response:

You can use the lineinfile module

- name: Add logrotate to daemon.json
  lineinfile:
    path: "<location of the docker daemon.json>"
    insertafter: '\"log-opts\": {'         # not sure about the escaping
    line: <your custom line>


CodePudding user response:

I'd use for blocks blockinfile:

- name: Add config to daemon.json
  ansible.builtin.blockinfile:
    path: "<location of the docker daemon.json>"
    insertafter: '\"log-opts\": {' # not sure about the escaping
    block: |
      "log-driver": "json-file",
      "log-opts": {
        "max-size": "1m",
        "max-file": "4"
      }
  • Related