Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kgoksal/ansible-server-playbooks
This repository contains Terraform and Ansible configurations to set up a practice environment on AWS EC2 instances. It includes Terraform files for provisioning resources and Ansible files for automation tasks.
https://github.com/kgoksal/ansible-server-playbooks
Last synced: about 1 month ago
JSON representation
This repository contains Terraform and Ansible configurations to set up a practice environment on AWS EC2 instances. It includes Terraform files for provisioning resources and Ansible files for automation tasks.
- Host: GitHub
- URL: https://github.com/kgoksal/ansible-server-playbooks
- Owner: KGoksal
- Created: 2024-07-22T20:50:20.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-07-27T20:42:45.000Z (5 months ago)
- Last Synced: 2024-07-27T23:48:07.694Z (5 months ago)
- Language: HCL
- Size: 75.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Terraform and Ansible Project for Infrastructure and Configuration Management
## OverviewThis project provides Terraform configurations to create AWS EC2 instances for Ansible practice and sample Ansible playbooks to perform common tasks on these instances.
### Terraform Files
- **main.tf**: Creates EC2 instances with Amazon Linux 2023 and Ubuntu 22.04, along with a security group allowing SSH (22) and HTTP (80) connections.
- **variables.tf**: Defines variables for instance tags, key name, user, AMIs, instance type, and AWS credentials.
- **inventory.txt**: Specifies the Ansible inventory with groups and variables.
- **ansible.cfg**: Configuration file for Ansible settings.### Ansible Playbooks
- **install_docker.yml**: Installs and configures Docker on servers.
- **setup_web_server.yml**: Sets up a web server using Apache.
- **create_user.yml**: Creates a user on servers.
- **install_nginx.yml**: Installs and configures Nginx.
- **install_mysql.yml**: Installs and configures MySQL.
- **install_python.yml**: Installs Python and pip on servers.## Prerequisites
- Terraform installed
- AWS CLI configured with appropriate credentials
- Ansible installed
- SSH key pair created and added to the AWS account# Usage
## Step 1: Terraform Setup1. **Initialize Terraform**
```bash
terraform init
terraform apply## Step 2: Ansible Setup
### Update Inventory File
Ensure the inventory.txt file contains the correct private IP addresses of the created EC2 instances.### Update Ansible Configuration
Make sure ansible.cfg is correctly configured.**Step 3: Running Ansible Playbooks**
**Connectivity Test**
```
ansible-playbook connectivity-test-playbook.yaml
```**Install Docker**
```
ansible-playbook install_docker.yaml
```**Copy File**
```
ansible-playbook copyfile-playbook.yaml
```**Create a User**
```
ansible-playbook create_user.yaml
```**Install and Configure Nginx**
```
ansible-playbook install_nginx.yaml
```**Install and Configure MySQL**
```
ansible-playbook install_mysql.yaml
```**Install Python and pip**
```
ansible-playbook install_python.yml
```************************************************************************************************
# Ansible Ad hoc Commands
## A. Install a package using apt/yum
### a. For CentOS/RHEL:
```
ansible all -b -m yum -a "name= state=present"
```
### b. For CentOS/RHEL:
```
ansible all -b -m apt -a "name= state=present"
```
## B. Other Commands:
- **Ping all hosts**
```
ansible all -m ping
```
- **Check free memory on all hosts**
```
ansible all -a "free -m"
```
- **Check uptime on all hosts**
```
ansible all -a "uptime"
```
- **Restart a service**
```
ansible -b -m service -a "name= state=restarted"
```
- **Run a shell command on all hosts**
```
ansible all -a "ls -l /path/to/directory"
```
- **Gather facts from all hosts**
```
ansible all -m setup
```
- **Create a new user on all hosts**
```
ansible all -b -m user -a "name= password= state=present"
```
- **Check disk space on all hosts**
```
ansible all -a "df -h"
```
**Notes:** The -b flag is used to become a superuser (equivalent to using sudo).
- **List all hosts**
```
ansible --list-hosts all
```
- **List all hosts other than web-servers**
```
ansible --list-hosts \!web-servers
```