Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Lists & Dictionary YAML Syntax by example

YAML Syntax for Ansible


  • Option 1:
    - Apple
    - Orange
    - Strawberry
    - Mango
  • In valid JSON:
  • Option 2:
- debug: msg='fruit name {{item}}'
  with_items: fruits
- debug: msg='fruit name {{item}}'
  with_items: fruits
  • In valid JSON:
[{"debug":"msg='fruit name {{item}}'","with_items":"fruits"},{"debug":"msg='fruit name {{item}}'","with_items":"fruits"}]


  • Option 1:
  - name: server1
    subject: pepito
  - name: server2
    subject: juanito
  • In valid JSON:
  • Usage:
- command: path={{ }} mode={{ item.subject }}
  with_items: instructions
  • Option 2:
- martin:
    name: Martin D'vloper
    job: Developer
    skill: Elite
- john:
    name: John D'vloper
    job: PM
    skill: Elite
  • In valid JSON:
[{"martin":{"name":"Martin D'vloper","job":"Developer","skill":"Elite"}},{"john":{"name":"John D'vloper","job":"PM","skill":"Elite"}}]
  • Option 3:
  -  martin: {name: Martin D'vloper, job: Developer, skill: Elite}
  -  john: {name: John D'vloper, job: PM, skill: Elite}

Dictionary with subitems

- prod_list:
  - prod_name: "unknown product"
      prod_serial: "123456"
      prod_location: /opt/product1"
  - prod_name: "popular product"
      prod_serial: "987654"
      prod_location: /opt/product2"
  • Usage:
- debug: msg="product name {{item.prod_name}} serial {{item.prod_serial}} location {{item.prod_location}}"
  with_items: prod_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment