Skip to content

ale900522/ansible-learn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ansible Lab with Vagrant

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.

Table of Contents

Features

  • Define and launch multiple Vagrant VMs with a single command.
  • Prepare VMs for Ansible with init-nodes and ssh-copy scripts.
  • Automatic provisioning using Ansible playbooks.
  • Customizable inventory and roles for varied use cases.
  • Ideal for Ansible labs, workshops, and proof-of-concept environments.

Prerequisites

  • Vagrant (>= 2.2.0)
  • VirtualBox or another Vagrant-supported provider
  • Ansible (>= 2.9)
  • Git
  • SSH key configured for passwordless access

Getting Started

Cloning the Repository

git clone https://github.com/yourusername/ansible-vagrant-lab.git
cd ansible-vagrant-lab

Launching and Provisioning

Bring up and provision all Vagrant machines in one step:

vagrant up

Under the hood, Vagrant will:

  1. Create the virtual machines as defined in the Vagrantfile.
  2. Run ssh-copy to install ssh-keys into VM and packages and configure each VM for Ansible.
  3. Invoke the Ansible provisioner to apply playbooks from the ansible/ directory.

Re-provisioning VMs

If you update playbooks or role definitions, re-run the Ansible provisioning without destroying VMs:

vagrant provision

To rebuild from scratch:

vagrant destroy -f && vagrant up

Project Structure

├── 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

Helper Scripts

ssh-copy

Distributes your SSH public key to each VM for passwordless access:

chmod +x ssh-copy
./ssh-copy

It uses Vagrant’s SSH wrapper and ssh-copy-id under the hood.

init-nodes

Prepares each VM for Ansible:

  • Installs required packages (e.g., Python, SSH)
  • Configures network settings
  • Sets hostnames

Ansible Playbooks

  • 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.

Troubleshooting

  • 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.

Contributing

Contributions are welcome! Please open an issue or submit a pull request:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m "Add feature").
  4. Push to your branch (git push origin feature-name).
  5. Open a pull request detailing your enhancements.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages