Saturday, July 27, 2024



CONFIGURATION TASKS WITH ANSIBLE PLAYBOOK

Ensure you have Ansible installed on your control machine. You can install Ansible using the following command:

sudo apt update
sudo apt install ansible
ansible –version

Save your key.pem on ubuntu server

vi key.pem

chmod 600 key.pem

Pwd


For MacUsers Only (Copy Key.pem to Ec2)

scp -i ~/Desktop/20.pem ~/Desktop/20.pem ubuntu@52.207.218.240:/tmp/

ssh -i ~/Desktop/20.pem ubuntu@52.207.218.240

sudo mv /tmp/20.pem /home/ubuntu/
sudo chown ubuntu:ubuntu /home/ubuntu/20.pem 


Create Your Inventory File

Create an inventory file vi host.ini that lists your EC2 instances. For example:

[ec2_instances]
ec2_instance ansible_host=3.15.0.203 ansible_user=ubuntu ansible_ssh_private_key_file=/home/ubuntu/key.pem



Create Your Playbook

Create a playbook file (install_git_java.yml) with the following content:

---
- name: Install Git and Java on EC2 Instances
  hosts: ec2_instances
  become: yes

  tasks:
    - name: Update and upgrade apt packages
      apt:
        update_cache: yes
        upgrade: dist

    - name: Install Git
      apt:
        name: git
        state: present

    - name: Install Java
      apt:
        name: openjdk-11-jdk
        state: present


This playbook does the following:

  1. Updates the apt package cache and upgrades all packages.
  2. Installs Git.
  3. Installs Java (OpenJDK 11).

 Run the Playbook

Run the playbook using the following command:

ansible-playbook -i host.ini install_git_java.yml
Playbook to Install Jenkins

---
- name: Install Jenkins on EC2 Instances
  hosts: ec2_instances
  become: yes

  tasks:
    - name: Update apt package index
      apt:
        update_cache: yes

    - name: Install Java (Jenkins dependency)
      apt:
        name: openjdk-11-jdk
        state: present

    - name: Install required packages
      apt:
        name: 
          - gnupg
          - software-properties-common
        state: present

    - name: Add Jenkins repository key to the system
      apt_key:
        url: https://pkg.jenkins.io/debian/jenkins.io.key
        state: present

    - name: Add Jenkins repository to the sources list
      apt_repository:
        repo: deb http://pkg.jenkins.io/debian-stable binary/
        state: present

    - name: Update apt package index (again)
      apt:
        update_cache: yes

    - name: Install Jenkins
      apt:
        name: jenkins
        state: present

    - name: Ensure Jenkins is started and enabled on boot
      systemd:
        name: jenkins
        enabled: yes
        state: started

Playbook for Installing Docker

---
- name: Install Docker
  hosts: servers
  become: yes

  tasks:
    - name: Update the apt package index
      apt:
        update_cache: yes

    - name: Install Docker
      apt:
        name: 
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common
        state: present

    - name: Add Docker GPG key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add Docker repository
      apt_repository:
        repo: deb https://download.docker.com/linux/ubuntu focal stable
        state: present

    - name: Update the apt package index again
      apt:
        update_cache: yes

    - name: Install Docker CE
      apt:
        name: docker-ce
        state: present

 ANSIBLE

Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It is designed to be simple to set up and use, yet powerful enough to manage complex deployments and IT environments. Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator or devops professionals does on a weekly or daily basis. Ansible doesn't depend on agent software and has no additional security infrastructure, so it's easy to deploy.

How Ansible works

In Ansible, there are two categories of computers: the control node and managed nodes. The control node is a computer that runs Ansible. There must be at least one control node, although a backup control node may also exist. A managed node is any device being managed by the control node.

