Categories
Uncategorized

YAML

Key Value Pair, there is a space after :

Fruit: Apple
Vegetable: celery
alcohol: beer
meat: beef

Arrays or List, there are 4 spaces after the dash

Fruit:
- orange
- apple
- banana

 

Dictionary, there are 4 spaces before Calories, Fat and Carbs

Banana:
Calories: 100
Fat: 400 gr
Carbs: 27 gr

Grapes:
Calores: 5
Fat: 0 gr
Carbs: 10 gr

Key:Value, Dictionary and Lists

Dictionaries have spaces at the beginning (4 in this case)
Lists have – followed by spaces (4 in this case)
Key: Value pair have one space after the :

Fruit:
– Banana:
Calories: 100
Fat: 100 gr
Carbs: 100 gr

When to use Dictionary, List or List of dictionaries
Dictionary: Use a dictionary to store properties of a single object defined as a key: value pair

Car:
Colour: blue
price: $20000
transmission: manual
model: corvette

If you split the model, then you represent a Dictionary withing another Dictionary

Car:
Color: blue
Model:
Name: corvette
year: 1995

List: Multiple items of the same type of object, the following is a list of strings

- blue Corvette
- grey Covertte
- red Corvette

List of Dictionaries: expand the list from a list of string to a list of dictionaries

- Color: blue
Model:
Name: corvette
year: 1995

- Color: grey
Model:
Name: corvette
year: 1995
- Color: red
Model:
Name: corvette
year: 1995

 

Dictionary: Unordered collection. The order doesn’t matter as long as they match.

Banana:
Calories: 100
Fat: 40 g
Carbs: 0 g
Banana:
Fat: 40 g
Calories: 100
Carbs: 0 g

List: order collection matters. The following is not the same

Fruits:
- Orange
- Apple
- Banana
Fruits:
- Apple
- Orange
- Banana

Loops:

-
name: Install Packages
hosts: all
taks:
- yum: name='{{ item }}' state:present
with_items:
- httpd
- gcc
- make

Include statements and Roles: include <playbook name>

Include statements, split yml file as Provision Vm, Install Dependencies, start app
setup_app.yml
setup App
- include provision.yml
- include dependencies.yml
- include startapp.yml

INCLUDE and TASKS and VARS


 name:  install app
 vars:
   http_port: 8081
   snmp_port:160-161
tasks:
  – firewall:
     service:https

move service:https to a file calles tasks.yml and replace above code with  – include: tasks.yml and move variables to variables.yml. Then

vars_files:
  – variables.yml
tasks:
  – include: tasks.yml

 

ROLES: 
Ansible Project
  inventory.txt
 setupapp.yml
 roles
     webservers
        files
        templates 
        tasks 
        handlers
        vars 
        defaults 
        meta 

Then move the variables.yml into the vars directory
Move the tasks.yml into the tasks folder  

In the master playbook, assing the roles
-
  name: Setup app
  hosts: all
  roles:
     - webservers