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
@gildegoma
Copy link

As demonstrated below, this whitespace bug is in childprocess library, which is used by Vagrant.

require 'childprocess'

r, w = IO.pipe

proc = ChildProcess.build("ansible-playbook", 
                          "--private-key=/path/to/insecure_private_key", 
                          "--user=vagrant", 
                          "-t elasticsearch", 
                          "--inventory-file=myinventory", 
                          "test.yml")
proc.io.stdout = proc.io.stderr = w
proc.start
w.close

begin
  loop { print r.readpartial(8192) }
rescue EOFError
end

proc.wait

output (tested with childprocess 0.5.1): ERROR: tag(s) not found in playbook: elasticsearch. possible values: elasticsearch

Note that you can alternatively use --tags=elasticsearch, which might look a bit nicer than -telasticsearch.

@zkf
Copy link

zkf commented Mar 29, 2015

For anyone else who comes along looking for how to pass arguments to Ansible via the Vagrant command line:

If you set ansible.raw_arguments = ENV[ANSIBLE_ARGS].to_s.split(':') you can put multiple arguments in the variable, seperated by :. to_s turns a Nil value into the empty string, so you can leave ANSIBLE_ARGS empty without having split throw a fit.

@generalov
Copy link

Use shell split algorithm:

ansible.raw_arguments = Shellwords.shellsplit(ENV['ANSIBLE_ARGS']) if ENV['ANSIBLE_ARGS']

It works like a charm.

@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