Skip to content

Commit

Permalink
Added the Vagrant Ansible Provisioner example
Browse files Browse the repository at this point in the history
  • Loading branch information
dmotte committed Dec 17, 2023
1 parent cb68589 commit e5e8f94
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/vagrant-ansible-provisioner/.ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
strict: true
exclude_paths:
- .vagrant/
mock_modules:
- community.docker.docker_image
- community.docker.docker_container
2 changes: 2 additions & 0 deletions examples/vagrant-ansible-provisioner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.vagrant/
/roles/
33 changes: 33 additions & 0 deletions examples/vagrant-ansible-provisioner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# vagrant-ansible-provisioner

This is a usage example of the [`dmotte/dockerbox`](https://app.vagrantup.com/dmotte/boxes/dockerbox) Vagrant box that uses Vagrant's [Ansible Provisioner](https://developer.hashicorp.com/vagrant/docs/provisioning/ansible).

From here on, let's assume you have put this folder in `~/myvm` on your PC.

Now customize the [`Vagrantfile`](Vagrantfile); in particular, you may want to customize the `config.vm.synced_folder` section. For example:

```ruby
config.vm.synced_folder "~/git", "/home/vagrant/git"
```

This means that the `~/git` folder on your PC will be mounted to `/home/vagrant/git` inside the VM.

Then you may want to customize the [`playbook.yml`](playbook.yml) file too; it contains just some examples of what you can do.

From the `~/myvm` directory, run the following command to **bring up your VM**:

```bash
vagrant up
```

You can also the following **alias** to your `~/.bashrc` file (replacing the script path with the correct one for your case):

```bash
alias myvmssh="$HOME/myvm/myvmssh.sh"
```

Open a new shell window. Now you can execute stuff in your VM from any directory within your `~/git` folder, using the `myvmssh` alias command. For example:

```bash
myvmssh docker ps -a
```
39 changes: 39 additions & 0 deletions examples/vagrant-ansible-provisioner/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
config.vm.box = "dmotte/dockerbox"

config.vm.hostname = "myvm"

config.vm.provider "virtualbox" do |vb|
vb.memory = 4096
vb.cpus = 4
end

ENV["VAGRANT_EXPERIMENTAL"] = "disks"
config.vm.disk :disk, size: "120GB", primary: true

config.vm.provision "shell", inline: <<-SHELL
echo -e "d\nn\np\n\n\n\nw" | fdisk /dev/sda
resize2fs /dev/sda1
SHELL

config.vm.network "private_network", ip: "192.168.56.100"

config.vm.synced_folder "~/git", "/home/vagrant/git"

config.vm.provision "ansible" do |ansible|
# ansible.galaxy_role_file = "requirements.yml"
ansible.playbook = "playbook.yml"
ansible.extra_vars = { ansible_python_interpreter: "/usr/bin/python3" }
end

# Ping the default route once (without even waiting for a reply) to fix a
# known network issue. See
# https://weisser-zwerg.dev/posts/local-discourse-on-vagrant/#vagrant-up
# for more information
config.vm.provision "shell", run: "always", inline: <<-SHELL
ping '192.168.56.1' -c1 -W.1 || :
SHELL
end
9 changes: 9 additions & 0 deletions examples/vagrant-ansible-provisioner/myvmssh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -e

vagrant_pwd="/home/vagrant/${PWD#"$HOME/"}"

cd "$(dirname "$0")"

vagrant ssh -c "cd $vagrant_pwd && ${*:-/bin/bash}"
46 changes: 46 additions & 0 deletions examples/vagrant-ansible-provisioner/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env ansible-playbook
---
- name: Main play
hosts: all

pre_tasks:
- name: Check Ansible version
ansible.builtin.assert:
that: ansible_version.full is version_compare('2.9', '>=')
msg: The Ansible version is too old for this playbook

tasks:
- name: Execute apt update if the last one is more than 1 hour ago
become: true
ansible.builtin.apt:
update_cache: true
cache_valid_time: 3600
when: ansible_pkg_mgr == "apt"

- name: Ensure some packages are installed
become: true
ansible.builtin.package:
name:
- rsync # Needed by the "ansible.posix.synchronize" Ansible module
- git
- nano
- tmux
- tree
- wget
- zip
- curl
- socat
- jq

- name: Pull the nginx Docker image
community.docker.docker_image:
name: nginx
source: pull

- name: Main container
community.docker.docker_container:
name: nginx-example
image: docker.io/library/nginx:latest
keep_volumes: false # Do not retain **anonymous** volumes when the container is removed
restart_policy: always
ports: ["80:80"]
4 changes: 4 additions & 0 deletions examples/vagrant-ansible-provisioner/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
roles:
- name: dmotte.disable_ipv6
# version: "2.0.0"

0 comments on commit e5e8f94

Please sign in to comment.