From 85836e311e6547ab6e7989dc7885be19a5fce022 Mon Sep 17 00:00:00 2001 From: "Andreas B. Mundt" Date: Fri, 12 Nov 2021 20:23:58 +0100 Subject: [PATCH] Move container restart task to handlers. --- roles/educontainer/handlers/main.yml | 7 +++++++ roles/educontainer/tasks/main.yml | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 roles/educontainer/handlers/main.yml diff --git a/roles/educontainer/handlers/main.yml b/roles/educontainer/handlers/main.yml new file mode 100644 index 0000000..44ccc8e --- /dev/null +++ b/roles/educontainer/handlers/main.yml @@ -0,0 +1,7 @@ +- name: enable and restart containers + systemd: + name: systemd-nspawn@{{ contname }}{{ "%02d" | format(item|int) }}.service + state: restarted + enabled: yes + loop: "{{ containers }}" + listen: enable and restart containers diff --git a/roles/educontainer/tasks/main.yml b/roles/educontainer/tasks/main.yml index 3ce27c5..3e6f1f8 100644 --- a/roles/educontainer/tasks/main.yml +++ b/roles/educontainer/tasks/main.yml @@ -6,7 +6,7 @@ ## container 1: ssh → host port 10001, HTTP → 10101 ## ... ... ... ## -## User 'ansible' in the sudo group. Password is empty. +## User 'ansible' in the sudo group. Password is {{ contpwd }}. ## - name: stop all containers @@ -68,6 +68,7 @@ creates: /var/lib/machines/{{ contname }}00 environment: http_proxy: http://localhost:3142 + notify: enable and restart containers - name: provide complete apt sources copy: @@ -127,6 +128,7 @@ - multi-user.target.wants - sockets.target.wants - network-online.target.wants + notify: enable and restart containers - name: enable systemd-networkd service file: @@ -139,6 +141,9 @@ - { src: systemd-networkd.service, dest: multi-user.target.wants/systemd-networkd.service } - { src: systemd-networkd.socket, dest: sockets.target.wants/systemd-networkd.socket } - { src: systemd-networkd-wait-online.service, dest: network-online.target.wants/systemd-networkd-wait-online.service } + notify: enable and restart containers + +######## - name: generate links to the initial container file: @@ -146,28 +151,25 @@ dest: /var/lib/machines/{{ contname }}{{ "%02d" | format(item|int) }} state: link loop: "{{ containers[1:] }}" + notify: enable and restart containers - name: provide container configuration template: src: contcfg.nspawn.j2 dest: /etc/systemd/nspawn/{{ contname}}{{ "%02d" | format(item|int) }}.nspawn loop: "{{ containers }}" + notify: enable and restart containers - name: provide container hostname file template: src: hostname.j2 dest: /var/lib/machines/{{ contname }}{{ "%02d" | format(item|int) }}.hostname loop: "{{ containers }}" + notify: enable and restart containers - name: provide container hosts file template: src: hosts.j2 dest: /var/lib/machines/{{ contname }}{{ "%02d" | format(item|int) }}.hosts loop: "{{ containers }}" - -- name: start all containers on boot and restart now - systemd: - name: systemd-nspawn@{{ contname }}{{ "%02d" | format(item|int) }}.service - state: restarted - enabled: yes - loop: "{{ containers }}" + notify: enable and restart containers