Simon Fredsted
Simon Fredsted

Reputation: 976

Ansible in Vagrantfile – pull playbook from git

I have 3 git repos:

  1. ansible config (roles, tasks...)
  2. backend code
  3. frontend code

In my Vagrantfile, I can specify a path to a playbook.

But what if I want to have the Ansible playbook pulled automatically from git? Can vagrant do that? I don't want to have duplicate Ansible configurations.

Or should I add my ansible repo as a submodule? Or something else?

Upvotes: 2

Views: 264

Answers (1)

kalefranz
kalefranz

Reputation: 4752

I have several similar situations to you. Here's what I do...

All three repos sit in the same parent directory.

./parent
  |-- ansible
  |-- backend
  +-- frontend

I use the vagrant-triggers plugin to trigger updates to the ansible repo when I execute vagrant up or vagrant provision. So the beginning of my Vagrantfile might look like

Vagrant.configure("2") do |config|
  config.trigger.before :up do
    system("../ansible/update.sh")
  end

where ../ansible/update.sh is a script that checks to make sure the ansible git repo is up-to-date and in the correct state (on the right branch, etc).

The next part then would be if you want the backend and frontend repos to be able to bootstrap themselves if the ansible repo doesn't exist. And that just takes extra shell scripting with the vagrant-triggers plugin. Also, if you prefer, you can use ansible's local_action (see docs) instead of shell scripts.

Upvotes: 2

Related Questions