More Ansible tips
Here are more cool Ansible tips I've learned recently.
Breaking long lines
Some times your playbooks have long lines and you want to break them up. Here are some ways to do that:
---
- hosts: localhost
vars:
one: >
long variable
all lines joined together
A NEWLINE ENDS
two: >-
long variable
all lines joined together
NO NEWLINE ENDS
tasks:
- debug:
msg: "{{ one }}"
- debug:
msg: "{{ two }}"
- name: Works with jinja expressions too!
debug:
msg: >-
{{
two
}}
- name: Preserves line breaks
shell: |
for i in one two three
do
echo $i
done
register: cmd
- debug:
msg: "{{ cmd.stdout }}"
Now lets run it.
PLAY [localhost] *********************************************************
TASK [Gathering Facts] ***************************************************
ok: [localhost]
TASK [debug] *************************************************************
ok: [localhost] => {
"msg": "long variable all lines joined together A NEWLINE ENDS\n"
}
TASK [debug] *************************************************************
ok: [localhost] => {
"msg": "long variable all lines joined together NO NEWLINE ENDS"
}
TASK [Works with jinja expressions too!] *********************************
ok: [localhost] => {
"msg": "long variable all lines joined together NO NEWLINE ENDS"
}
TASK [Preserves line breaks] *********************************************
changed: [localhost]
TASK [debug] *************************************************************
ok: [localhost] => {
"msg": "one\ntwo\nthree"
}
PLAY RECAP ***************************************************************
localhost : ok=6 changed=1 unreachable=0 failed=0
Here's the answer that taught me this.
Using a YAML linter
A YAML linter helps me find those annoying hard to find errors. I use Vim, but I'm sure you can find something with your editor. Here's what I did.