Ansible:4

DevOps Classroom Series – 18/Dec/2020

Ansible Key Terms

  • Inventory: list of nodes to which ACS has to connect
  • Module: Atomic unit of automation in Ansible where we can express the desired state.
    • We specify our desired state in ansible using modules
  • Task: Task in ansible will consist of module
  • Play: will consists of multiple tasks
  • Playbooks: will consist of one or more play’s Preview
  • In Ansible we express playbooks in the form of YAML files Refer Here for json and yaml tutorial

Ansible Usage Workflow

  • We use ansible to deploy applications.
  • Deploying applications is a sequence of commands to be executed.
  • Make a note all the commands to be executed and for each command find the equivalent module
  • Now express all of the desired state (intention) in module by using Playbook structure.

Lets try to realize the above steps, install a LAMP server on ubuntu 18

# updates the ubuntu packages
sudo apt update
# installs apache2 (apache server)
sudo apt install apache2 -y
# install php packages
sudo apt install php libapache2-mod-php php-mysql php-cli -y
# create a info.php file in /var/www/html/info.php
# become root user
sudo -i
echo "<?php phpinfo(); ?>" > /var/www/html/info.php

  • Lets try to find ansible modules Refer Here
  • Lets take first few steps where we use package manager (apt) Preview Preview
  • To the module we pass parameters as arguments & one parameter (state) will define
  • At this moment we want to update packages and install apache2
    • We need to set the package name to apache2 and we need to update apt packages
    • So to our module (apt Refer Here), we have give the following parameters
      • name: apache2
      • state: present
      • update_cache: yes
  • In Ansible i can execute the modules directly from command line (adhoc commands) or we can write playbook in yaml format
  • For today lets use adhoc command
  • Our inventory is node1 so create it Preview
  • Then lets construct the ansible adhoc command Refer Here

# ansible ensure apache2 package is installed and packages are up to date to all servers in inventory
ansible -b -i hosts -m apt -a "name=apache2 state=present update_cache=yes" all

  • If we have to run the same in YAML format
---
- name: installing apache server
  become: yes
  hosts: all
  tasks:
    - name: install apache2
      apt:
        name: apache2
        update_cache: yes
        state: present

  • Next Steps: Understanding YAML and writing YAML for configuration management
  • Please ensure you have the softwares installed as Refer Here