An open API service indexing awesome lists of open source software.

https://github.com/javadtorabikh/devopsansible

A hybrid intelligent system for automated web cluster management and backup on VMware ESXi
https://github.com/javadtorabikh/devopsansible

admin-server ansible docker gitlab server

Last synced: 6 months ago
JSON representation

A hybrid intelligent system for automated web cluster management and backup on VMware ESXi

Awesome Lists containing this project

README

          

# Ansible Infrastructure Automation ๐Ÿš€

Welcome to the Ansible Infrastructure Automation project! This repository provides a robust framework for managing server infrastructure using Ansible, integrated with a GitLab CI/CD pipeline for automated linting, testing, and deployment. Whether you're setting up Docker, installing base packages, or configuring DNS, this project has you covered with a modular and scalable design.

![Ansible Logo](images.jpeg)

## โœจ Features

- Dynamic Inventory: Uses a Python script (env_inventory.py) to generate server lists from environment variables, eliminating static host files.
- Modular Roles: Organized roles for Docker and base package installation, with Molecule tests for validation.
- CI/CD Pipeline: Automated linting, testing, planning, and applying changes via GitLab CI.
- DNS Management: Dedicated playbook for DNS configuration.
- Error Handling: Retries and artifact logging for robust pipeline execution.

## ๐Ÿ“‹ Prerequisites
To use this project, ensure the following are set up:

1. Python 3 and pip:

- Install Python 3 and pip:
```bash
dnf install python3-pip # For Rocky Linux
apt install python3-pip # For Ubuntu
```
2. Docker Engine:

- Install Docker CE following the official instructions: https://docs.docker.com/engine/install/

3. SSH Key Distribution:
- Copy the runner's public SSH key to managed servers:

```bash
Copy the runner's public SSH key to managed servers:
cat ~/.ssh/id_rsa.pub | ssh @ 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'
```

4. GitLab Environment Variables:

- Define server details in GitLab CI/CD Settings โ†’ Variables (e.g., SERVER1_DATA, SERVER2_DATA).
- Format: See ansible/inventory/env_inventory.py for details.

## ๐Ÿ—‚๏ธ Repository Structure
```plain
DevOpsAnsible/
โ”œโ”€โ”€ .gitlab-ci.yml # Enhanced CI/CD pipeline
โ”œโ”€โ”€ README.md # Updated documentation
โ”œโ”€โ”€ CHANGELOG.md # Version history
โ”œโ”€โ”€ LICENSE # MIT/Apache license
โ”œโ”€โ”€ .ansible-lint # Linting rules
โ”œโ”€โ”€ requirements.txt # Python deps
โ”œโ”€โ”€ tests/ # Test directory
โ”‚ โ”œโ”€โ”€ molecule/ # Molecule tests
โ”‚ โ””โ”€โ”€ test-requirements.txt # Test dependencies
โ””โ”€โ”€ ansible/
โ”œโ”€โ”€ ansible.cfg # Config
โ”œโ”€โ”€ requirements.yml # Galaxy roles
โ”œโ”€โ”€ vault.yml # Encrypted secrets
โ”œโ”€โ”€ inventory/
โ”‚ โ”œโ”€โ”€ production/
โ”‚ โ”‚ โ”œโ”€โ”€ hosts # Prod hosts
โ”‚ โ”‚ โ””โ”€โ”€ group_vars/
โ”‚ โ”‚ โ”œโ”€โ”€ all.yml # Common vars
โ”‚ โ”‚ โ”œโ”€โ”€ docker.yml # Docker-specific
โ”‚ โ”‚ โ””โ”€โ”€ gitlab.yml # GitLab-specific
โ”‚ โ”œโ”€โ”€ staging/
โ”‚ โ”‚ โ”œโ”€โ”€ hosts
โ”‚ โ”‚ โ””โ”€โ”€ group_vars/
โ”‚ โ””โ”€โ”€ env_inventory.py # Dynamic inventory
โ”œโ”€โ”€ playbooks/
โ”‚ โ”œโ”€โ”€ site.yml # Master playbook
โ”‚ โ”œโ”€โ”€ docker.yml # Docker setup
โ”‚ โ”œโ”€โ”€ gitlab.yml # GitLab setup
โ”‚ โ”œโ”€โ”€ monitoring.yml # Enhanced monitoring
โ”‚ โ”œโ”€โ”€ backups.yml # Backup system
โ”‚ โ”œโ”€โ”€ security.yml # Security hardening
โ”‚ โ””โ”€โ”€ maintenance.yml # Maintenance tasks
โ””โ”€โ”€ roles/
โ”œโ”€โ”€ common/ # Enhanced common
โ”‚ โ”œโ”€โ”€ tasks/
โ”‚ โ”‚ โ”œโ”€โ”€ main.yml
โ”‚ โ”‚ โ”œโ”€โ”€ packages.yml
โ”‚ โ”‚ โ”œโ”€โ”€ security.yml
โ”‚ โ”‚ โ”œโ”€โ”€ users.yml
โ”‚ โ”‚ โ””โ”€โ”€ tuning.yml
โ”‚ โ”œโ”€โ”€ handlers/
โ”‚ โ”œโ”€โ”€ templates/
โ”‚ โ””โ”€โ”€ defaults/
โ”œโ”€โ”€ docker/ # Enhanced Docker
โ”‚ โ”œโ”€โ”€ tasks/
โ”‚ โ”‚ โ”œโ”€โ”€ main.yml
โ”‚ โ”‚ โ”œโ”€โ”€ install.yml
โ”‚ โ”‚ โ”œโ”€โ”€ config.yml
โ”‚ โ”‚ โ”œโ”€โ”€ compose.yml # Docker Compose
โ”‚ โ”‚ โ””โ”€โ”€ networks.yml # Network config
โ”‚ โ”œโ”€โ”€ templates/
โ”‚ โ”‚ โ”œโ”€โ”€ daemon.json.j2
โ”‚ โ”‚ โ””โ”€โ”€ docker-compose.yml.j2
โ”‚ โ”œโ”€โ”€ defaults/
โ”‚ โ””โ”€โ”€ vars/
โ”œโ”€โ”€ gitlab/ # Enhanced GitLab
โ”‚ โ”œโ”€โ”€ tasks/
โ”‚ โ”‚ โ”œโ”€โ”€ main.yml
โ”‚ โ”‚ โ”œโ”€โ”€ install.yml
โ”‚ โ”‚ โ”œโ”€โ”€ config.yml
โ”‚ โ”‚ โ”œโ”€โ”€ runners.yml # CI runners
โ”‚ โ”‚ โ””โ”€โ”€ backup.yml # GitLab backup
โ”‚ โ”œโ”€โ”€ templates/
โ”‚ โ”‚ โ”œโ”€โ”€ gitlab.rb.j2
โ”‚ โ”‚ โ””โ”€โ”€ backup.sh.j2
โ”‚ โ”œโ”€โ”€ defaults/
โ”‚ โ””โ”€โ”€ vars/
โ”œโ”€โ”€ monitoring/ # Enhanced monitoring
โ”‚ โ”œโ”€โ”€ tasks/
โ”‚ โ”‚ โ”œโ”€โ”€ main.yml
โ”‚ โ”‚ โ”œโ”€โ”€ node_exporter.yml
โ”‚ โ”‚ โ”œโ”€โ”€ cadvisor.yml # Container monitoring
โ”‚ โ”‚ โ””โ”€โ”€ alerts.yml # Alert rules
โ”‚ โ”œโ”€โ”€ templates/
โ”‚ โ””โ”€โ”€ defaults/
โ”œโ”€โ”€ backups/ # Enhanced backups
โ”‚ โ”œโ”€โ”€ tasks/
โ”‚ โ”‚ โ”œโ”€โ”€ main.yml
โ”‚ โ”‚ โ”œโ”€โ”€ setup.yml
โ”‚ โ”‚ โ”œโ”€โ”€ gitlab.yml
โ”‚ โ”‚ โ””โ”€โ”€ docker.yml
โ”‚ โ”œโ”€โ”€ templates/
โ”‚ โ””โ”€โ”€ defaults/
โ””โ”€โ”€ security/ # New security role
โ”œโ”€โ”€ tasks/
โ”‚ โ”œโ”€โ”€ main.yml
โ”‚ โ”œโ”€โ”€ firewall.yml
โ”‚ โ”œโ”€โ”€ ssh_hardening.yml
โ”‚ โ””โ”€โ”€ audits.yml
โ”œโ”€โ”€ templates/
โ””โ”€โ”€ defaults/
```

