-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the Vagrant Ansible Provisioner example
- Loading branch information
Showing
7 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.vagrant/ | ||
/roles/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
roles: | ||
- name: dmotte.disable_ipv6 | ||
# version: "2.0.0" |