Chris F
Chris F

Reputation: 16695

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

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?

Upvotes: 35

Views: 35364

Answers (5)

stackprotector
stackprotector

Reputation: 13451

You can use the Jinja2 indent filter to add your desired indentation:

- name: Added a block of lines in the file
  blockinfile:
    dest: /path/some_file.yml
    insertafter: 'authc:'
    block: |
      {% filter indent(width=4, first=true) %}
      line0
        line1
        line2
        line3
          line4
      {% endfilter %}

Result:

  authc:
    line0
      line1
      line2
      line3
        line4

Upvotes: 16

Cristiano Rosa
Cristiano Rosa

Reputation: 91

If you want to insert multiple blocks in the same file by using blockinfile and also want to keep the leading spaces before each line you are going to add, you could do something like this with each block having a different marker:

- name: Name 1
  blockinfile:
    path: /folder/file.yaml
    insertafter: 'insert after this text'
    block: |
      # Adding parameters:
          line 1
          line 2
    marker: "# BLOCK 1"

- name: Name 2
  blockinfile:
    path: /folder/file.yaml
    insertbefore: "insert before this text"
    block: |
      # Adding parameters:
          line 3
            line 4
            line 5
    marker: "# BLOCK 2"

- name: Name 3
  blockinfile:
    path: /folder/file.yaml
    insertbefore: "insert before this text"
    block: |
      # Adding parameters:
        line 6
            line 7
            line 8
          line 9
    marker: "# BLOCK 3"

Upvotes: 0

antex
antex

Reputation: 854

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:

Update:

As Dave correctly pointed out, this does not work anymore in the current version 2.14.2 of ansible :( I'd suggest using the comment workaround from the next answer

Upvotes: 70

Nortol
Nortol

Reputation: 449

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.

Upvotes: 10

Ivan Ermilov
Ivan Ermilov

Reputation: 1829

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.

Upvotes: 2

Related Questions