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)
- all steps Refer Here
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>andhttp://<publicip>/info.php

- Manual Steps: (Ubuntu 20.04)
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 automatingapt
- 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>

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

