【问题标题】:How to insert block at specific line number using ansible如何使用ansible在特定行号处插入块
【发布时间】:2022-08-17 23:52:24
【问题描述】:

我想使用 ansible-playbook 将以下 docker 日志轮换规范添加到 daemon.json 文件中

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

如果 daemon.json 已经存在于我正在应用剧本的节点上怎么办。我不想弄乱现有的配置。如何在第 1 行添加上面的块。 2(即在 \'{\' 之后或最后一行之前,即 \'}\' )?

    标签: docker ansible docker-daemon


    【解决方案1】:

    您可以使用lineinfile 模块

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

    【讨论】:

      【解决方案2】:

      我将用于块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"
            }
      

      【讨论】:

        猜你喜欢
        • 2011-09-26
        • 1970-01-01
        • 2013-12-14
        • 1970-01-01
        • 1970-01-01
        • 2014-02-02
        • 1970-01-01
        • 2020-03-29
        相关资源
        最近更新 更多