## ๐Ÿš€ Getting Started

1. Clone the Repository:
```bash
git clone https://github.com/JavadTorabiKh/DevOpsAnsible.git
cd DevOpsAnsible
```

2. Set Up GitLab CI:

- Configure environment variables in GitLab CI/CD Settings โ†’ Variables.
- Ensure your runner has access to the target servers via SSH.

3. Run Locally (Optional):

- Install dependencies:
```bash
pip3 install --user ansible ansible-lint yamllint molecule molecule-docker docker
ansible-galaxy install -r ansible/requirements.yml
```

4. Run the main playbook:
```bash
ansible-playbook ansible/playbooks/main.yml -i ansible/inventory/env_inventory.py
```

## ๐Ÿ› ๏ธ CI/CD Pipeline
The GitLab CI pipeline automates the following stages:

| Stage | Description |
|---------|---------|
| lint | Runs yamllint and ansible-lint to ensure code quality. |
| test | Executes Molecule tests for roles in parallel (e.g., docker, packages_base). |
| check_config | Simulates and applies DNS configuration changes. |
| plan | Simulates changes for the main playbook (--check --diff). |
| apply | Applies changes manually to target servers. |

## Artifacts

- Logs are stored as artifacts for 1 week to debug failures.
- Use the GitLab UI to trigger the apply stage manually.

## ๐Ÿงช Testing with Molecule
Each role (docker, packages_base) includes Molecule tests:

- Navigate to ansible/roles//molecule/default/.

- Run tests:
```bash
cd ansible/roles/docker
molecule test
```

## ๐Ÿ› ๏ธ Customization

- Add New Roles: Create a new directory under ansible/roles/ with tasks/main.yml and defaults/main.yml.
- Extend Inventory: Update env_inventory.py to support additional server attributes.
- Modify Pipeline: Adjust .gitlab-ci.yml to add new stages or jobs.

## ๐Ÿ“ Notes

- Ensure environment variables are securely stored in GitLab.
- Review ansible.cfg for custom Ansible settings (e.g., timeouts, SSH options).
- For large deployments, consider using a custom Docker image with pre-installed dependencies to reduce pipeline runtime.

## ๐Ÿค Contributing
Contributions are welcome! Please:

1. Fork the repository.
2. Create a feature branch (git checkout -b feature/awesome-feature).
3. Commit changes (git commit -m 'Add awesome feature').
4. Push to the branch (git push origin feature/awesome-feature).
5. Open a pull request.

## ๐Ÿ“ง Contact
For questions or support, reach out via [GitLab Issues](/issues) or [email](javadtorabi462@gmail.com).

---

Powered by Ansible and GitLab CI/CD