I am trying to use ansible blockinfile with its indent feature. Here is my playbook:
- name: Add line to netplan file
become: true
ansible.builtin.blockinfile:
path: /etc/netplan/00-installer-config.yaml.REMOVE
marker: ""
block: |4
test
Here is the 00-installer-config.yaml.REMOVE file before the change:
test
Here is it after
test
test
Notice how it only does 2 spaces, even though I state clearly to use 4. I have tried 8 spaces, and 10, any integer. Always indents 2 spaces. why?
The simple playbook below writes the block into the file
- hosts: localhost
vars:
test: |
test
tasks:
- copy:
dest: /tmp/test.txt
content: "{{ test }}"
The text is not indented in the file
shell> cat /tmp/test.txt
test
If we indent the text in the block further
vars:
test: |
test
the text is still not indented in the file because YAML interprets all four spaces as YAML indentation
shell> cat /tmp/test.txt
test
If we tell YAML that we will use only two spaces as YAML indentation
vars:
test: |2
test
The text will be indented by 2 spaces because YAML takes 2 spaces as YAML indentation and the next 2 spaces as part of the text in the block
shell> cat /tmp/test.txt
test
This explains the behavior of the text indentation in a block.
Notes:
- hosts: localhost vars: test: | line1 line2 line3 tasks: - copy: dest: /tmp/test.txt content: | {{ test|indent(4, first=true) }}
gives
shell> cat /tmp/test.txt line1 line2 line3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.