Search code examples
vagrant

Vagrant ansible: pick var fron environment variable


Here my ansible_local related Vagrantfile code:

config.vm.provision "ansible_local" do |ansible|
    ansible.become = true
    ansible.inventory_path = '/vagrant/provisioning/inventory/hosts.ini'
    ansible.playbook = "/vagrant/provisioning/playbook.yml"
    ansible.limit = 'all'
    ansible.galaxy_role_file = "/vagrant/provisioning/requirements.yml"
    ansible.galaxy_roles_path = "/etc/ansible/roles"
    ansible.galaxy_command = "sudo ansible-galaxy install --role-file=%{role_file} --roles-path=%{roles_path} --force"
end

As you can see, ansible.limit is all.

├── ansible.cfg
├── provisioning
│   ├── group_vars
│   │   └── all.yml
│   ├── inventory
│   │   ├── hosts.ini
│   │   └── hosts.yml
│   ├── playbook.yml
│   └── requirements.yml
└── Vagrantfile

all.yml content is:

solr_cores:
mssql_restore_backups: false

I need to replace mssql_restore_backup default value picking it up from an environment variable.

Is there anyway to pass environment variable value to ansible provisioner?

Any ideas?


Solution

  • In Ansible the variables with mayor precedence are extra-vars and you can add them to your Vagrantfile as below

    ansible.extra_vars = {
      mssql_restore_backup: $MSSQLRESTOREBACKUP
    }
    

    Documentation:

    https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#understanding-variable-precedence

    https://www.vagrantup.com/docs/provisioning/ansible_common#extra_vars