Overriding ansible_local provisioner settings

90 views
Skip to first unread message

MK

unread,
May 6, 2016, 11:42:39 AM5/6/16
to Vagrant
I'm have a box that uses ansible_local provisioning, which is defined in the Vagrant file included with the box:


config.vm.provision :ansible_local do |ansible|
  ansible_root
= "/home/vagrant/provision"
  ansible
.provisioning_path = ansible_root
  ansible
.playbook = "#{ansible_root}/playbook.yml"
end




This works, but now I need to pass a variable to Ansible while provisioning a specific machine, i.e. I need to override the default provisioning settings.


At first I tried this (in the local Vagrantfile, which should override values in the box's Vagrantfile):

config.vm.provision :ansible_local do |ansible|
 ansible
.extra_vars = {"develop_mode": true}
end

This is no good, because Vagrant replaces the entire provision block instead of merging, and then complains that there's no playbook path defined. Let's try this:

config.vm.provision :ansible_local do |ansible|
  ansible_root
= "/home/vagrant/provision"
  ansible
.provisioning_path = ansible_root
  ansible
.playbook = "#{ansible_root}/playbook.yml"
  ansible
.extra_vars = {"develop_mode": true}
end



Now, Vagrant can run the provisioner, but the `develop_mode` variable doesn't get set correctly. This is the output from Ansible:

TASK: [build | debug ] **************************************************
ok
: [default] => {
   
"var": {
       
"develop_mode": "develop_mode"
   
}
}



There are a couple of Vagrant 1.8.1 bugs about this, so here's a suggested workaround (using `raw_arguments` instead of `extra_vars`):

config.vm.provision :ansible_local do |ansible|
  ansible_root
= "/home/vagrant/provision"
  ansible
.provisioning_path = ansible_root
  ansible
.playbook = "#{ansible_root}/playbook.yml"
  ansible
.raw_arguments = ["--extra-vars", "develop_mode=true"]
end



Still no good, same Ansible output as before:

TASK: [build | debug ] **************************************************
ok
: [default] => {
   
"var": {
       
"develop_mode": "develop_mode"
   
}
}



Maybe I need to use an override in the local Vagrantfile?

config.vm.provider "virtualbox" do |vbox, override|
 
override.vm.provision :ansible_local do |ansible|
    ansible_root
= "/home/vagrant/provision"
    ansible
.provisioning_path = ansible_root
    ansible
.playbook = "#{ansible_root}/playbook.yml"
    ansible
.raw_arguments = ["--extra-vars", "'develop_mode=true'"]
 
end
end


No, still the same Ansible output as before. Is there a way to do what I'm trying to do?


FYI, here's the expected output as generated from running `ansible-playbook` directly on the VM:

TASK: [build | debug ] **************************************************
ok
: [localhost] => {
   
"var": {
       
"develop_mode": "true"
   
}
}



Reply all
Reply to author
Forward
0 new messages