|
| 1 | +# Ansible DevSecOps Playbooks |
| 2 | + |
| 3 | +This repository contains example Ansible playbooks designed to automate security tasks within a DevSecOps pipeline. These playbooks demonstrate how to integrate security checks and hardening procedures into your infrastructure automation process. |
| 4 | + |
| 5 | +<p align="center"> |
| 6 | + <img src="https://github.com/user-attachments/assets/a5169df1-bb06-42e2-9623-9ce33df20d75" alt="Sublime's custom image"/> |
| 7 | +</p> |
| 8 | + |
| 9 | +## What is an Ansible Playbook? |
| 10 | + |
| 11 | +An Ansible Playbook is a YAML file that defines a set of automation tasks, configurations, and policies to be enforced on remote hosts. It allows you to describe your desired state for infrastructure and applications in a code-like manner, making it versionable, repeatable, and idempotent (meaning you can run it multiple times without causing unintended changes). |
| 12 | + |
| 13 | +## Playbooks Overview |
| 14 | + |
| 15 | +### 1. Host Hardening Playbook (`host-hardening.yml`) |
| 16 | + |
| 17 | +This playbook performs basic security hardening on a Linux server by: |
| 18 | +- Disabling SSH root login. |
| 19 | +- Restricting SSH access to specific users. |
| 20 | +- Disabling weak SSH encryption algorithms. |
| 21 | +- Installing and configuring `fail2ban` to protect against brute-force attacks. |
| 22 | +- Configuring a firewall (UFW) to allow only essential ports (SSH, HTTP, HTTPS). |
| 23 | + |
| 24 | +### 2. Container & Dependency Scan Playbook (`container-dependency-scan.yml`) |
| 25 | + |
| 26 | +This playbook is designed to run in a CI/CD pipeline and performs security scans on: |
| 27 | +- **Docker Images**: Uses `Trivy` to scan container images for known vulnerabilities (CVEs), failing the pipeline if critical issues are found. |
| 28 | +- **Python Dependencies**: Uses `Safety` to check for vulnerabilities in Python packages listed in a `requirements.txt` file. |
| 29 | + |
| 30 | +## Prerequisites |
| 31 | + |
| 32 | +- Ansible installed on the control node. |
| 33 | +- For `host-hardening.yml`: Target hosts must be accessible via SSH. |
| 34 | +- For `container-dependency-scan.yml`: Docker and Python/pip must be installed on the runner. |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +### 1. Clone the Repository |
| 39 | +```bash |
| 40 | +git clone https://github.com/D3One/Vault-backup-automation/tree/main/Ansible%20samples |
| 41 | +cd ansible-devsecops-playbooks |
| 42 | +``` |
| 43 | + |
| 44 | +### 2. Create an Inventory File |
| 45 | +Create a file named `inventory.ini` to define your target hosts: |
| 46 | +```ini |
| 47 | +[webservers] |
| 48 | +web-server-1 ansible_host=192.168.1.10 |
| 49 | +web-server-2 ansible_host=192.168.1.11 |
| 50 | + |
| 51 | +[local] |
| 52 | +localhost ansible_connection=local |
| 53 | +``` |
| 54 | + |
| 55 | +### 3. Run the Host Hardening Playbook |
| 56 | +```bash |
| 57 | +ansible-playbook -i inventory.ini host-hardening.yml --user <your-username> --become |
| 58 | +``` |
| 59 | + |
| 60 | +### 4. Run the Security Scan Playbook (in CI/CD) |
| 61 | +Example command to run the scan locally: |
| 62 | +```bash |
| 63 | +ansible-playbook -i inventory.ini container-dependency-scan.yml -e "image_name=my-app:latest requirements_path=./requirements.txt" |
| 64 | +``` |
| 65 | + |
| 66 | +## Example Integration in GitLab CI |
| 67 | + |
| 68 | +```yaml |
| 69 | +stages: |
| 70 | + - security check |
| 71 | + |
| 72 | +trivy_scan: |
| 73 | + stage: security check |
| 74 | + image: docker:latest |
| 75 | + services: |
| 76 | + - docker:dind |
| 77 | + before_script: |
| 78 | + - apk add --no-cache ansible |
| 79 | + script: |
| 80 | + - ansible-playbook -i localhost, -c local container-dependency-scan.yml |
| 81 | + artifacts: |
| 82 | + paths: |
| 83 | + - ./*security-scan-report*.txt |
| 84 | +``` |
| 85 | +
|
| 86 | +## Key Benefits |
| 87 | +
|
| 88 | +- **Infrastructure as Code (IaC)**: Security policies are defined in code, versioned, and reviewable. |
| 89 | +- **Automation**: Eliminates manual security checks, reducing human error. |
| 90 | +- **Shift-Left Security**: Identifies vulnerabilities early in the development lifecycle. |
| 91 | +- **Compliance**: Helps enforce consistent security configurations across all environments. |
| 92 | +
|
| 93 | +## Contributing |
| 94 | +
|
| 95 | +Feel free to submit issues, fork the repository, and create pull requests to improve these playbooks. |
| 96 | +
|
| 97 | +## License |
| 98 | +
|
| 99 | +This project is licensed under the MIT License. |
| 100 | +
|
| 101 | +--- |
| 102 | +
|
0 commit comments