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


 

 

 

 

Categories
Uncategorized

Ansible

Ansible

Inventory files:

using groups:

[mail]

mailsvr1.example.com
mailsvr2.example.com

[db]

dbsvr1.example.com
dbsvr2.example.com

[web]

websvr1.example.com
websvr2.example.com

Group servers

[allsvrs:children]

mail
db
web

using alias: using parameters

web    ansible_host=server1.example.com
db ansible_host=server2.example.com

#other parameters, ansible_connection, ansible_port, ansible_user, ansible_ssh_pass

#check ansible Vault to use passwd in encryted format

web    ansible_host=server1.example.com    ansible_connection=ssh  ansible_port=(default is 22)    ansible_user=root
db ansible_host=server2.example.com ansible_connection=winrm ansible_user=admin
mail ansible_host=server3.example.com ansible_connection=ssh ansible_ssh_pass=mypasswd
web2 ansible_host=server4.example.com ansible_connection=winrm ansible_password=winpasswrd

#use localhost to indicate using localhost and not remote host

localhost    ansible_connection=localhost

#Create a new directory script/ansible/inventory.txt

user@ansiblecontroller: vim inventory
backupsvr ansible_host=192.168.1.10 ansible_ssh_pass=mypass
desktop ansible_host=192.168.1.11 ansible_ssh_pass=mypass ansible_ssh_user=myuser

Run ansible:

ansible <hosts> -a <command>
ansible all -a "/sbin/reboot"
ansible <hosts> -m <module>
ansible desktop -m ping -e 'ansible_python_interpreter=/usr/bin/python3' -i inventory.txt

Run Ansible-playbook

ansible-playbook <playbook-name>

Ansible Modules

System: 
- User
- Group
- Hostname
- Iptables
- Make
- Mount
- Service
- Systemd

Commands:
- Command
- Script
- Shell
- Raw

Files:
- Arhive
- File
- Find
- Replace

Database:
- Mysql
- Postgresql

Windows:
- Win_copy
- Win_command
- Win_user