YAML
YAML is a file format which is designed over name value pairs
YAML is used to represent data
YAML files have extension of .yml or .yaml
Any thing in yaml
<name>: <value>
Data is categorized into following types
number:
yaml
experience: 4
text
yaml
designation: DevOps Engineer
boolean
yaml
certifications: yes
list/array
“`yaml
tools:</li>
<li>ansible</li>
<li>docker</li>
<li>k8s
“`
map/object/dictionary
yaml
address:
flatno: 601
building: nilgiri
landmark: ameerpet metro
city: hyderabad
Scalar or simple types
Complex
Example 1 : Ansible Playbook
---
- name: Install and configure Nginx web server
hosts: webservers
become: true
gather_facts: yes
tasks:
- name: Display OS information
debug:
var: ansible_distribution
- name: Update apt cache
apt:
update_cache: yes
- name: Install Nginx web server
apt:
name: nginx
state: latest
- name: Start Nginx service
service:
name: nginx
state: started
Structure:
list(map/object)
name: string
become: boolean
gather_facts: boolean
tasks: list(map/object)
Example
trigger: #list(text)
- main
pool: #map/object
vmImage: 'ubuntu-latest' #text
stages: # list(map/object)
- stage: Build
displayName: 'Build Stage'
jobs:
- job: BuildJob
displayName: 'Build Job'
steps:
- script: echo "Building the application..."
displayName: 'Build Step'
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '**/*.csproj'
- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/*.csproj'
arguments: '--configuration Release'
- stage: Test
displayName: 'Test Stage'
jobs:
- job: TestJob
displayName: 'Test Job'
steps:
- script: echo "Running tests..."
displayName: 'Test Step'
- task: DotNetCoreCLI@2
inputs:
command: 'test'
projects: '**/*.csproj'
arguments: '--configuration Release --no-build'
- stage: Deploy
displayName: 'Deploy Stage'
jobs:
- job: DeployJob
displayName: 'Deploy Job'
steps:
- script: echo "Deploying the application..."
displayName: 'Deploy Step'
- task: AzureWebApp@1
inputs:
azureSubscription: '<YourAzureSubscription>'
appType: 'webApp'
WebAppName: '<YourWebAppName>'
packageForLinux: '$(System.DefaultWorkingDirectory)/**/*.zip'
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Refer Here for yaml syntax
In devops we use yaml in many tools, each tool has a schema or structure defined and we need to follow.
Ansible Playbook
Ansible Playbook is written in YAML format
Structure
In each Playbook task is the atomic activity (smallest unit of work) & is achevied with the help of module
A module is where we can express what we want
Way of Working
List down manual steps and ensure they are working
try to find a module for each manual step
Trying the above way of working
Sample 1
wget https://www.free-css.com/assets/files/free-css-templates/download/page296/oxer.zip
unzip oxer.zip
Finding ansible modules
approach 1: Refer Here for module list
approach 2: google <command> in ansible
Module expressed in playbook as a task
---
- name: this is sample playbook
hosts: all
become: no
tasks:
- name: download the zip file
<module-name>:
<parameter-1>:
#..
<paramter-n>:
#[state]:
Lets fill the download file
---
- name: this is sample playbook
hosts: all
become: no
tasks:
- name: download the zip file
ansible.builtin.get_url:
url: https://www.free-css.com/assets/files/free-css-templates/download/page296/oxer.zip
dest: /tmp/oxer.zip
- name: unzip the file
ansible.builtin.unarchive:
dest: /tmp/oxer
src: /tmp/oxer.zip
Sample 2
touch /tmp/1.txt
mkdir /tmp/test
---
- name: this is sample playbook
hosts: all
become: no
tasks:
- name: create a file
ansible.builtin.file:
path: /tmp/1.txt
state: touch
- name: create a directory
ansible.builtin.file:
path: /tmp/test
state: directory
Sample 3
I want to install apache, php in the ubuntu 22.04 Refer Here
sudo apt update
sudo apt install apache2 -y
sudo apt install php libapache2-mod-php php-mysql
- name: install apache and php
become: yes
hosts: all
tasks:
- name: install apache
ansible.builtin.apt:
name: apache2
update_cache: yes
state: present
- name: install php packages
ansible.builtin.apt:
name:
- php
- libapache2-mod-php
- php-mysql
state: present