ansible-role-blockinfile icon indicating copy to clipboard operation
ansible-role-blockinfile copied to clipboard

Spacing in blocks does not work as expected

Open dobber opened this issue 9 years ago • 10 comments

- name: Add logstash-forwarder to datadog process monitoring
  blockinfile:
    dest: /etc/dd-agent/conf.d/process.yaml
    block: |
      - name: logstash-forwarder
        search_string: ['logstash-forwarder']
        exact_match: false
        thresholds:
          # critical if no server.py is running
          critical: [1, 1]

this code for example will be aligned to the first line in the block. this means that the result in the yaml would be:

- name: logstash-forwarder
  search_string: ['logstash-forwarder']

notice that spacing. the - name is at the beginning of the line and search_string is two spaces behind.

This breaks the yaml format and should be handled.

dobber avatar Sep 29 '15 20:09 dobber

http://pastie.org/10450801 with correct spaces

dobber avatar Sep 29 '15 20:09 dobber

You might want to use Block Indentation Indicator in YAML.

- name: Add logstash-forwarder to datadog process monitoring
  blockinfile:
    dest: /etc/dd-agent/conf.d/process.yaml
    block: |2
        - name: logstash-forwarder
          search_string: ['logstash-forwarder']
        ...

Notice that the block content has indentation with 4 spaces. You can get 2 space indented block from this task.

yaegashi avatar Oct 01 '15 01:10 yaegashi

@yaegashi that is exactly what I want, but ansible 2.0.2 complains about the

block: |2

It's fine if I remove the number 2.

kbroughton avatar May 05 '16 16:05 kbroughton

Still broken in ansible 2.1.1.0, that is the number in the block: | causes a syntax error.

cfoutstd avatar Sep 27 '16 18:09 cfoutstd

It really does seem like the Block Indentation Indicator should work. I was not having any luck with it either unfortunately. It worked with one space, but anything else threw an error. I have resorted to putting the YAML all on one line:

- name: Add logstash-forwarder to datadog process monitoring
  blockinfile:
    dest: /etc/dd-agent/conf.d/process.yaml
    block: "  - name: logstash-forwarder\n    search_string: ['logstash-forwarder']"

vmichnowicz avatar Oct 26 '16 15:10 vmichnowicz

Any news on this issue? I'm still having the same problem as of Ansible 2.2.1.0.

danilo404 avatar Apr 25 '17 12:04 danilo404

Bug still open... Also any number after block: | except 2 throws an invalid syntax in the block itself.

scaronni avatar Mar 15 '19 15:03 scaronni

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

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

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

If you like to intend 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 is without intend and the line insert some stuff has 2 leading spaces.

PetzJohannes avatar Mar 05 '21 12:03 PetzJohannes

It is still broken in 2.11.5. Block Indentation Indicator is not working, but causes:

Syntax Error while loading YAML.
  expected <block end>, but found '<scalar>'

RalphusB avatar Oct 04 '21 19:10 RalphusB

For me, this works fone, but seems that I can only specify numbers until 1 digit, (and I need 12 spaces)

iranzo avatar Feb 22 '23 12:02 iranzo