This repository provides a reproducible multi-node environment using Vagrant and Ansible. It enables you to spin up virtual machines, prepare them with helper scripts, and automatically provision them with Ansible playbooks—ideal for learning, testing, and demonstrating infrastructure-as-code practices.
- Features
- Prerequisites
- Getting Started
- Project Structure
- Helper Scripts
- Ansible Playbooks
- Troubleshooting
- Contributing
- License
- Define and launch multiple Vagrant VMs with a single command.
- Prepare VMs for Ansible with
init-nodesandssh-copyscripts. - Automatic provisioning using Ansible playbooks.
- Customizable inventory and roles for varied use cases.
- Ideal for Ansible labs, workshops, and proof-of-concept environments.
- Vagrant (>= 2.2.0)
- VirtualBox or another Vagrant-supported provider
- Ansible (>= 2.9)
- Git
- SSH key configured for passwordless access
git clone https://github.com/yourusername/ansible-vagrant-lab.git
cd ansible-vagrant-labBring up and provision all Vagrant machines in one step:
vagrant upUnder the hood, Vagrant will:
- Create the virtual machines as defined in the
Vagrantfile. - Run
ssh-copyto install ssh-keys into VM and packages and configure each VM for Ansible. - Invoke the Ansible provisioner to apply playbooks from the
ansible/directory.
If you update playbooks or role definitions, re-run the Ansible provisioning without destroying VMs:
vagrant provisionTo rebuild from scratch:
vagrant destroy -f && vagrant up├── Vagrantfile # Defines VM count, networking, and Ansible provisioner
├── ansible/
│ ├── inventory.yaml # Vagrant-generated inventory for Ansible
│ ├── playbook.yml # Main playbook to configure all nodes
│ └── roles/ # Directory for Ansible roles
│ └── example_role/
│ ├── tasks/
│ └── templates/
├── init-nodes # Script to prepare VMs before provisioning
├── ssh-copy # Script to copy SSH keys and finalize VM setup
└── README.md # Project documentation
Distributes your SSH public key to each VM for passwordless access:
chmod +x ssh-copy
./ssh-copyIt uses Vagrant’s SSH wrapper and ssh-copy-id under the hood.
Prepares each VM for Ansible:
- Installs required packages (e.g., Python, SSH)
- Configures network settings
- Sets hostnames
- inventory.ini: Dynamically generated by Vagrant; lists all VM hostnames and IPs.
- playbook.yml: Entry point to apply roles and tasks across nodes.
- roles/: Modular configuration sets; each role encapsulates a service or configuration.
Customize or add new roles under ansible/roles, then reference them in playbook.yml.
- Ansible connection failures: Ensure SSH agent forwarding is enabled and your key is added (
ssh-add). - Playbook errors: Run Ansible locally for debugging:
ansible-playbook -i ansible/inventory.ini ansible/playbook.yml --check
- VM networking issues: Verify no port conflicts and that the host machine allows virtualization networking.
Contributions are welcome! Please open an issue or submit a pull request:
- Fork the repository.
- Create a feature branch (
git checkout -b feature-name). - Commit your changes (
git commit -m "Add feature"). - Push to your branch (
git push origin feature-name). - Open a pull request detailing your enhancements.
This project is licensed under the MIT License. See the LICENSE file for details.