DevOps Classroom notes 07/Oct/2023

Ways of Working in Ansible

  • Figure out the manual steps
  • Execute and verify if they are working
  • for each manual step findout an ansible module which can help

Ansible Module

  • this is the smallest unit of work in Ansible
  • Module takes some inputs which are generally referred as parameters and it has state
  • Ansible modules Refer Here

Ansible Execution Approaches

  • Adhoc command: This uses the following structure and it is used for non repetitive tasks
ansible -m "<module-name>" -a "<arguments>" <where>
  • Playbook: This is yaml representation of sequence of commands and it is designed to be reptitive.

Activity: install apache server on node 1

sudo apt update
sudo apt install apache2 -y
  • Finding modules
    Preview
  • Dev Env: Use Visual studio code and install ansible extension from Redhat
    Preview
  • Refer Here for the changes
  • Syntax check
ansible-playbook --syntax-check -i <path to inventory> <path to playbook.yaml>
ansible-playbook --syntax-check -i hosts activity1.yaml

Preview
* Check the execution (dry run). Note this is not always correct

ansible-playbook --check -i <path to inventory> <path to playbook.yaml>
ansible-playbook --check -i hosts activity1.yaml

Preview
* Now lets execute the playbook

ansible-playbook -i <path to inventory> <path to playbook.yaml>
ansible-playbook -i hosts activity1.yaml

Preview
Preview
* Now lets rerun
Preview

Activity 2: Install php on apache server

sudo apt udpate 
sudo apt install apache2 -y
sudo apt install php libapache2-mod-php php-mysql -y
  • Lets create a file /var/www/html/info.php with the following content
<?php phpinfo(); ?>
  • Now access http://publicip/info.php
    Preview
  • Refer Here for the changes done
  • Execute ansible playbook after syntax check and check
ansible-playbook -i hosts php.yaml

Preview
Preview

Activity 3: Perform the activity2 on redhat

  • I need to setup a redhat vm with devops user
  • manual steps:
sudo dnf install httpd -y
sudo dnf install php php-cli php-common php-gd php-mysqlnd php-pdo -y
  • Lets create a file /var/www/html/info.php with the following content
<?php phpinfo(); ?>
  • Refer Here for the playbook
  • Execute ansible playbook after syntax check and check
ansible-playbook -i hosts php.yaml
  • We need to start the service Refer Here for the changes done to start and enable the service
    Preview

Activity 4: Use the same playbook for ubuntu and redhat machines

  • To do this activity, the two topics which we need to understand are
    • inventory groups
    • facts

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner