carlessanagustin
2/8/2016 - 2:45 PM

Lists & Dictionary YAML Syntax by example

Lists & Dictionary YAML Syntax by example

YAML Syntax for Ansible

Lists

  • Option 1:
fruits:
    - Apple
    - Orange
    - Strawberry
    - Mango
  • In valid JSON:
{"fruits":["Apple","Orange","Strawberry","Mango"]}
  • 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"}]

Dictionary

  • Option 1:
instructions:
  - name: server1
    subject: pepito
  - name: server2
    subject: juanito
  • In valid JSON:
{"instructions":[{"name":"server1","subject":"pepito"},{"name":"server2","subject":"juanito"}]}
  • Usage:
- command: path={{ item.name }} 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:
employees:
  -  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