Example for an arbitrary number of lines
---
- hosts: localhost
gather_facts: false
vars:
# You can add any number of lines
my_lines:
- hello
- I can add many lines
- and even more
- bye
my_lines_joined: "{{ my_lines | join('\n') }}"
tasks:
- name: Display the expected result
ansible.builtin.debug:
var: my_lines_joined
- name: "To clear any misunderstanding, let's push the result
to a file we can later inspect to see we effectively
wrote new lines"
ansible.builtin.copy:
content: "{{ my_lines_joined }}"
dest: /tmp/test-result.txt
Running this playbook results in:
PLAY [localhost] *************************************************************************************************************************************************************
TASK [Display the expected result] *******************************************************************************************************************************************
ok: [localhost] => {
"my_lines_joined": "hello\nI can add many lines\nand even more\nbye"
}
TASK [To clear any misunderstanding, let's push the result to a file we can later inspect to see we effectively wrote new lines] *********************************************
changed: [localhost]
PLAY RECAP *******************************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
And we can then inspect the file content:
$ cat /tmp/test-result.txt
hello
I can add many lines
and even more
bye