---
- name: Install system dependencies
  apk:
    name: libvirt-dev, ruby-bundler
    state: present
  tags: system
  become: yes

- name: Clone vagant
  git:
    depth: 1
    repo: https://github.com/hashicorp/vagrant.git
    dest: '{{ vagrant_dir }}'

- name: Build vagrant
  bundler:
    chdir: '{{ vagrant_dir }}'
    state: present
    gem_path: vendor
    binstub_directory: exec

- name: Symlink vagrant executable
  file:
    src: '{{ vagrant_dir }}/exec/vagrant'
    dest: ~/.local/bin/vagrant
    state: link

- name: Check whether vagrant-libvirt plugin is installed
  shell:
    cmd: ~/.local/bin/vagrant plugin list | grep -qF 'vagrant-libvirt'
  register: vagrant_plugin_check
  changed_when: no
  failed_when: no

- name: Install vagrant-libvirt plugin
  command:
    cmd: ~/.local/bin/vagrant plugin install vagrant-libvirt
  when: vagrant_plugin_check.rc == 1
  environment:
    CONFIGURE_ARGS: with-libvirt-include=/usr/include/libvirt with-libvirt-lib=/usr/lib
