YAML for DevOps

Anyone learning DevOps or a pro already working in DevOps tools, already knows for most of the tools, the configuration is maintained in YAML/YML files, hence it’s very important to understand YAML really well.

In this tutorial, we will cover what is YAML, its use cases, basic syntax, and more!

Sounds interesting? then keep reading…

--- # start of YAML document
# - is used for comment 
#string
name: "Sandip Das"
#nuber
age: 30
# string without quotes
city: Kolkata
#array/list of items
skills:
  - DevOps
  - Cloud
  - Programming
  - Mentoring
  - Communication
#object / nested object
Latop:
 name: Macbook Pro
 model: 2021
 chip: m1 max 
  
cars: 
  - model: 
     colour: Red
     name: "Maruti Dzire"
  - model: 
     colour: Dark
     name: "Tata Nexon EV"
# Boolean: we can use: True/False or Yes/No
is_married : Yes
has_child : True
# Since YAML is a superset of JSON,
# We can also write JSON-style maps and
# sequences:
json_map: {"key": "value"}
json_seq: [3, 2, 1, "Yessss..working"]
#'folded block' (using '>') / Folded String
heading: >
  Senior Cloud & DevOps Engineer , 
  AWS Container Hero , 
  AWS Certified DevOps Professional , 
  Full Stack Developer , 
  Youtuber , 
  Mentor: Teaching Simplified Cloud, DevOps & Programming (Python, Go, JS)
#'literal block' (using |) / Multi line string
About me: |
  Teaching People Simplified Cloud & DevOps topics 😇


  I create new video tutorials every week, which mainly includes Cloud, DevOps, Programming Topics.
  Subscribe and activate bell notification so that you don't miss new videos 🔔


  I am an AWS Hero 🦸♂️ and have 6x AWS Certification  💪
  Visit ►  https://learn.sandipdas.in
some_var: &someVarRef Test Value
some_var_value_referning: *someVarReft

What is YAML?

YAML stand’s for YAML Ain’t Markup Language, is a serialization language that has steadily increased in popularity over the last few years. It’s often used as a format for configuration files, but its object serialization abilities make it a viable replacement for languages like JSON.

YAML has varieties of language support and maps easily into native data structures. It’s also easy for humans to read, which is why it’s a good choice for configuration. The YAML acronym was shorthand for Yet Another Markup Language. But the maintainers renamed it to YAML Ain’t Markup Language to place more emphasis on its data-oriented features.

YAML is a superset of JSON, we can also write JSON-style maps and sequences.

Use cases :

YAML is used as the configuration file for many software/systems, mainly used for DevOps tools such as:

Kubernetes configuration files, GitHub Actions Workflow file, Ansible Playbook, AWS CodeBuild & CodeCommit configuration file, and much more such use cases.

How to Write YAML File?

Point to note before writing:

  • YAML is a data serialization language designed to be directly writable and readable by humans
  • Check https://yaml.org for the latest recommendations
  • YAML is CASE sensitive
  • End your YAML file with the .yaml or .yml extension
  • YAML is a superset of JSON

Dev Environment Setup:

  • Best Editors/IDE: VS CodeAtom
  • Plugins: YAML plugins or language support or lining available in all major IDEs, before starting writing, should install the relevant plugin

Basic Syntax:

  • The file starts with —
  • We can write configuration blocks in the YAML in 2 ways:

— # Indented Block

name: Sandip Das

age: 30

— # Inline Block

{name: Sandip Das, age: 30}

  • We can define value like:

named value:

sample_key: sample_value
  • Array/List
--

sample_list:

- 1

- 2

- 3

- 'sample string'
  • Dictionary

Named Dictionary:

sample_dict:

 key1: val1

 key2: val2

 key3: val3

List of Dictionaries:

 developers

   - name: Sandip Das

     age: 30

   - name: James Smith

     age: 54:

YAML Data Types:

Supported Basic Data Types:

a: 1 # integer

a: 1.234 # float

b: 'abc' # string

b: "abc"

b: abc

c: false # boolean type

d: 2015-04-05 # date typer

Advance Data Types:

Array: Either like below or: ["DevOps", "Cloud", "Programming"...

- DevOps

- Cloud

- Programming

- Mentoring

- Communication]

An array of Objects:

cars

- name: "Maruti Dzire"

  colour: Red

- name: "Nexon EV"

  colour: Dark:

Nested Array of Objects:

cars

- Model

   name: "Maruti Dzire"

   colour: Red

- Model:

   name: "Nexon EV"

   colour: Dark:

YAML Anchors and Alias:

We can place Anchors (&) on an entity to mark a multi-line section. We can then use an Alias (*) call that anchor later in the document to reference that section. E.g.

some_var: &someVarRef Test Value

some_var_value_referning: *someVarRef

How YAML is different from JSON?

Let’s first understand what is JSON?

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data, often used when data is sent from a server to a web page or server to server or program to program communication, works as a common standard data format. It is “self-describing” and easy to understand

JSON Example:

{

"name": "Sandip Das",

"age": 30,

"city": "Kolkata",

"skills": [

"DevOps",

"Cloud",

"Programming",

"Mentoring",

"Communication"

]

}:
No alt text provided for this image

Real Life Usecases:

Kubernetes:

apiVersion: v
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 8080

Ansible Playbook:

- hosts: webserver
	  accelerate: no
	  accelerate_port: 5099
	  ansible_connection: local
	  any_errors_fatal: True
	  become: yes
	  become_method: su
	  become_user: postgress
	  become_flags: True
	  debugger: on_failed
	  gather_facts: no
	  max_fail_percentage: 30
	  order: sorted
	  remote_user: root
	  serial: 5
	  strategy: debug
	  vars:
	    http_port: 80
	  vars_files:
	    - "vars.yml"
	  vars_prompt:
	    - name: "my_password2"
	      prompt: "Enter password2"
	      default: "secret"
	      private: yes
	      encrypt: "md5_crypt"
	      confirm: yes
	      salt: 1234
	      salt_size: 8
	  tags: 
	    - stuff
	  pre_tasks:
	    - <task>
	  roles:
	    - common
	    - common
	      vars:
	        port: 5000
	      when: "bar == 'Baz'"
	      tags : [one, two]
	    - common
	    - { role: common, port: 5000, when: "bar == 'Baz'", tags :[one, two] }
	  tasks:
	    - include: tasks.yaml
	    - include: tasks.yaml
	      when: day == 'Thursday'
	      vars:
	        foo: aaa 
	        baz:
	          - z
	          - y
	    - { include: tasks.yaml, foo: zzz, baz: [a,b]}
	    - <task>
	  post_tasks:
	    - <task>s

AWS CodeBuild BuildSpec.yml example

version: 0.
run-as: root


phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
      - REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
      - echo Writing image definitions file...
      - printf '[{"name":"%s","imageUri":"%s"}]' $CONTAINER_NAME $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
    files: imagedefinitions.json2

GitHub Actions Example:

name: GitHub Actions Dem
on: [push]
jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
      - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
      - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
      - name: Check out repository code
        uses: actions/checkout@v2
      - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
      - run: echo "🖥️ The workflow is now ready to test your code on the runner."
      - name: List files in the repository
        run: |
          ls ${{ github.workspace }}
      - run: echo "🍏 This job's status is ${{ job.status }}."o

How To Read any YAML file using Python?

Python Code:

import yam

if __name__ == '__main__':

stream = open("sample.yaml", 'r')

dictionary = yaml.load(stream, Loader=yaml.FullLoader)

try:

for key, value in dictionary.items():

print (key + " : " + str(value))

except Exception as e:

print("e",e)l

Steps:

  • import python inbuild YAML module
  • open any YAML file in reading mode
  • use yaml.load() function to convert YAML readable file stream data into a python dictionary
  • We can use keys/values of the dictionary variable as needed

Demo Code Repo

Official YAML site with Official YAML Module/Plugin for all programming languages

Ansible playbook demo code

Kubernetes Configuration Demo Codes/Templates

Online YAML Validator

Total
0
Shares
კომენტარის დატოვება

თქვენი ელფოსტის მისამართი გამოქვეყნებული არ იყო. აუცილებელი ველები მონიშნულია *

Previous Post

Containerization Best Practices

Next Post

Run commands on your Linux instance at launch

Related Posts