Skip to content

Instantly share code, notes, and snippets.

@carlessanagustin
Last active January 22, 2024 21:55
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save carlessanagustin/50dab6d642e34f8f617d to your computer and use it in GitHub Desktop.
Save carlessanagustin/50dab6d642e34f8f617d to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment