About Specific YAML Syntax

YAML format has some syntax which has no analogies in JSON.

  • > means that the lines after will be combined into a single line by replacing line breaks to spaces.
  • | means that the lines after should preserve the original line breaks.

Here are some example Ansible commands using that syntax:

- name: Backup and optimize database
  shell: >
    pg_dump mydatabase > /backup/db_backup_$(date +%Y%m%d).sql &&
    psql mydatabase -c "VACUUM FULL ANALYZE" &&
    gzip /backup/db_backup_$(date +%Y%m%d).sql

- name: Create a script
  copy:
    content: |
      #!/bin/bash
      echo "First line"
      echo "Second line"
      exit 0
    dest: /path/to/script.sh

Tips and Tricks Programming Ansible YAML