How to add spaces at beginning of block in Ansible's blockinfile?

后端 未结 2 1798
情话喂你
情话喂你 2021-02-12 17:36

I found this blockinfile issue, where a user suggested adding a number after the \"|\" in the \"block: |\" line, but gives a syntax error. Basically, I want to use blockinfile m

相关标签:
2条回答
  • 2021-02-12 18:20

    I was trying to use YAML feature called "Block Indentation Indicator" from the other answer, but it did not work for me (ansible 2.9.10.post0). Ugly, but working solution is:

    - name: docker-compose.yml - service has links to another container
      lineinfile:
        path: "/path/to/docker-compose.yml"
        insertafter: "service:"
        line: "    links:\n      - apache2:proxy.example.com\n      - apache2:proxy2.example.com"
    

    You basically need to put as many spaces as necessary before elements. And use \n for newlines.

    0 讨论(0)
  • 2021-02-12 18:23

    You can use a YAML feature called "Block Indentation Indicator":

    - name: Added a block of lines in the file
      blockinfile:
      dest: /path/some_file.yml
      insertafter: 'authc:'
      block: |2
          line0
            line1
            line2
            line3
              line4
    

    It's all about the 2 after the |

    References:

    • https://groups.google.com/forum/#!topic/ansible-project/mmXvhTh6Omo
    • In YAML, how do I break a string over multiple lines?
    • http://www.yaml.org/spec/1.2/spec.html#id2793979
    0 讨论(0)
提交回复
热议问题