generated from GamingChaos/ansible-repository-template
Compare commits
5
Commits
889cc120fe
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
13526f928c | ||
|
|
a3d4e9b4fa | ||
|
|
093589732e | ||
|
|
c409e38d4e | ||
|
|
9b7383f1f4 |
@@ -4,7 +4,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
|
|||||||
|
|
||||||
## What this is
|
## 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/`. The README is the generic Ansible best-practices template and doesn't describe this repo.
|
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
|
## Setup and commands
|
||||||
|
|
||||||
@@ -29,16 +29,16 @@ The `# noqa:` annotations in the playbooks are for ansible-lint (`ansible-lint p
|
|||||||
## Inventory layout
|
## Inventory layout
|
||||||
|
|
||||||
- `inventory/homelab` holds the infrastructure groups: `ubuntu` (all Ubuntu VMs), `docker`, `kubernetes`, `proxmox`, `truenas`.
|
- `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 the recent commits).
|
- `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/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.
|
- `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
|
## Playbook notes
|
||||||
|
|
||||||
- `update-checkmk.yml` has three plays that share state through a fact:
|
- `update-checkmk.yml` has three plays that share state through a fact:
|
||||||
1. On `localhost`, it gets the latest stable Checkmk tag from GitHub with `git ls-remote` and sets `checkmk_version`. A `set_fact` only applies to the hosts in its own play, so later plays only see the fact if this play covers their hosts too (as `hosts: all` plus `delegate_to: localhost` did) or if they read it as `hostvars['localhost'].checkmk_version`.
|
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 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`.
|
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`).
|
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'`.
|
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`.
|
- `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`.
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
@@ -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 }}
|
||||||
|
|||||||
Reference in New Issue
Block a user