More Ansible tips

Tags:

go to ansible.com

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.

  1. Installed yamllint. It's in Debian's package list.
  2. Installed syntastic. Debian also has this, but it's too old. Version 3.8 is required.
  3. Created an ftplugin file ~/.vim/ftplugin/vim.yaml. Here's mine.
submit to reddit