How to pass ansible variables into vagrant?

by edmond_brakus , in category: Third Party Scripts , 5 months ago

How to pass ansible variables into vagrant?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 5 months ago

@edmond_brakus 

To pass Ansible variables into Vagrant, you can use the Vagrant Ansible provisioner. The Vagrant Ansible provisioner allows you to specify playbook paths, inventory files, extra variables, and other Ansible settings in your Vagrantfile.


Here is an example of how to pass Ansible variables into Vagrant using the Vagrant Ansible provisioner:

  1. Update your Vagrantfile to include the Ansible provisioner with the necessary configuration options. For example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Vagrant.configure("2") do |config|
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "path/to/playbook.yml"
    ansible.inventory_path = "path/to/inventory"
    ansible.extra_vars = {
      var1: "value1",
      var2: "value2"
    }
  end
end


  1. In your Ansible playbook, you can access these variables using the {{ var1 }} and {{ var2 }} syntax. For example:
1
2
3
4
5
6
---
- name: Example playbook
  hosts: all
  tasks:
    - debug:
        msg: "Value of var1 is {{ var1 }} and value of var2 is {{ var2 }}"


  1. Run vagrant up to start the VM and provision it using Ansible. The Ansible provisioner will pass the extra variables var1 and var2 to your playbook.


By following these steps, you can pass Ansible variables into Vagrant and use them in your Ansible playbooks to configure your virtual machines.