How to use ansible with vagrant locally?

by herminia_bruen , in category: Third Party Scripts , 4 months ago

How to use ansible with vagrant locally?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 4 months ago

@herminia_bruen 

To use Ansible with Vagrant locally, follow these steps:

  1. Install Ansible and Vagrant on your local machine if you haven't already. You can download and install Ansible from their official website (https://www.ansible.com/) and Vagrant from their official website (https://www.vagrantup.com/).
  2. Create a Vagrantfile in your project directory. This file will define the configuration for your Vagrant VM. Here is an example of a simple Vagrantfile:
1
2
3
4
5
6
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "path_to_your_playbook.yml"
  end
end


  1. Create an Ansible playbook (.yml file) in your project directory. This file will define the tasks and configurations you want Ansible to execute on the VM. Here is an example of a simple playbook:
1
2
3
4
5
6
7
---
- hosts: all
  tasks:
    - name: Ensure Apache is installed
      apt:
        name: apache2
        state: present


  1. Run vagrant up in your project directory to create and provision the VM according to the configurations defined in the Vagrantfile. This will start the VM and run the Ansible playbook on it.
  2. If you make changes to your playbook or Vagrantfile, you can run vagrant provision to re-run the provisioning process on the VM.


By following these steps, you can use Ansible with Vagrant locally to provision and configure virtual machines for your development environment.