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

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
  • How do I break a string in YAML over multiple lines?
  • http://www.yaml.org/spec/1.2/spec.html#id2793979

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)