From 3f9b16b010aef1201e6ac8fa8174ccbecc7de51c Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 6 Oct 2025 11:54:41 +0300 Subject: Refactor to use tags --- README.md | 5 +++-- playbook.yml | 11 ++++++++++- tasks/jail_datasets.yml | 14 -------------- tasks/jail_single.yml | 22 ---------------------- tasks/jails.yml | 7 ++----- tasks/jails_dataset.yml | 14 ++++++++++++++ tasks/jails_single.yml | 22 ++++++++++++++++++++++ 7 files changed, 51 insertions(+), 44 deletions(-) delete mode 100644 tasks/jail_datasets.yml delete mode 100644 tasks/jail_single.yml create mode 100644 tasks/jails_dataset.yml create mode 100644 tasks/jails_single.yml diff --git a/README.md b/README.md index f55a981..774b67f 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ This repository describes my home server setup with Ansible. The server machine 1. Download a recent FreeBSD release. Tested with https://download.freebsd.org/releases/amd64/amd64/ISO-IMAGES/14.3/FreeBSD-14.3-RELEASE-amd64-memstick.img. 2. Run the installer off a USB drive. During installation, configure: - - a Finnish keymap - hostname "pursotin" - an Auto-ZFS setup with Root-on-ZFS, pool name "zroot" @@ -34,9 +33,11 @@ PermitRootLogin yes Use an [.envrc file](https://direnv.net/) to provide secrets through the shell environment. Required variables are listed in `playbook.yml`. ```shell -ansible-playbook -i inventory playbook.yml +ansible-playbook -i inventory playbook.yml -t [tag1] [tag2] ... ``` +See `playbook.yml` for available tags. + ## Author Jan Tuomi, \. diff --git a/playbook.yml b/playbook.yml index fd9d2ad..f99048c 100644 --- a/playbook.yml +++ b/playbook.yml @@ -55,16 +55,25 @@ tasks: - name: Run general tasks + tags: [general] import_tasks: tasks/general.yml - name: Run network tasks + tags: [network] import_tasks: tasks/network.yml - name: Run email tasks + tags: [email] import_tasks: tasks/email.yml - name: Run ZFS tasks + tags: [zfs] import_tasks: tasks/zfs.yml - - name: Run jails tasks + - name: Run common jails tasks + tags: [jails] import_tasks: tasks/jails.yml + + - name: Run ingress jail tasks + tags: [jail_ingress] + import_tasks: tasks/jail_ingress.yml diff --git a/tasks/jail_datasets.yml b/tasks/jail_datasets.yml deleted file mode 100644 index 9466c46..0000000 --- a/tasks/jail_datasets.yml +++ /dev/null @@ -1,14 +0,0 @@ -- name: "Check if dataset {{ dataset.name }} exists" - shell: zfs list -o name | grep -Fxq "{{ dataset.name }}" - changed_when: false - failed_when: false - register: check_dataset_exists - -- name: "Create ZFS dataset {{ dataset.name }}" - shell: | - {% if dataset.mountpoint is defined %} - zfs create -o "mountpoint={{ dataset.mountpoint }}" -p "{{ dataset.name }}" - {% else %} - zfs create -p "{{ dataset.name }}" - {% endif %} - when: check_dataset_exists.rc != 0 diff --git a/tasks/jail_single.yml b/tasks/jail_single.yml deleted file mode 100644 index 9d539d0..0000000 --- a/tasks/jail_single.yml +++ /dev/null @@ -1,22 +0,0 @@ -- name: "Check if jail directory for {{ jail.name }} exists" - shell: zfs list -o name | grep -Fxq "zroot/jails/containers/{{ jail.name }}" - failed_when: false - changed_when: false - register: check_jail_directory - -- name: "ZFS clone snapshot to jail directory for {{ jail.name }}" - shell: zfs clone zroot/jails/templates/{{ jail_userland_version }}@base "zroot/jails/containers/{{ jail.name }}" - when: check_jail_directory.rc != 0 - -- name: Make resolv.conf immutable - shell: chflags schg /usr/local/jails/containers/{{ jail.name }}/etc/resolv.conf - -- name: "Check if jail {{ jail.name }} is running" - shell: jls -j "{{ jail.name }}" - failed_when: false - changed_when: false - register: check_jail_active - -- name: "(Re)start jail {{ jail.name }}" - shell: service jail restart "{{ jail.name }}" - when: check_jail_active.rc != 0 diff --git a/tasks/jails.yml b/tasks/jails.yml index 7cbe707..99eb938 100644 --- a/tasks/jails.yml +++ b/tasks/jails.yml @@ -15,7 +15,7 @@ - { name: "zroot/jails/containers" } loop_control: loop_var: dataset - include_tasks: jail_datasets.yml + include_tasks: jails_dataset.yml - name: Check if userland snapshot already exists shell: zfs list -t snapshot -o name | grep -Fxq "zroot/jails/templates/{{ jail_userland_version }}@base" @@ -71,7 +71,4 @@ loop: "{{ jails }}" loop_control: loop_var: jail - include_tasks: jail_single.yml - -- name: Configure ingress jail - include_tasks: jail_ingress.yml + include_tasks: jails_single.yml diff --git a/tasks/jails_dataset.yml b/tasks/jails_dataset.yml new file mode 100644 index 0000000..9466c46 --- /dev/null +++ b/tasks/jails_dataset.yml @@ -0,0 +1,14 @@ +- name: "Check if dataset {{ dataset.name }} exists" + shell: zfs list -o name | grep -Fxq "{{ dataset.name }}" + changed_when: false + failed_when: false + register: check_dataset_exists + +- name: "Create ZFS dataset {{ dataset.name }}" + shell: | + {% if dataset.mountpoint is defined %} + zfs create -o "mountpoint={{ dataset.mountpoint }}" -p "{{ dataset.name }}" + {% else %} + zfs create -p "{{ dataset.name }}" + {% endif %} + when: check_dataset_exists.rc != 0 diff --git a/tasks/jails_single.yml b/tasks/jails_single.yml new file mode 100644 index 0000000..9d539d0 --- /dev/null +++ b/tasks/jails_single.yml @@ -0,0 +1,22 @@ +- name: "Check if jail directory for {{ jail.name }} exists" + shell: zfs list -o name | grep -Fxq "zroot/jails/containers/{{ jail.name }}" + failed_when: false + changed_when: false + register: check_jail_directory + +- name: "ZFS clone snapshot to jail directory for {{ jail.name }}" + shell: zfs clone zroot/jails/templates/{{ jail_userland_version }}@base "zroot/jails/containers/{{ jail.name }}" + when: check_jail_directory.rc != 0 + +- name: Make resolv.conf immutable + shell: chflags schg /usr/local/jails/containers/{{ jail.name }}/etc/resolv.conf + +- name: "Check if jail {{ jail.name }} is running" + shell: jls -j "{{ jail.name }}" + failed_when: false + changed_when: false + register: check_jail_active + +- name: "(Re)start jail {{ jail.name }}" + shell: service jail restart "{{ jail.name }}" + when: check_jail_active.rc != 0 -- cgit v1.3