Ansible works by connecting to nodes (clients, servers, or whatever you're configuring) on a network, and then sending a small program called an Ansible module to that node. Ansible executes these modules over SSH and removes them when finished. The only requirement for this interaction is that your Ansible control node has login access to the managed nodes. SSH keys are the most common way to provide access, but other forms of authentication are also supported.

 

Basic Concepts:

1.      Playbooks:

o    Playbooks are YAML files where you define your automation tasks. Each playbook consists of one or more plays, and each play applies a set of tasks to a group of hosts.

2.      Inventory:

o    The inventory is a file that defines the hosts and groups of hosts upon which Ansible will operate. It can be a static file or dynamically generated.

3.      Tasks:

o    Tasks are the actions executed by Ansible modules within a play. Each task typically calls a module to perform a specific function


1.      Roles:

o    Roles are a way to organize playbooks and tasks into reusable components. Each role can include variables, tasks, files, templates, and handlers.

2.      Variables:

o    Variables are used to store values that can be reused throughout playbooks and roles. They make playbooks more flexible and easier to maintain.

3.      Handlers:

o    Handlers are similar to tasks but are only run when notified. They are often used to restart services after a configuration change.

How Ansible Works:

  1. Define Inventory:
    • Create an inventory file listing all the hosts you want to manage.
  2. Write Playbooks:
    • Write playbooks to define the tasks and configurations to be applied to the hosts.
  3. Run Playbooks:
    • Execute the playbooks using the ansible-playbook command to apply the configurations to the managed hosts.

 

Ansible Simplified

In layman terms, Ansible is a tool that helps you automate tasks on multiple computers. It's like having a remote control for your computers, allowing you to do things like installing software, updating settings, and starting or stopping services without having to manually log into each computer and do it yourself.

Here’s a simple analogy:

Ansible as a Remote Control for Your Computers:

  1. Imagine You Have Multiple Computers:
    • Let’s say you have a bunch of computers, and you need to install a program on all of them. Doing it manually on each one would take a lot of time.
  2. Using a Remote Control:
    • Instead of going to each computer, you use a remote control (Ansible) to send instructions to all of them at once. You write down what you want to do (like installing a program) in a simple script.
  3. Script as a Set of Instructions:
    • This script, called a playbook, is written in plain English-like language so it’s easy to understand. It lists all the steps that need to be done.
  4. Sending Instructions:
    • With one click, Ansible sends these instructions to all the computers. It uses a built-in communication method (like SSH) to connect to each computer and run the instructions.
  5. No Special Setup Needed:
    • The computers don’t need any special software installed to receive these instructions, just a way to communicate (like SSH, which is usually already set up).
  6. Consistency and Efficiency:
    • This way, you ensure that all your computers are set up exactly the same way without having to repeat the process manually, saving you a lot of time and effort.

 


Thursday, July 18, 2024

  Steps to install terraform on Ubuntu / Ubuntu cloud server :


 1.       Install unzip

sudo apt-get install unzip

2.  Download latest version of the terraform (substituting newer version number if needed)

wget https://releases.hashicorp.com/terraform/1.0.7/terraform_1.0.7_linux_amd64.zip

3.  Extract the downloaded file archive

unzip terraform_1.0.7_linux_amd64.zip

4. Move the executable into a directory searched for executables

sudo mv terraform /usr/local/bin/

5. Run it

terraform --version


CREATE EC2 WITH TERRAFORM

To create an EC2 instance with Terraform using the provided AWS credentials, follow these steps. Be sure to replace the placeholder values with your actual credentials and ensure your credentials are kept secure.

Step 1: Setup Terraform Project Directory

  1. Create a Project Directory:

mkdir my-ec2-instance

cd my-ec2-instance

 

2.      Create main.tf File: Open your favorite text editor and create a file named main.tf.

Step 2: Define the AWS Provider and EC2 Instance

In the main.tf file, define the AWS provider and the EC2 instance resource:

# main.tf

# Define the AWS provider
provider "aws" {
  region     = "us-east-1"
  access_key = "AKIAU6GDXLT5RR3WZ755"
  secret_key = "4pllcY6LbEkPJHCpqwZUUq7C6+dqui6HDQY1srcF"
}

# Define the EC2 instance resource
resource "aws_instance" "example" {
  ami           = "ami-04b70fa74e45c3917"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleInstance"
  }
}


2.       Initialize Terraform: Initialize your Terraform configuration. This step downloads the necessary providers.

terraform init

3.       Plan the Configuration: Create an execution plan, which lets you preview the changes that Terraform will make to your infrastructure.

terraform plan

4.       Apply the Configuration: Apply the configuration to create the EC2 instance.

terraform apply

