Skip to content

Commit 9134a5e

Browse files
author
John Fitzgerald
committed
Add devcontainers for Ansible
1 parent 13b3bba commit 9134a5e

File tree

9 files changed

+71
-33
lines changed

9 files changed

+71
-33
lines changed

.devcontainer/devcontainer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "Ansible",
3+
// Community Ansible dev tools image - has ansible, ansible-lint, ansible-dev-tools pre-installed
4+
"image": "ghcr.io/ansible/community-ansible-dev-tools:latest",
5+
// Host networking lets the container reach Vagrant VMs on 192.168.56.x
6+
// This only works on Linux (which you're on)
7+
"runArgs": ["--network=host"],
8+
// Mount your SSH keys so Ansible can connect to hosts
9+
"mounts": [
10+
"source=${localEnv:HOME}/.vagrant.d,target=/root/.vagrant.d,type=bind,consistency=cached,readonly"
11+
],
12+
"customizations": {
13+
"vscode": {
14+
"extensions": ["redhat.ansible", "redhat.vscode-yaml"]
15+
}
16+
}
17+
}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
.vagrant
33
venv
44
roles/geerlingguy.security
5-
working_notes.md
5+
working_notes.md

README.md

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@ The goal of this project is to demonstrate how to build a Kubernetes cluster on
55
- Vagrant/Virtual Box
66
- Home lab (coming soon)
77
- The plan in the future is to deploy it on some cheap cloud VM's
8-
## Dependencies
9-
### Ansible
10-
Ansible version is core 2.19.0. To install do:
11-
```shell
12-
sudo apt install python3-venv -y
13-
python3 -m venv ~/venvs/ansible
14-
source ~/venvs/ansible/bin/activate
15-
pip install ansible-core==2.19.0
16-
```
17-
Then `source path/to/venv/bin/activate`
188
### Vagrant
199
- Install Vagrant and Virtual Box-
2010
## Inventories
@@ -25,16 +15,52 @@ Then `source path/to/venv/bin/activate`
2515
192.168.56.112 srv2
2616
192.168.56.113 srv3
2717
```
28-
#### Deploy to Vagrant
29-
Then from the root of the project:
18+
#### Local Development (Devcontainer)
19+
Ansible is run from a devcontainer to ensure a consistent, up-to-date version. You will need:
20+
- [Docker](https://docs.docker.com/engine/install/)
21+
- VS Code with the [Dev Containers](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) extension
22+
23+
**1. Open the project in the devcontainer**
24+
25+
Open the project in VS Code, then either accept the "Reopen in Container" popup or run via the command palette (`Ctrl+Shift+P`):
26+
```
27+
Dev Containers: Reopen in Container
28+
```
29+
The first run will pull the image and may take a minute. Subsequent opens are instant.
30+
31+
**2. Start the Vagrant VMs**
32+
33+
From a terminal on your **host machine** (not inside VS Code):
3034
```shell
3135
vagrant up
3236
```
33-
You should then be able to ssh to each node with `vagrant ssh ${node_name}`
34-
#### Re-running plays
35-
Run:
37+
This boots the three VMs without running Ansible. You can verify they are up with:
38+
```shell
39+
vagrant status
40+
```
41+
42+
**3. Install Ansible roles**
43+
44+
From the VS Code terminal (inside the devcontainer):
45+
```shell
46+
ansible-galaxy install -r roles/requirements.yml
47+
```
48+
49+
**4. Run the playbook**
50+
51+
```shell
52+
ansible-playbook -i inventory/local_vagrant site.yml --become
53+
```
54+
55+
**Re-running the playbook**
56+
57+
Just re-run step 4. No need to restart the VMs.
58+
59+
**Tearing down**
60+
61+
From the host terminal:
3662
```shell
37-
vagrant provision
63+
vagrant destroy -f
3864
```
3965
### Home Servers
4066
- This is a for sure a work in progress.... Currently, I only have three Lenovo m910q machines plugged straight into my ISP modem (It's not serving anything to the internet).

Vagrantfile

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@ Vagrant.configure("2") do |config|
3636
box_config.vm.box = box[:box]
3737
box_config.vm.hostname = box[:hostname]
3838
box_config.vm.network "private_network", ip: box[:ip]
39-
40-
# do ansible provision after last box starts
41-
if index == boxes.size - 1
42-
box_config.vm.provision "ansible" do |ansible|
43-
ansible.compatibility_mode = "2.0"
44-
ansible.playbook = "site.yml"
45-
ansible.limit = "all"
46-
ansible.inventory_path = "inventory/local_vagrant"
47-
ansible.become = true
48-
ansible.galaxy_role_file = "roles/requirements.yml"
49-
ansible.galaxy_command = "ansible-galaxy install -f -r %{role_file}"
50-
end
51-
end
5239
end
5340
end
5441
end

ansible.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
[defaults]
33
roles_path = ./roles
44
collections_paths = ./collections
5+
host_key_checking = False
6+
remote_user = vagrant
7+
deprecation_warnings=False

inventory/local_vagrant/group_vars/all/base.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
---
2+
# Vagrant SSH connection settings
3+
ansible_user: vagrant
4+
ansible_ssh_private_key_file: ~/.vagrant.d/insecure_private_key
5+
ansible_ssh_extra_args: "-o IdentitiesOnly=yes"
6+
27
# Create users
38
user_config:
49
- username: john
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
ansible_host: 192.168.56.111
2-
ansible_user: ansible
2+
ansible_user: vagrant
33
hostname_short: srv1
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
ansible_host: 192.168.56.112
2-
ansible_user: ansible
2+
ansible_user: vagrant
33
hostname_short: srv2
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
ansible_host: 192.168.56.113
2-
ansible_user: ansible
2+
ansible_user: vagrant
33
hostname_short: srv3

0 commit comments

Comments
 (0)