Compare commits

..
11 Commits
10 changed files with 156 additions and 95 deletions
+51
View File
@@ -0,0 +1,51 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## What this is
Ansible playbooks for a homelab: Ubuntu VMs on a Proxmox host (`rimeru`), a TrueNAS box (`veldora`), and a Checkmk monitoring server (`shion`). There are no roles and no `ansible.cfg`. Everything is a standalone playbook in `playbooks/`. `README.md` is the human-facing overview: setup, the playbook table, and the steps for adding a host.
## Setup and commands
```sh
./init.sh # creates ./venv, installs requirements.txt (proxmoxer, requests) and Galaxy collections
ansible-galaxy collection install -r requirements.yml # collections only: community.proxmox, checkmk.general
```
Ansible itself is not in `requirements.txt`. It must already be installed on the machine.
Without an `ansible.cfg`, you always pass the inventory explicitly. Use the directory so both inventory files and `group_vars`/`host_vars` get loaded:
```sh
ansible-playbook -i inventory/ playbooks/<playbook>.yml --ask-vault-pass
ansible-playbook -i inventory/ playbooks/<playbook>.yml --syntax-check
ansible-playbook -i inventory/ playbooks/<playbook>.yml --check --limit <host>
ansible-vault edit inventory/group_vars/all/vault.yml
```
The `# noqa:` annotations in the playbooks are for ansible-lint (`ansible-lint playbooks/`).
## Inventory layout
- `inventory/homelab` holds the infrastructure groups: `ubuntu` (all Ubuntu VMs), `docker`, `kubernetes`, `proxmox`, `truenas`.
- `inventory/monitoring` holds the Checkmk groups: `sites` (Checkmk servers) and `agents` (hosts that run the Checkmk agent). When you add a host to the homelab, it usually also goes into `agents` (see README for the full steps).
- `inventory/group_vars/all/` holds the API connection vars for both Proxmox and Checkmk (`servers.yml`) plus `vault.yml`, which is Ansible Vault–encrypted and holds the matching secrets (`proxmox_api_token_secret`, `checkmk_api_secret`). They live under `all` (not `sites`) so the `ubuntu` hosts in `update-debian.yml` can reach the Checkmk API vars too. Secrets go in vault files, not inline in playbooks.
- `inventory/host_vars/<fqdn>.yml` for a Checkmk server sets `checkmk_sites` (the list of OMD site names to manage) and `vmid`. No playbook reads `vmid` yet. The snapshot task finds the VM by name instead.
## Playbook notes
- `update-checkmk.yml` has three plays that share state through a fact:
1. On `all` hosts, delegated to localhost, it gets the latest stable Checkmk tag from GitHub with `git ls-remote` and sets `checkmk_version`. It targets `all` on purpose: a `set_fact` only applies to the hosts in its own play, so `sites` and `agents` would lose the fact if the play ran only on `localhost` (or they'd have to read `hostvars['localhost'].checkmk_version`).
2. On `sites`, it reads the installed version with `omd version -b` (as the login user, no `become`) and ends the play for that host (`meta: end_host`) when it already matches `checkmk_version`. Otherwise it snapshots the VM through the Proxmox API (delegated to localhost, using `inventory_hostname_short` as the Proxmox VM name), installs the community-edition `.deb`, then runs `omd stop`, `backup`, `update` and `start` for each entry in `checkmk_sites`.
3. On `agents`, it installs the agent `.deb` from the local Checkmk server (`checkmk.stuyckv.com/local`), then registers the agent for TLS with `cmk-agent-ctl register`, using the `checkmk_api_*` vars. It registers on every run on purpose, because registering again renews the agent certificate. `checkmk.general` has an `agent` role but no `agent` module, so this is done with a plain command.
Package installs only run when `ansible_facts.lsb.id == 'Ubuntu'`.
- `update-debian.yml` runs against `ubuntu`. It sets a Checkmk downtime through `checkmk.general`, then runs dist-upgrade, reboots if needed and autoremoves. Like the Proxmox snapshot, the downtime task is delegated to localhost with `become: false`.
- `install-kubernetes.yml` (untested) prepares the `kubernetes` group with Docker CE, containerd and kubeadm/kubelet/kubectl pinned to the v1.35 repo. The untracked `install-kube.sh` is the shell version it was ported from.
## Conventions
- Newer playbooks use FQCN module names (`ansible.builtin.*`) and the boolean literals `true`/`false` (not `yes`/`no`). `install-kubernetes.yml` doesn't follow either yet, and `update-debian.yml` still has some `yes`/`no`.
- Shell/command tasks set `changed_when` explicitly.
- Managed hosts are reached as the `ansible` user with passwordless sudo, set up by `create-ansibleuser.sh`. It authorizes an `ansible@semaphore` key, so the playbooks are also run from Ansible Semaphore.
+38 -82
View File
@@ -1,93 +1,49 @@
# ansible-repository-template # ansible-homelab
[From ansible best practices](https://docs.ansible.com/ansible/2.8/user_guide/playbooks_best_practices.html) Ansible playbooks for managing the homelab. Most machines are Ubuntu VMs running on the Proxmox host `rimeru`, and `shion` runs Checkmk to monitor everything. The playbooks run from a workstation or from Ansible Semaphore.
## Directory Layout ## Setup
The top level of the directory would contain files and directories like so: Ansible itself must already be installed. Then:
``` ```sh
production # inventory file for production servers ./init.sh
staging # inventory file for staging environment
group_vars/
group1.yml # here we assign variables to particular groups
group2.yml
host_vars/
hostname1.yml # here we assign variables to particular systems
hostname2.yml
library/ # if any custom modules, put them here (optional)
module_utils/ # if any custom module_utils to support modules, put them here (optional)
filter_plugins/ # if any custom filter plugins, put them here (optional)
site.yml # master playbook
webservers.yml # playbook for webserver tier
dbservers.yml # playbook for dbserver tier
roles/
common/ # this hierarchy represents a "role"
tasks/ #
main.yml # <-- tasks file can include smaller files if warranted
handlers/ #
main.yml # <-- handlers file
templates/ # <-- files for use with the template resource
ntp.conf.j2 # <------- templates end in .j2
files/ #
bar.txt # <-- files for use with the copy resource
foo.sh # <-- script files for use with the script resource
vars/ #
main.yml # <-- variables associated with this role
defaults/ #
main.yml # <-- default lower priority variables for this role
meta/ #
main.yml # <-- role dependencies
library/ # roles can also include custom modules
module_utils/ # roles can also include custom module_utils
lookup_plugins/ # or other types of plugins, like lookup in this case
webtier/ # same kind of structure as "common" was above, done for the webtier role
monitoring/ # ""
fooapp/ # ""
``` ```
## Alternative Directory Layout This creates a `venv/` with the Python dependencies (`proxmoxer`, `requests`) and installs the Galaxy collections from `requirements.yml` (`community.proxmox`, `checkmk.general`).
Alternatively you can put each inventory file with its group_vars/host_vars in a separate directory. This is particularly useful if your group_vars/host_vars don’t have that much in common in different environments. The layout could look something like this: ## Running a playbook
``` There's no `ansible.cfg`, so pass the inventory directory every time. API secrets are vault-encrypted, so you also need the vault password:
inventories/
production/
hosts # inventory file for production servers
group_vars/
group1.yml # here we assign variables to particular groups
group2.yml
host_vars/
hostname1.yml # here we assign variables to particular systems
hostname2.yml
staging/ ```sh
hosts # inventory file for staging environment ansible-playbook -i inventory/ playbooks/update-debian.yml --ask-vault-pass
group_vars/
group1.yml # here we assign variables to particular groups
group2.yml
host_vars/
stagehost1.yml # here we assign variables to particular systems
stagehost2.yml
library/
module_utils/
filter_plugins/
site.yml
webservers.yml
dbservers.yml
roles/
common/
webtier/
monitoring/
fooapp/
``` ```
This layout gives you more flexibility for larger environments, as well as a total separation of inventory variables between different environments. The downside is that it is harder to maintain, because there are more files. To dry-run against a single host first:
```sh
ansible-playbook -i inventory/ playbooks/update-debian.yml --ask-vault-pass --check --limit shion.stuyckv.local
```
## Playbooks
| Playbook | Hosts | What it does |
| --- | --- | --- |
| `update-debian.yml` | `ubuntu` | Schedules a one-hour Checkmk downtime, runs a dist-upgrade, reboots if needed and removes unused packages. |
| `update-checkmk.yml` | `sites`, `agents` | Finds the latest Checkmk release, snapshots the Checkmk VM in Proxmox, upgrades Checkmk and its OMD sites (taking a backup first), then updates the agent on every monitored host. |
| `install-kubernetes.yml` | `kubernetes` | Installs Docker CE, containerd, kubeadm, kubelet and kubectl (v1.35). **Not tested yet.** |
## Inventory
- `inventory/homelab`: hosts grouped by role (`ubuntu`, `docker`, `kubernetes`, `proxmox`, `truenas`).
- `inventory/monitoring`: the Checkmk server (`sites`) and the hosts that run the Checkmk agent (`agents`).
- `inventory/group_vars/all/servers.yml`: Proxmox and Checkmk API connection settings.
- `inventory/group_vars/all/vault.yml`: the API secrets, encrypted with Ansible Vault. Edit it with `ansible-vault edit inventory/group_vars/all/vault.yml`.
- `inventory/host_vars/`: per-host settings, such as the OMD sites to upgrade on the Checkmk server (`checkmk_sites`).
## Adding a host
1. On the new host, run `create-ansibleuser.sh` as root. It creates the `ansible` user with passwordless sudo and authorizes the workstation and Semaphore SSH keys.
2. Add the host to the matching groups in `inventory/homelab`.
3. Add the host to `agents` in `inventory/monitoring` so it gets the Checkmk agent.
@@ -2,3 +2,7 @@
proxmox_api_host: rimeru.stuyckv.local proxmox_api_host: rimeru.stuyckv.local
proxmox_api_user: ansible@pve proxmox_api_user: ansible@pve
proxmox_api_token_id: ansible proxmox_api_token_id: ansible
checkmk_api_url: https://checkmk.stuyckv.com/
checkmk_api_site: local
checkmk_api_user: ansible
+10
View File
@@ -0,0 +1,10 @@
$ANSIBLE_VAULT;1.1;AES256
63643461333732333933306361633239656336663835393137353361353331363065303939643639
6162643138613434653034626466613731663661353161350a656634643739636231363330613637
63343936303665643532363932396239373135643766383563636532393762313837393364393561
6630393133656337650a313465663663373131373561613333313366386237366137663563383231
64303065343330643136373437306534336238633665356462653439306432663063366639333330
34353430623463303863326133303032646161376435643133353866626534643433373538336265
37626264663935373838666233383039323135383837633131613435373732616433306564313466
62653733663063616362323937376431386332326533633132363430666531333166616365306332
64336139386663653861346331616530346431656232343539636262643936386436
-9
View File
@@ -1,9 +0,0 @@
$ANSIBLE_VAULT;1.1;AES256
66393831316666346565393032333961373365346633643730386661616332356431653331396334
3633373066646165636639646462376338366665376561650a643530306339666436323130383737
36373532623435336466386433326664356537326137363537343133323739343732383565346336
6630636236363261610a646336613436323462663339313364393833653539653238666437623261
65383864346165623730363136353838396131643962393134366362393266356365396231633064
65366631653437626666343465383338383637373139353130623961316662353431313037303834
36356131323233613461333163303537306635343065663435333961376264316235643335323661
36336331336537343432
+3
View File
@@ -3,6 +3,8 @@ shion.stuyckv.local
charybdis.stuyckv.local charybdis.stuyckv.local
ramiris.stuyckv.local ramiris.stuyckv.local
millim.stuyckv.local millim.stuyckv.local
shuna.stuyckv.local
diablo.stuyckv.local
guy.stuyckv.local guy.stuyckv.local
claymen.stuyckv.local claymen.stuyckv.local
leon.stuyckv.local leon.stuyckv.local
@@ -11,6 +13,7 @@ leon.stuyckv.local
charybdis.stuyckv.local charybdis.stuyckv.local
ramiris.stuyckv.local ramiris.stuyckv.local
millim.stuyckv.local millim.stuyckv.local
diablo.stuyckv.local
[kubernetes] [kubernetes]
guy.stuyckv.local guy.stuyckv.local
+2
View File
@@ -7,6 +7,8 @@ shion.stuyckv.local
charybdis.stuyckv.local charybdis.stuyckv.local
ramiris.stuyckv.local ramiris.stuyckv.local
millim.stuyckv.local millim.stuyckv.local
shuna.stuyckv.local
diablo.stuyckv.local
guy.stuyckv.local guy.stuyckv.local
claymen.stuyckv.local claymen.stuyckv.local
leon.stuyckv.local leon.stuyckv.local
+29 -2
View File
@@ -32,6 +32,17 @@
become: true become: true
tasks: tasks:
- name: Get current checkmk version
become: false
register: current_version
changed_when: false
ansible.builtin.command: omd version -b
# omd appends the edition to the version (e.g. 2.4.0p12.cre), the git tag doesn't
- name: Stop when checkmk is up to date
when: current_version.stdout | regex_replace('[.][a-z]+$', '') == checkmk_version
ansible.builtin.meta: end_host
- name: Snapshot VM before update - name: Snapshot VM before update
become: false become: false
delegate_to: localhost delegate_to: localhost
@@ -80,9 +91,25 @@
- name: Update checkmk agents on monitored hosts - name: Update checkmk agents on monitored hosts
hosts: agents hosts: agents
become: true become: true
vars:
checkmk_server: "shion.stuyckv.local"
tasks: tasks:
- name: Install new checkmk agent - name: Install new checkmk agent
ansible.builtin.apt: ansible.builtin.apt:
deb: "https://checkmk.stuyckv.com/local/check_mk/agents/check-mk-agent_{{ checkmk_version }}-1_all.deb" deb: "http://{{ checkmk_server }}/local/check_mk/agents/check-mk-agent_{{ checkmk_version }}-1_all.deb"
when: ansible_facts.lsb.id == 'Ubuntu' when: ansible_facts.os_family == 'Debian'
# registering again renews the agent certificate
- name: Register agent with the site
changed_when: true
ansible.builtin.command:
argv:
- cmk-agent-ctl
- register
- --trust-cert
- --hostname={{ inventory_hostname }}
- --server={{ checkmk_server }}
- --site={{ checkmk_api_site }}
- --user={{ checkmk_api_user }}
- --password={{ checkmk_api_secret }}
+15 -2
View File
@@ -1,10 +1,23 @@
--- ---
# source: https://www.jeffgeerling.com/blog/2022/ansible-playbook-upgrade-ubuntudebian-servers-and-reboot-if-needed # source: https://www.jeffgeerling.com/blog/2022/ansible-playbook-upgrade-ubuntudebian-servers-and-reboot-if-needed
- hosts: ubuntu - hosts: ubuntu
gather_facts: yes gather_facts: true
become: yes become: true
tasks: tasks:
- name: Create Downtime
become: false
delegate_to: localhost
checkmk.general.downtime:
server_url: "{{ checkmk_api_url }}"
site: "{{ checkmk_api_site }}"
api_user: "{{ checkmk_api_user }}"
api_secret: "{{ checkmk_api_secret }}"
host_name: "{{ inventory_hostname }}"
comment: "created by update-debian playbook"
end_after:
hours: 1
- name: Perform a dist-upgrade. - name: Perform a dist-upgrade.
ansible.builtin.apt: ansible.builtin.apt:
upgrade: dist upgrade: dist
+4
View File
@@ -1,4 +1,8 @@
--- ---
# ansible-galaxy install -r requirements.yml
collections: collections:
- name: community.proxmox - name: community.proxmox
version: "1.6.0" version: "1.6.0"
- name: checkmk.general
version: "8.6.0"