Configure Docker in AWS with Help of Ansible

Ansible + AWS

PROBLEM STATEMENT ❓

🔹 Configure Docker

🔹 Start and enable Docker services

🔹 Pull the httpd server image from the Docker Hub

🔹 Run the httpd container and expose it to the public

🔹 Copy the html code in /var/www/html directory and start the web server

SOLUTION 🤘

For this setup we know some information about this technology

Ansible 😎=>

Docker😎=>

AWS😎=>

Terraform😎 =>

Now we have integrate All this tools for automation

AWS + ANSIBLE + DOCKER

Now First we have start with AWS because for do any think we need OS so we have start to create two container with help of terraform

So we have create container with terraform for this we have need terraform in our system you can use direct GUI but for some more automation we used terraform

For installing terraform use this link👇

After it check with command

terraform version

Here version of terraform👆

Then we create one folder for workspace using cmd and create one file using .tf extension .tf is fixed for terraform if you not used this you can’t run terraform code

mkdir workspace #command for make folder name of folder is workspace
cd workspace #go to created directory
notepad ansible.tf #open notepad command

we have create notepad file

provider “aws” {
region = “ap-south-1”
profile = “PrincePrashantSaini”
}

#creating security group
resource “aws_security_group” “allow_tls” {
name = “allow_tls”
description = “allow ssh and httpd”

ingress {
description = “SSH Port”
from_port = 0
to_port = 65525
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}

egress {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
}
tags = {
Name = “allow_tls”
}
}

#creating key variable
variable “enter_ur_key_name” {
type = string
default = “awskey”
}

#create EC2
resource “aws_instance” “myinstance” {
ami = “ami-0447a12f28fddb066”
instance_type = “t2.micro”
key_name = var.enter_ur_key_name
security_groups = [“${aws_security_group.allow_tls.name}”]
connection {
type = “ssh”
user = “ec2-user”
private_key = file(“C:/Users/Prashant Saini/Downloads/awskey.pem”)
host = aws_instance.myinstance.public_ip
}
provisioner “remote-exec” {
inline = [
“sudo yum install python3 -y”,
“sudo pip3 install ansible”,

]
}

tags = {
Name = “PrincePrashant”
}
}

then after create notepad file we used

terraform init

terraform init command

terraform apply

now created 😎😎🤘🤘

Now same file used for other VM change name and used it works after this check in AWS👇

now see it configure

Take IP and open in putty and key you can used cmd also but i used putty

Open it and then we go to root user with command

sudo su — root

👉Then we create one more user
We wouldn’t be able to install Ansible as a root user here, because in RHEL 8, this operation is not allowed. So, we are going to create a new user and setup a password for it.

useradd pps

passwd Prince

👉same thing do in second VM also in
(note:-ss we create user pps2 but we have create pps before so it not show here it already exists)

👉Once it done we give power to our user for this we use command

echo “pps ALL=(ALL) NOPASSWD: ALL” >> /etc/sudoers

👉The control node, also referred to as Ansible Master, connects to the managed host using SSH. Though using key-based authentication is recommended, when you are at a learning stage, use password-based authentication. so we go to this file and find PasswordAuthentication it disabled with # tag so we can enabled show in image 👇👇

vi /etc/ssh/sshd_config

After it reload service because if we changed any thing so after it we have to reload the services

systemctl reload sshd

👉In RHEL 8, Ansible cannot be installed as a root user. So, we are going to install Ansible after switching user that we have created we have created pps so we have used command for switch user 👇

su pps

👉after it command they ask password you can give set password👇

now install ansible here with command

pip3 install ansible — user

in my case already install so it not show now you can install in your system

👉Then check with version command it installed or not 👇

ansible — version

👉now create txt file for communicate with other VM for this we have to use vi command and here we give user and password for target vm👇

vi /etc/myhosts.txt

👆In the above ss, I have mentioned the private IP of the managed host under [IP]. This is called creating a group. I created a group named ‘webservers’ so that I can call all the servers at once, and thus avoid using individual IPs in my Ansible commands👆

👉Now let me show you, how we can configure password-less authentication between the ansible-control host and managed hosts. Run the command below in the Ansible directory👇

ssh-keygen
ssh-copy-id pps@172.31.5.176

Try one time with SSH its work or not

ssh pps@172.31.5.176

👉now try to ping one OS to another OS with ansible for check ansible setup or not

now its work fine if you see this green message if its red soo its fail check where you have do mistake 😎😎😎😎😎😎

👉now create one YML file i have create pb.yml you can create any

now final command run ansible notebook

ansible-playbok pb.yml

all green working fine 😎😎😎

check website is working or not that give in pub.yml we have passed home.html. know work fine it means our docker configure and its work fine.

For check in second OS its configure or not use

docker ps
docker ps -a
docker images

working good no challenge

THANKS FOR READING

Devops Engineer