1

Using Vagrant's ansible_local provisioner, how can I ensure the roles used in my playbook are installed before Ansible tries to execute them?

I tried to install the role with shell: ansible-galaxy ... in both a preceding task block and as part of pre_tasks, but Ansible (v2.1.2.0 on Ubuntu 16.04) seems looks at the roles: stanza of a playbook before anything else and exits with an error if it is not found in the roles path.

Davor Cubranic
  • 365
  • 2
  • 9

1 Answers1

0

Vagrant's ansible_local is probably not a good choice as it is not very flexible.

I achieve a similar setup by using the shell provisionier and an inline script in the Vagrantfile:

Vagrant.configure(2) do |config|
config.vm.synced_folder ".", "/vagrant"
config.vm.box = "deb/wheezy-amd64"
config.ssh.forward_agent = true
$script = <<SCRIPT
sudo apt-get update
sudo apt-get install python2.7-dev -qq
sudo apt-get install python-pip -qq
sudo easy_install pip
sudo pip install markupsafe ansible
sudo rm -rf /etc/ansible
sudo mkdir -p /etc/ansible/roles
cd /vagrant
sudo ansible-galaxy install -r requirements.yml -p roles/ --force
# Add ansible.cfg to pick up roles path.
echo '[defaults]' >> /etc/ansible/ansible.cfg
echo 'roles_path = /etc/ansible/roles/' >> /etc/ansible/ansible.cfg
sudo -E -s ansible-playbook -vv -i /vagrant/local-test/inventory /vagrant/local-test/site.yml --connection=local
SCRIPT
# Vagrant.configure("2") do |config|
config.vm.provision "shell", run: "always", inline: $script, privileged: "false"
end

old answer

If you run a task

shell: ansible-galaxy install ...

Ansible will install the role on the target system, not the system executing ansible-playbook. But Ansible will look for the roles of a play on the control system.

Best way to handle this is IMHO:

  1. create a requirements.yml containing the roles your play needs
  2. install the roles with ansible-galaxy install -r requirements.yml

Take a look at the documentation for parameters of the requirements.yml

Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39
  • Henrik, it does need to install Galaxy on the target system because it's where Ansible itself is running. I edited the question to make this clearer. I'd like to avoid running ansible-galaxy as a separate step, but get the dependencies installed while running the same playbook. – Davor Cubranic Oct 11 '16 at 17:47
  • Ah. Ok. I did the same thing for Windows support for some coworkers. I ended up running galaxy install in a script in the Vagrantfile. I'm currently not at work. Will edit my answer tomorrow. – Henrik Pingel Oct 11 '16 at 18:03