diff --git a/README.md b/README.md index 536292e..587b867 100644 --- a/README.md +++ b/README.md @@ -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: -``` -production # inventory file for production servers -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/ # "" +```sh +./init.sh ``` -## 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 -``` -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 +There's no `ansible.cfg`, so pass the inventory directory every time. API secrets are vault-encrypted, so you also need the vault password: - staging/ - hosts # inventory file for staging environment - 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/ +```sh +ansible-playbook -i inventory/ playbooks/update-debian.yml --ask-vault-pass ``` -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.