Understanding multi line strings in YAML and Ansible (Part II - Ansible)
In Part I of this series we examined the two block styles of YAML, literal and folded, as well as the three block chomping methods, strip, clip and keep. In this post we want to investigate how these styles and methods interact with different Ansible use cases.
Multi line strings in Modules
The classic usage of a multi line string in Ansible is in the command
or shell
module. This example is directly taken from the Ansible docs:
# You can use shell to run other executables to perform actions inline
- name: Run expect to wait for a successful PXE boot via out-of-band CIMC
shell: |
set timeout 300
spawn ssh admin@{{ cimc_host }}
expect "password:"
send "{{ cimc_password }}\n"
expect "\n{{ cimc_name }}"
send "connect host\n"
expect "pxeboot.n12"
send "\n"
exit 0
args:
executable: /usr/bin/expect
delegate_to: localhost
A literal style block with clip chomping is used to send several commands.
Folded style would not make any sense here, except if you wanted to either pipe
the commands or chain them with &&
or ||
.