Blog 7: Installation and Configuration of packages using Ansible on AWS EC2
Installing and configuration of packages using Ansible on AWS EC2 instance
First, we need to install ansible on the container. Based on different Linux OS we can install ansible.
apt-get update -y
- apt-get install ansible -y
- yum install ansible
- pip install ansible
- brew install asnible
Ansible is the automation tool for cloud provisioning, configuration management, application deployment, etc. File extension for the fie is .yml which has strict indention rules.
I prefer to use vim as text editor to edit the files for the ansible playbook file.
apt-get install vim -y
Creating anisble playbook file
There are Strict mandatory steps and indention rules to write the Ansible code. For EG: spacing, syntax, etc.
Here, the Ansible code will perform different confiugrations. This includes update and upgrade of repository, installing curl, ssh, git, apache2, mysql, php packages.
For PHP packages installation, I have not mention any ver.. Since, the latest version will download latest version above 7.2
Creating a new file for the ansible code:
vi exampl.yml
---
- hosts: web_server
tasks:
- name: Update and Upgrade repo
become: true
apt:
upgrade: yes
update_cache: yes
cache_valid_time: 86400
- name: Installing packages
become: true
apt:
- name:
- curl
- ssh
- git
- apache2
- mysql-client
- php
- libapache2-mod-php
- php-mysql
- php-cli
- php-curl
- php-gd
- php-mbstring
- php-mysql
- php-xml
- php-xml
state: present
Using the syntax Become: true
This statement denotes that the command will execute with root privelege.
Attatching the ipv4 address of EC2 instance
After launching a new instance on the AWS, we can now access the EC2 instance using CLI. We need to have a key pair and use ssh command to get access.
ssh -i <path to EC2 key pair> ec2-user@<ipv4 address of EC2 instance>
We can also give root privelege to get access to EC2 instance if any error occurs.
sudo ssh -i <path to EC2 key pair> ec2-user@<ipv4 address of EC2 instance>
Creating a Host file
To make connection with specified servers. We need to create a sperate file on the same ansible file directory. The file that hosts specified servers should have extension of .ini
Here, we will create a file named hosts.ini on the same directory.
vi hosts.ini
[web_server]
<ipv4 address of AWS EC2 instance>
Running ansible file
After denoting the EC2 instance ipv4 address on the Ansible file. We can perform the following commands:
ansible-playbook -i hosts.ini exampl.yml -u ec2-user --key-file ~<path to KEY pair>
Now by accessing the EC2 instance with the following commands we can verrify if the installation was sucessful. If any errors occurs we can see on the page where we have ran the ansible-playbook.
sudo ssh -i <path to EC2 key pair> ec2-user@<ipv4 address of EC2 instance>
apache2 -v