DevOps Classroom notes 02/Nov/2024

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
    • number
    • text
    • boolean
  • Complex
    • list
    • map
  • 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)
        • name: string
        • debug:
  • 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'
  • Example
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
    Preview
  • 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

  • manual steps
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
      Preview
  • 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

  • Manual steps
touch /tmp/1.txt
mkdir /tmp/test
  • Playbook structure
---
- 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
  • Playbook
- 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


Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube