22

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 module to add a block of lines in a file, but I want the block to be indented 6 spaces in the file. Here's the task

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

I expect

  authc:
    line0
      line1
      line2
      line3
        line4

but get

  authc:
line0
  line1
  line2
  line3
    line4

Adding spaces in the beginning of the lines does not do it. How can I accomplish this?

techraf
  • 59,327
  • 25
  • 171
  • 185
Chris F
  • 10,995
  • 23
  • 79
  • 147

3 Answers3

56

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:

Community
  • 1
  • 1
antex
  • 704
  • 7
  • 6
  • 2
    [**Currently broken**](https://groups.google.com/forum/#!topic/ansible-project/mmXvhTh6Omo) since Ansible version `2.1` and at least up to `2.3.1.0` – Serge Stroobandt Jun 15 '17 at 11:48
  • 12
    A couple things, first this should be the correct answer! Second, this is [**not broken**](https://github.com/ansible/ansible/issues/23777) but rather the interpretation of the block starts at the "Block Indentation Indicator" as the answer correctly demonstrates. Notice that the first line in the code in the answer has four spaces underneath the `block: |2` line. That is interpreted to mean the following block to insert starts after 2 spaces (on each line), which will result in the file having 2 spaces before the first line when inserted. – ddrake12 Nov 28 '17 at 18:05
  • The vertical bar is the virtual left margin for the block. The integer specifies the number of spaces to indent relative to the left margin. Simply align content under the bar, plus one space. The result will be indented by 2 or 4 or whatever value provided for the integer. – Rick O'Shea Mar 07 '22 at 23:30
4

The number after the | describes how many lines the block is indented. For example:

  block: |2
    insert some stuff
  ^^ 2 spaces here.

  block: |4
      insert some stuff
  ^^^^ 4 spaces here.

If you like to indent your line in the destination file you can use this workaround:

  block: |
    # this is a comment
      insert some stuff

In this example, the line # this is a comment will not be indented and the line insert some stuff will have 2 leading spaces.

Eddie Parker
  • 4,658
  • 3
  • 33
  • 41
Nortol
  • 371
  • 2
  • 8
  • This example fails. The VERTICAL BAR acts as the left margin for your content. Nothing to do with "block:" or anything else. If you want to indent N spaces, align content under the bar, one space to the right. Put the indentation value N to the right of the vertical bar. – Rick O'Shea Mar 07 '22 at 23:33
2

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.

Ivan Ermilov
  • 1,711
  • 1
  • 11
  • 9