Dav Clark
Dav Clark

Reputation: 1460

Execute script to setup ansible (or another provisioner) for Vagrant?

The general goal I have is that a member of my team would generally clone a git repo, cd into it, then type vagrant up. In particular, I don't want them to have to install anything apart from vagrant and the VM runner (i.e., VirtualBox).

For ansible, I include it in a subtree. It's easy enough to run, e.g., source ansible/hacking/env-setup. But, I want this to be as bulletproof and standardized as possible.

Is there a way to get vagrant up to run a script like the above to enable the provisioner (on the host)? Or do I need to wrap this in a shell script?

[In case you want a rationale for vagrant up over a shell script, Vagrant automatically walks up the source tree to find the appropriate Vagrantfile - so my target developer / designer audience doesn't need to think about where the Vagrantfile is.]

Upvotes: 0

Views: 640

Answers (2)

Dav Clark
Dav Clark

Reputation: 1460

As alluded to in my comment, for ansible at least, you can solve this via doing shell environment configuration from within the Vagrant.configure block. For ansible, the following currently works (assuming you've put ansible in a directory next to your Vagrantfile called ansible):

curr_dir = Dir.pwd
ENV['PYTHONPATH'] = "#{curr_dir}/ansible/lib:#{ENV['PYTHONPATH']}"
ENV['PATH'] = "#{curr_dir}/ansible/bin:#{ENV['PATH']}"
ENV['ANSIBLE_LIBRARY'] = "#{curr_dir}/ansible/library"

Note that the ansible/hacking/env-setup script also sets up the MANPATH. That's obviously of limited utility here!

Upvotes: 0

Matt Cooper
Matt Cooper

Reputation: 10840

You could do something like this, I haven't tested this completely as I don't use ansible... but you should get the idea:

Vagrant.configure("2") do |config|
    ansible_installed = `ansible-playbook --version` rescue nil
    if !ansible_installed
      `export DEBIAN_FRONTEND=noninteractive; sudo apt-get -y install ansible`
    end
end

It will probably ask them for their password as well... but that should be no issue for a one time thing.

Upvotes: 1

Related Questions