5.       Confirm Apply: When prompted, type yes to confirm the apply step.

 

After testing, if you want to delete the created resources, you can use:

terraform destroy

This will remove all the resources defined in your Terraform configuration.

This basic setup gets you started with Terraform and AWS EC2 instances. You can expand and customize it as needed for your specific requirements.


Multiple instances

# Define the EC2 instance resource

resource "aws_instance" "example" {

  count         = 5

  ami           = "ami-0862be96e41dcbf74"

  instance_type = "t2.micro"

 

  tags = {

    Name = "ExampleInstance-${count.index}"

  }

}


Thursday, July 11, 2024

TERRAFORM

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows you to define and provision data center infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. Terraform is designed to create, manage, and update infrastructure resources such as virtual machines, networks, and storage in a reproducible and automated manner.

WHY USE TERRAFORM:

  1. Infrastructure as Code (IaC):
    • Terraform enables you to write code to define and manage your infrastructure. This code can be versioned, reviewed, and shared, making your infrastructure setup transparent and consistent.
  2. Declarative Configuration:
    • You declare the desired state of your infrastructure, and Terraform takes care of the rest. For example, if you need an AWS EC2 instance, you specify it in the configuration, and Terraform ensures it is created as described.
  3. Resource Management:
    • Terraform manages the lifecycle of infrastructure resources. This includes creating, updating, and deleting resources as specified in the configuration files.
  4. Execution Plans:
    • Before applying changes, Terraform generates an execution plan that shows what actions will be taken to reach the desired state. This helps in understanding and validating the changes before they are applied.
  5. State Management:
    • Terraform keeps track of the state of your infrastructure using a state file. This file is used to map your configuration to the real-world resources, ensuring consistency and enabling Terraform to determine what changes need to be made.
  6. Provisioning Across Multiple Providers:
    • Terraform supports a wide range of cloud providers and services, including AWS, Azure, Google Cloud, Kubernetes, and many others. This allows you to manage infrastructure across multiple platforms using a single tool.

7.      Standardize configurations

Terraform supports reusable configuration components called modules that define configurable collections of infrastructure, saving time and encouraging best practices. You can use publicly available modules from the Terraform Registry, or write your own.

The core Terraform workflow consists of three stages:

·        Terraform Init

            Terraform init is the command used to initialize a Terraform working directory. This command              sets up the environment and prepares it for further actions.

·         Plan: Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration.

·         Apply: On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies. For example, if you update the properties of a VPC and change the number of virtual machines in that VPC, Terraform will recreate the VPC before scaling the virtual machines.




UNDERSTANDING TERRAFORM

Imagine You are Building a House

  1. Blueprints (Terraform Configuration Files):
    • Just like you need blueprints to build a house, you use Terraform to write down the plans for your infrastructure. This plan includes all the details about what your infrastructure should look like, such as the number of servers (like rooms), databases (like plumbing), and networking settings (like electrical wiring).
  2. The Builder (Terraform Tool):
    • Terraform is like a smart builder who reads your blueprints and constructs the house exactly as you've planned. It takes care of all the details and makes sure everything is put together correctly.
  3. Step-by-Step Plan (Execution Plan):
    • Before the builder starts working, they show you a detailed plan of what they will do. This is Terraform’s "execution plan," which tells you what changes it will make to your infrastructure.
  4. Construction (Applying Changes):
    • Once you approve the plan, the builder starts constructing your house. Similarly, when you run terraform apply, Terraform creates or updates your infrastructure based on the blueprint.
  5. State of the House (State Management):
    • Terraform keeps track of the current state of your house. If you want to make changes later, it knows exactly what the house looks like now and what needs to be changed to match the new blueprints.
  6. Multiple Builders (Providers):
    • Terraform can work with many different builders (cloud providers) like AWS, Google Cloud, and Azure. It can build parts of your house (infrastructure) in different places using the same set of blueprints.

7.     Understanding Modules

In Terraform, modules are like reusable blueprints or building blocks. Instead of writing the same detailed plan for every house you build, you can create a module once and reuse it whenever you need to build that part of the house.


  PROMETHEUS AND GRAFANA A robust performance monitoring and alerting stack is crucial to service reliability. Cloud Native Computing Foun...