Skip to content

This repository demonstrates using Ansible with Vagrant for automating virtual machine setup and configuration.

License

Notifications You must be signed in to change notification settings

t-sorger/ansible-vagrant-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ansible-vagrant-demo

Instalation

  1. Vagrant 2.4.1
  2. VirtualBox 7.0.20
  3. Ansible 2.17.4

Commands

Vagrant Commands

  1. Initalize the Vagrantfile. This will generate a file called Vagrantfile.

    vagrant init --minimal
  2. Copy the prepared Vargantfile_Prepared into the generated Vagrantfile:

    cp Vargantfile_Prepared Vagrantfile
  3. Spin up the VMs defined in the Vagrantfile:

    vagrant up
  4. Destroy VMs:

    vagrant destroy -f

Ansible Commands

  1. Run the Ansible playbook command using the specified configuration.
    ANSIBLE_CONFIG=./ansible/ansible.cfg ansible-playbook ./ansible/main.yml

Demo

This demo shows multiple Ansible features:

  • Playbooks
  • Roles
  • Profiling
  • Conditionals

Script

  1. Spin up VMs

    vagrant up
  2. Configure VMs using Ansible

    ANSIBLE_CONFIG=./ansible/ansible.cfg ansible-playbook ansible/main.yml
  3. Check out web servers using a script:

    ./scripts/open_ips_in_chrome.sh
    
  4. Change ./ansible/main.yml to use the Nginx role.

    - name: Nginx Role for Dev and Prod
      include_role:
        name: nginx
    
  5. Rerun Ansible playbook

    ANSIBLE_CONFIG=./ansible/ansible.cfg ansible-playbook ansible/main.yml
  6. While playbook is running add profiling to ansible.cfg:

    callbacks_enabled = timer, profile_tasks, profile_roles
    
  7. Show that web servers are still configured the same.

  8. Add conditionals in ./ansible/main.yml:

    - name: Nginx Role for Dev and Prod
      include_role:
        name: nginx
      when: inventory_hostname != "infra"
    
    - name: Apache Role for Infra
      include_role:
        name: apache
      when: inventory_hostname == "infra"
    
  9. Show that only infra is now using Apache as web server.

About

This repository demonstrates using Ansible with Vagrant for automating virtual machine setup and configuration.

Topics

Resources

License

Stars

Watchers

Forks