Skip to content

Instantly share code, notes, and snippets.

@phantomwhale
Created March 20, 2014 04:14
Show Gist options
  • Save phantomwhale/9657134 to your computer and use it in GitHub Desktop.
Save phantomwhale/9657134 to your computer and use it in GitHub Desktop.
Passing command line arguments into Vagrant to configure Ansible Provisioning
# with a space, this doesn't work...
$ ANSIBLE_ARGS='-t elasticsearch' vagrant provision
==> default: Running provisioner: ansible...
ERROR: tag(s) not found in playbook: elasticsearch. possible values: apache,common,elasticsearch,java,passenger,postgresql,ruby
Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.
# without the space, it now works...
$ ANSIBLE_ARGS='-telasticsearch' vagrant provision
==> default: Running provisioner: ansible...
PLAY [web] ********************************************************************
...
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.provision "ansible" do |ansible|
ansible.playbook = "config/ansible/site.yml"
ansible.extra_vars = {
"ansible_ssh_user" => "vagrant",
}
ansible.raw_arguments = ENV['ANSIBLE_ARGS']
end
end
@diroussel
Copy link

The whitespace issue seems to be fixed now. I tested in ansible 1.9

@geerlingguy
Copy link

geerlingguy commented Jun 22, 2017

I think this is the third time I've come back here so just wanted to finally star the gist.

Also, here's what I have working:

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "main.yml"
    ansible.raw_arguments = Shellwords.shellsplit(ENV['ANSIBLE_ARGS']) if ENV['ANSIBLE_ARGS']
  end
# CLI command.
ANSIBLE_ARGS='--extra-vars "some_var=value"' vagrant up

This allows the playbook to work either way (with or without any ANSIBLE_ARGS).

Thanks to @generalov for the tip on shellsplit!

@karlskewes
Copy link

Thanks very much everyone! Using @geerlingguy's snippets above.

@gkedge
Copy link

gkedge commented Jul 24, 2021

I have to provision a VM on Windows using ansible_local. Pity me. And, I am PS impaired also so for those of my lot, this might help:

PS> $env:ANSIBLE_ARGS='--tags "start_of_day"'; vagrant up --provision

The same entry in the Vagrant file shared by @geerlingguy applies. The application of the ANSIBLE_ARGS on a Windows PS (compared to the Linux line shared) is that ANSIBLE_ARGS is permanent for the terminal session.

To get that same behavior to not have an env var linger, you would have to unset it after the vagrant command:

PS> $env:ANSIBLE_ARGS='--tags "start_of_day"'; vagrant up --provision; $env:ANSIBLE_ARGS=""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment