Introduction to YAML

This lab will help get you familiar with YAML, an important format for using Ansible and Kubernetes. YAML lets you defined structured data in a reasonably straightforward way, without a lot of extra formatting, which is why it’s called a human readable format. But, it takes a bit of practice to get it right because there are a few gotchas in YAML that you have to master before you can use it fully.

Step 1: My YAML Checker

In the bin/ directory of the course repository you’ll find a file called yaml-check.py. It’s a simple program takes YAML on standard input and parses it into a Python dictionary and prints it. Run it from the command line and give it simple input:

$ python3 cis-91/bin/yaml-check.py
hello: world
<Ctrl-d>
{'hello': 'world'}

In the example <Ctrl-d> means hold the Control key and tap the d key

Step 2: A List of Values

Now enter a simple list of the following values:

  • Apple

  • Orange

  • Banana

  • Pear

The output of the program should look like this:

['Apple',
 'Orange',
 'Banana',
 'Pear']

Step 3: A Dictionary

A dictionary is a set of key/value pairs. Enter this list of common names (keys) to scientific names (values):

  • Lion: Panthera Leo

  • Tiger: Panthera Tigris

  • Bear: Ursus Arctos

  • Cat: Felis Sylvesteris Cattus

The output should look like this:

{'Lion': 'Panthera Leo',
 'Tiger': 'Panthera Tigris',
 'Bear': 'Ursus Arctos',
 'Cat': 'Felis Sylvesteris Cattus'}

Step 4: A Contact List

Now use YAML to create a contact list with two contacts.

  • Mike Matera

    • Email: mike@mike.com

    • Phone:

      • Work: 555-1212

      • Mobile: 555-2323

  • Bike Batera

    • Email: bike@bike.com

    • Phone:

      • Work: 555-3434

      • Mobile: 555-4545

Use a YAML File Instead

Since it might take you a few tries, consider making a file called contact.yaml and executing the script like this:

python3 yaml-check.py < contact.yaml

The output of the program should look like this:

[{'Mike Matera': {'Email': 'mike@mike.com',
                  'Phone': {'Work': '555-1212', 'Mobile': '555-2323'}}},
 {'Bike Batera': {'Email': 'bike@bike.com',
                  'Phone': {'Work': '555-3434', 'Mobile': '555-4545'}}}]

Turn In

Submit your contact list YAML on Canvas.