๐๐ฒ๐ป๐ธ๐ถ๐ป๐ ๐ง๐ฒ๐ฟ๐ฟ๐ฎ๐ณ๐ผ๐ฟ๐บ-๐๐ป๐๐ถ๐ฏ๐น๐ฒ ๐ฃ๐ถ๐ฝ๐ฒ๐น๐ถ๐ป๐ฒ :- ๐จ ๐ญ๐๐๐ ๐ฎ๐๐๐ ๐.
In this, we will make a jenkins terraform ansible pipeline.
Prerequisites
Before starting the project, you should have these things in your system :-
Account on AWS
Code
STEP 1 : Launch Instance
Connect to instance through ssh :-
Now create a S3 bucket :-
Bucket created :-
Part 2 : Setup Jenkins
- Now to install Jenkins, first we need Java install because Jenkins need Java so for to install Java use command :-
sudo apt upgrade && sudo apt upgrade -y
sudo apt install fontconfig openjdk-17-jre
- To check Java version use command :-
java --version
- After installing Java, we will install Jenkins so for to install Jenkins use command :-
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
- To check Jenkins status, use command :-
sudo service jenkins status
- Now copy Public IPv4 address:8080 and we will be on Unlock Jenkins page. To unlock jenkins, use command :-
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
- And we will get our password. Copy and paste it to unlock Jenkins โ Now click Install suggested plugins โ Fill details โ Welcome to Jenkins
Part 2 : Terraform
- To install terraform use command :-
sudo apt update && sudo apt install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt -y install terraform
terraform --version
Part 3 : Ansible
- To install ansible use command :-
sudo apt update
sudo apt -y install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt -y install ansible
ansible --version
Now make sure you have installed python3 & boto3 :-
python3 --version
pip show boto3
- Now create a file named aws_ec2.yaml in the /opt directory :-
sudo vi /opt/aws_ec2.yaml
You can change the inventory location as per your requirement but you have to specify your inventory location in ansible configuration file
Now write the following configuration in aws_ec2.yaml file :-
---
plugin: aws_ec2
regions:
-us-east-2
filters:
tag:Environment: dev
Note: You can change the tags as per the requirement.
- Create a role with admin access policy and attach it to the server.
Note: if you donโt want to Create role then you can put access and secret keys in the aws_ec2.yaml but it is not a good practice.
- Now to enable EC2 plugin, open /etc/ansible/ansible.cfg file :-
sudo vi /etc/ansible/ansible.cfg
Find the [default] section and add the following line to inventory :-
inventory = /opt/aws_ec2.yaml private_key_file = /etc/ansible/Akshay_key.pem host_key_checking = False
Find the [inventory] section and add the following line to enable the ec2 plugin.
enable_plugins = aws_ec2
Now open /etc/ansible/hosts file :-
Now open /etc/ansible/hosts file :-
Now open /etc/ansible/hosts file :-
And add the following line as given below :-
Also copy key file from local system to /etc/ansible through scp.
- Now install AWS CLI v2 (run these commands in home directory) :
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt install unzip
unzip awscliv2.zip
sudo ./aws/install -i /usr/local/aws-cli -b /usr/local/bin --update
Now we have to configure AWS CLI, for this we need IAM user. So to make IAM user, Go To IAM โ Create user โ Username โ Next โ Attach policies directly โ select AdministratorAccess โ Next โ Create User. Now we will make security credentials. Why? Because our AWS CLI can do identify my account. Now go to User โ Security Credentials โ Create access key โ select CLI โ Next โ Create access key. So we will get Access key and Secret access key. Now go to instance and write command given below :-
aws configure
Now paste Access key and Secret access key
Now itโs time to test our dynamic inventory. Run the below ad hoc command to test our dynamic inventory :-
ansible-inventory -i /opt/aws_ec2.yaml --list
Change permission 744 of aws_ec2.yaml file and ansible.cfg file. Also change permission 644 of key file.
Now build a pipeline click on Create a job โ give name "Terraform-Ansible-Project" โ select "Freestyle project" โ click OK.
Now add choice & string parameters :-
Now select git in Source Code management :-
Now click Apply and Save โ Build with Parameters and our pipeline will build successfully.
In this we created 2 instances through terraform and deploy simple website through ansible.
Our Terraform-Ansible Project Completed ๐.
๐I've put a lot of effort please take a look at the project on GitHub and let me know what you think! Your feedback means a lot. Do Repost on Linkedin to share this valuable Project to your Connections.๐
GitHub Repository :https://github.com/akshayshinde3/Terraform-Ansible-Project.git
linkdin: https://www.linkedin.com/in/akshay-shinde99/
Happy IAC !!!!
Happy Reading !!!!
Akshay Shinde