DevOps Classroom Series – 08/Oct/2021

Approach

  • We use ansible in DevOps to automate deploying Applications, So we need to know the manual way of deploying before we automate the deployment.
  • Make a note of all the steps involved in deploying an application and try to automate each step using Configuration Management.
  • Example 1: Installing Apache and PHP on the Ubuntu server
    • Manual Steps: (Ubuntu 20.04)
      sudo apt update
      sudo apt install apache2 -y
      sudo apt install php libapache2-mod-php php-mysql -y
      # Create a file in /var/www/html/info.php "<?php phpinfo(); ?>"
      #echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
      sudo service apache2 restart
      
      • Navigate to http://<publicip> and http://<publicip>/info.php Preview Preview

YAML

  • We would be using YAML to write Ansible Playbooks
  • In YAML we define data as collection of name-value pairs
  • Refer Here

Playbook

  • Playbook represents collection of plays. Each play is collection of tasks. Each task represents a step to be automated (desired state)
- name: <name of your play>
  hosts: <where do you want to execute>
  become: <For installation do we need to become sudo user>
  tasks:
    - name: <name of the task>
      <module>: <desired state>
    - name: <name of the task>
      <module>: <desired state>
    ..
    - name: <name of the task>
      <module>: <desired state>
         
  • Module in Ansible does the actual work (Smallest unit of work), using modules we can describe desired state
  • Refer Here for the list of modules
  • Lets start writing ansible playbook for installing apache and php
  • First step involves updating the ubuntu packages using apt. To find the module in apache which helps in automating apt Preview
  • Once we find the module to express our desired we need to use parameters
  • So in our case the module is apt Refer Here and lets look at parameters
  • Refer Here for the changes done in the class
  • To run the playbook the command is
ansible-playbook -i <path to invetory> <path to playbook.yaml>

Preview

  • When we execute again ansible has dont any thing as apache is already installed Preview

Leave a Reply

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

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

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