From 002f934c70eeafcb9dde9a9b0b2a338eb0b75586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Fri, 7 Feb 2025 00:28:05 +0100 Subject: [PATCH] Add ansible and playbook to trigger upgrade --- flake.nix | 19 +++++++++++++++++++ inventory | 31 +++++++++++++++++++++++++++++++ playbooks/run-upgrade.yml | 9 +++++++++ system/packages.nix | 1 + 4 files changed, 60 insertions(+) create mode 100755 inventory create mode 100644 playbooks/run-upgrade.yml diff --git a/flake.nix b/flake.nix index ec7b238..088209c 100644 --- a/flake.nix +++ b/flake.nix @@ -32,6 +32,14 @@ config.allowUnfree = true; }; }; + allSystems = [ + "x86_64-linux" + "aarch64-linux" + "x86_64-darwin" + "aarch64-darwin" + ]; + forAllSystems = + f: nixpkgs.lib.genAttrs allSystems (system: f { pkgs = import nixpkgs { inherit system; }; }); in { nixosConfigurations = { @@ -297,5 +305,16 @@ ]; }; }; + devShells = forAllSystems ( + { pkgs }: + { + default = pkgs.mkShell { + packages = with pkgs; [ + ansible + python3 + ]; + }; + } + ); }; } diff --git a/inventory b/inventory new file mode 100755 index 0000000..6e07b1f --- /dev/null +++ b/inventory @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +import json +import subprocess + +IGNORED_HOSTS = [ + "inc1", + "inc2", + "media1", + "nixos-test1", + "ns3", + "ns4", + "template1", +] + +result = subprocess.run(["nix", "flake", "show", "--json"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) +results = json.loads(result.stdout) + +configs = results.get("nixosConfigurations") +hosts = [x for x in configs.keys() if x not in IGNORED_HOSTS] + +output = { + "all": { + "hosts": hosts, + "vars": { + "ansible_python_interpreter": "/run/current-system/sw/bin/python3" + }, + } +} + +print(json.dumps(output)) diff --git a/playbooks/run-upgrade.yml b/playbooks/run-upgrade.yml new file mode 100644 index 0000000..30062d4 --- /dev/null +++ b/playbooks/run-upgrade.yml @@ -0,0 +1,9 @@ +--- +- name: Trigger nixos-upgrade job on all hosts + hosts: all + remote_user: root + + tasks: + - ansible.builtin.systemd_service: + name: nixos-upgrade.service + state: started diff --git a/system/packages.nix b/system/packages.nix index 2a414e6..76699f2 100644 --- a/system/packages.nix +++ b/system/packages.nix @@ -4,5 +4,6 @@ pkgs.git pkgs.jq pkgs.kitty.terminfo + pkgs.python3 ]; }