Skip to content

About

Multi-distro infrastructure automation using Vagrant and Ansible — provisions and configures web, database, and file servers across Rocky Linux and Ubuntu environments.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

14 Commits

Folders and files

Repository files navigation

ansible-learnlinux

A multi-distro infrastructure automation project using Vagrant and Ansible to provision and configure a realistic multi-tier server environment across Rocky Linux and Ubuntu systems.

Built as a hands-on learning project demonstrating infrastructure-as-code principles, role-based Ansible structure, and cross-distribution configuration management.


Architecture

Four virtual machines are provisioned automatically, each assigned a dedicated role:

Hostname IP OS Role
ubuntu01 192.168.56.10 Ubuntu 22.04 Web Server
ubuntu02 192.168.56.11 Ubuntu 22.04 Database Server
rocky01 192.168.56.12 Rocky Linux 8 Web Server
rocky02 192.168.56.13 Rocky Linux 8 File Server

The web tier serves a small PHP health application. Each web node connects to MariaDB on ubuntu02 with its own source-restricted, read-only database grant and mounts a read-only NFSv4 export from rocky02. MariaDB port 3306 and NFSv4 port 2049 accept traffic only from the two web-server addresses.

clients -> ubuntu01 / rocky01 (Apache + PHP)
                         |-> ubuntu02 (MariaDB)
                         `-> rocky02 (read-only NFS content)

Features

  • Multi-distro support — handles package management, service names, and configuration differences between Rocky Linux (dnf) and Ubuntu (apt) automatically
  • Role-based structure — clean separation of concerns across base, web, database, and file server roles
  • group_vars — variables organized by group rather than per-host, keeping configuration DRY and scalable
  • Idempotent playbooks — safe to run multiple times without unintended side effects
  • Hardened access — key-only SSH, validated SSH drop-ins, passwordless automation sudo, and default-deny host firewalls
  • Integrated tiers — PHP queries MariaDB and consumes read-only shared content over NFSv4
  • Least-privilege networking — database and NFS traffic is accepted only from the web nodes
  • Secret management — the application database password is supplied through Ansible Vault
  • Deployment verification — the playbook checks the application and both backend dependencies after convergence
  • Tagged tasks — run specific parts of the playbook without executing everything
  • Single entrypoint — site.yml orchestrates the full stack in the correct order

Prerequisites

  • Vagrant >= 2.3
  • VirtualBox >= 6.1
  • Ansible Core >= 2.16
  • An SSH key pair at ~/.ssh/ansible (see Setup below)
  • At least 4 GB of free RAM for the four 1 GB virtual machines

Configuration

Before running the playbooks, update group_vars/all.yml with your values:

admin_user: ansible
admin_ssh_key: "ssh-ed25519 YOUR_PUBLIC_KEY_HERE ansible"
server_admin_email: admin@example.com

The remaining addresses and application settings in that file match the default Vagrant topology. Update them together with the Vagrantfile and inventory if you change the lab network.

To generate an SSH key for Ansible:

ssh-keygen -t ed25519 -C "ansible" -f ~/.ssh/ansible

Setup

1. Clone the repository:

git clone https://github.com/zemation/ansible-learnlinux.git
cd ansible-learnlinux

2. Update group_vars/all.yml with your SSH public key and email as shown above.

3. Start the VMs:

vagrant up

4. Bootstrap the environment — creates the ansible service account, deploys the SSH key, and configures sudoers:

vagrant ssh-config > .vagrant/ssh-config
ansible-playbook bootstrap.yml \
  -i 'ubuntu01,ubuntu02,rocky01,rocky02,' \
  -u vagrant \
  --ssh-common-args='-F .vagrant/ssh-config'

This one-time command uses Vagrant's generated SSH settings because the ansible account does not exist until bootstrap completes.

5. Install the required Ansible collections:

ansible-galaxy collection install -r requirements.yml

6. Create the encrypted application secret:

ansible-vault create group_vars/vault.yml

Add a password of at least 20 characters to the encrypted file:

vault_app_db_password: "replace-with-a-long-random-password"

The Vault file is ignored by Git. Back it up securely; Ansible uses this value for the MariaDB application accounts and protected PHP configuration.

7. Run the full playbook:

ansible-playbook site.yml --ask-vault-pass

Project Structure

ansible-learnlinux/
├── ansible.cfg               # Ansible configuration (inventory, remote user, key)
├── Vagrantfile               # VM definitions (4 nodes, Rocky + Ubuntu)
├── inventory                 # Host groups (web_servers, db_servers, file_servers)
├── bootstrap.yml             # Initial setup — service account, SSH key, sudoers
├── site.yml                  # Main playbook — applies all roles in order
├── requirements.yml          # Required Ansible collections
├── group_vars/
│   ├── all.yml               # Topology, application, and administrator variables
│   ├── vault.yml             # Encrypted database password (local, Git-ignored)
│   ├── web_servers.yml       # Apache, PHP, and NFS client variables
│   ├── db_servers.yml        # MariaDB network variables
│   └── file_servers.yml      # Samba and NFS variables
└── roles/
    ├── base/                 # SSH hardening and host firewall baseline
    ├── web_servers/          # Apache, PHP application, and NFS client
    ├── db_servers/           # MariaDB hardening, schema, and application accounts
    └── file_servers/         # Hardened Samba share and restricted NFS export

Usage

Run the full stack:

ansible-playbook site.yml --ask-vault-pass

Run only specific roles using tags:

ansible-playbook site.yml --ask-vault-pass --tags apache  # Apache/PHP tasks
ansible-playbook site.yml --ask-vault-pass --tags db      # Database tasks
ansible-playbook site.yml --ask-vault-pass --tags samba   # Samba tasks
ansible-playbook site.yml --ask-vault-pass --tags ssh     # SSH policy
ansible-playbook site.yml --ask-vault-pass --tags verify  # Re-run app checks

Run the complete playbook once before using narrow tags. The application tasks depend on the database and file tiers already being configured.

Target a specific host:

ansible-playbook site.yml --ask-vault-pass --limit 192.168.56.10

Limiting a run does not provision omitted dependencies. Use --limit for maintenance only after a successful full-stack deployment.

Verify the application manually:

curl --fail http://192.168.56.10/
curl --fail http://192.168.56.12/

The application returns HTTP 200 only when MariaDB and NFS are both reachable; otherwise it returns HTTP 503. The playbook performs this check automatically on each web node after a full run.

Tear down the environment:

vagrant destroy -f

Roles

base

Applied to all hosts. Deploys the Ansible service account SSH key, enforces key-only SSH with a validated configuration drop-in, and enables a default-deny host firewall while preserving SSH access.

web_servers

Installs Apache, PHP, the MariaDB PHP driver, and an NFS client. Each web node mounts the shared file tier read-only and serves a status application that queries MariaDB and reads shared content. It returns HTTP 200 when both backend tiers respond and HTTP 503 when either dependency is unavailable.

db_servers

Installs MariaDB using the correct package name for each distribution (mariadb-server on both current targets), removes anonymous accounts and the test database, restricts remote root access, creates the application schema, and grants each web address read-only access. Port 3306 is accepted only from the web hosts.

file_servers

Installs Samba with SMB1, guest, and anonymous access disabled. Creates a private group-controlled share and permits Samba traffic only from the lab's private network. Add intended users to the sambashare Unix group and create their Samba passwords before using the share.

The same shared directory is exported read-only over NFSv4 to the two web servers, allowing the application tier to consume file-tier content without sharing Samba credentials.

No Samba login is created automatically. To use the interactive Samba share, create or select a Unix account on the file server, add it to sambashare, and set a separate Samba password with smbpasswd -a USER. NFS access used by the web application does not require a Samba account.

Security Notes

  • SSH permits only the vagrant and configured administrator accounts, using public keys.
  • Root SSH login, password authentication, X11, agent forwarding, and TCP forwarding are disabled.
  • The vagrant account remains allowed as a recovery path for this lab.
  • HTTP is configured for the demonstration application. Port 443 is allowed by the firewall, but TLS certificates and an HTTPS virtual host are not configured yet.
  • The Vault password protects decryption of group_vars/vault.yml; keep both the Vault password and encrypted file backed up securely.
  • The NFS export is read-only and restricted by source IP. Treat the Vagrant private network as a trusted lab network.

Roadmap

  • Upgrade Rocky Linux boxes to Rocky 9
  • Wire web servers to the database and shared file tiers
  • Configure TLS for the web tier
  • Add HAProxy load balancer node
  • Add Kafka message broker as optional extension
  • Add centralized logging (ELK/EFK) as optional extension

License

MIT

About

Multi-distro infrastructure automation using Vagrant and Ansible — provisions and configures web, database, and file servers across Rocky Linux and Ubuntu environments.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages