zerkms
zerkms

Reputation: 254926

Multiline strings with leading spaces

How would one specify the multiline strings that have leading spaces on some lines?

If I define a variable as

multiline_str: |
    foo
      bar
        baz

And then write it to a file using

- name: write multiline string
  copy: content="{{ multiline_str }}" dest="/path/to/file"

Then the target file contents is

foo
bar
baz

What is the trick here?

Upvotes: 6

Views: 11362

Answers (4)

Philipp Waller
Philipp Waller

Reputation: 604

You can use the escape notation to keep the leading spaces:

multiline_str: |
  \  foo
  \    bar
  \      baz

Upvotes: 0

Eric Anderson
Eric Anderson

Reputation: 11

You may be able to use \n (newline) and \s (space) \t (tab)

Upvotes: 1

visit1985
visit1985

Reputation: 498

Had the same problem combined with local_action.

This works:

- name: write multiline string
  local_action:
    module: copy
    content: "{{ multiline_str }}"
    dest: /path/to/file

Upvotes: 1

yaegashi
yaegashi

Reputation: 1510

Try this.

- name: write multiline string
  copy:
    content: "{{ multiline_str }}"
    dest: /path/to/file

Upvotes: 11

Related Questions