Skip to content

LondheShubham153/terraform-ansible

Repository files navigation

Terraform Ansible

Overview

This project integrates Terraform and Ansible to automate infrastructure provisioning and configuration management. Terraform is used to provision the required servers, and Ansible is used for configuring them.

Prerequisites

Before you begin, ensure you have the following installed on your local machine:

Installation

1. Update and Install Ansible

sudo apt-get update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
ansible --version  # Verify installation

2. Configure Ansible Inventory

Edit the hosts file to define the managed servers:

sudo vim /etc/ansible/hosts

Add your servers under a group:

[junoon_servers]
server1 ansible_host=IP_ADDRESS ansible_user=ubuntu ansible_ssh_private_key_file=/path/to/private/key
server2 ansible_host=IP_ADDRESS ansible_user=ubuntu ansible_ssh_private_key_file=/path/to/private/key

3. Create SSH Key and Set Permissions

mkdir ~/keys
cd ~/keys/
ssh-keygen -t rsa -b 4096 -f terra-key-ansible.pem
chmod 400 terra-key-ansible.pem

Ansible Commands

1. Check Server Connectivity

ansible junoon_servers -m ping

2. Execute Commands on Servers

ansible server1 -a "free -h"  # Check memory usage
ansible junoon_servers -a "df -h"  # Check disk space

3. Install Nginx Using Ansible

Create a playbook install_nginx.yml:

- name: Install Nginx
  hosts: junoon_servers
  become: yes
  tasks:
    - name: Update package cache
      apt:
        update_cache: yes
    - name: Install Nginx
      apt:
        name: nginx
        state: present

Run the playbook:

ansible-playbook install_nginx.yml

4. Install Docker Using Ansible

Create a playbook install_docker.yml:

- name: Install Docker
  hosts: junoon_servers
  become: yes
  tasks:
    - name: Install dependencies
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common
        state: present
    - name: Add Docker GPG key
      shell: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    - name: Add Docker repository
      shell: add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
    - name: Install Docker
      apt:
        name: docker-ce
        state: present

Run the playbook:

ansible-playbook install_docker.yml

Verify Docker installation:

ansible server1 -a "docker --version"

Inventory Management

To manage different environments, create an inventories/ directory:

mkdir inventories
cd inventories/
vim prd  # Production Inventory
vim dev  # Development Inventory

Example content for prd:

[junoon_servers]
server1 ansible_host=IP_ADDRESS ansible_user=ubuntu ansible_ssh_private_key_file=/path/to/private/key
server2 ansible_host=IP_ADDRESS ansible_user=ubuntu ansible_ssh_private_key_file=/path/to/private/key

Run playbook with specific inventory:

ansible-playbook -i ../inventories/prd install_docker.yml

Cloning the Repository

git clone https://github.com/LondheShubham153/terraform-ansible.git
mv playbooks/ terraform-ansible/

Conclusion

This setup helps in automating infrastructure provisioning using Terraform and Ansible. You can extend it further with more configurations and optimizations.


Maintainer: Shubham Londhe

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages