--- # The two opt-in units for a host that runs Docker with live-restore. Each is installed when its input is true and removed when it is false. - name: Install the boot unit that starts containers Docker skipped when: docker_start_missed | bool tags: [docker, configure] block: - name: Install the script that starts containers Docker skipped at boot become: true ansible.builtin.copy: src: docker-start-missed dest: /usr/local/sbin/docker-start-missed owner: root group: root mode: "0755" tags: [docker, configure] # Type=simple, not oneshot: a oneshot unit in multi-user.target holds the boot until it finishes, which can take minutes. A failure # still shows in `systemctl --failed`, and the target only Wants this unit, so it never fails the boot. No Wants= on Docker: a host # whose operator keeps Docker disabled must not get it started by this unit. - name: Install the unit that starts containers Docker skipped at boot become: true ansible.builtin.copy: dest: /etc/systemd/system/docker-start-missed.service owner: root group: root mode: "0644" content: | [Unit] Description=Start containers that Docker skipped at boot After={{ docker_service }}.service ConditionPathExists=/usr/bin/docker [Service] Type=simple ExecStart=/usr/local/sbin/docker-start-missed --settle {{ docker_start_missed_settle_seconds }} [Install] WantedBy=multi-user.target register: __docker_start_missed_unit tags: [docker, configure] # Not in check mode: on the first run the unit file above is only simulated, so systemd would not find it. - name: Enable the unit that starts containers Docker skipped at boot become: true ansible.builtin.systemd: name: docker-start-missed.service enabled: true daemon_reload: true when: not ansible_check_mode tags: [docker, configure] - name: Take out the unit that starts containers Docker skipped ansible.builtin.include_tasks: unit_absent.yml vars: __docker_unit_name: docker-start-missed __docker_unit_script: /usr/local/sbin/docker-start-missed when: not (docker_start_missed | bool) tags: [docker, configure] - name: Install the shutdown unit that ends containers while the network is up when: docker_stop_at_shutdown | bool tags: [docker, configure] block: - name: Install the script that ends containers at shutdown become: true ansible.builtin.copy: src: docker-stop-at-shutdown dest: /usr/local/sbin/docker-stop-at-shutdown owner: root group: root mode: "0755" tags: [docker, configure] # Stop order is the reverse of start order: Before= makes docker stop first, so no restart policy can interfere, and After= the network # and remote file systems makes this unit stop before them, while NFS can still be reached. The work is the stop action, which systemd # runs only for a unit that is active: RemainAfterExit keeps it active after its start action, and the role starts it below. - name: Install the unit that ends containers at shutdown become: true ansible.builtin.copy: dest: /etc/systemd/system/docker-stop-at-shutdown.service owner: root group: root mode: "0644" content: | [Unit] Description=Stop containers while the network and NFS shares are still up After=network-online.target remote-fs.target Before={{ docker_service }}.service ConditionPathExists=/usr/bin/docker [Service] Type=oneshot RemainAfterExit=yes ExecStart=/bin/true Environment=DOCKER_STOP_TIMEOUT={{ docker_stop_at_shutdown_timeout }} ExecStop=/usr/local/sbin/docker-stop-at-shutdown TimeoutStopSec={{ (docker_stop_at_shutdown_timeout | int) + 30 }} [Install] WantedBy=multi-user.target register: __docker_stop_at_shutdown_unit tags: [docker, configure] # Started now, so the first shutdown after this run is covered without a reboot. Not in check mode: the unit file above is only # simulated there, and a dry run must not start anything. A changed unit is not restarted, because stopping it runs its stop action, # which ends every container. - name: Enable and start the unit that ends containers at shutdown become: true ansible.builtin.systemd: name: docker-stop-at-shutdown.service enabled: true state: started daemon_reload: true when: not ansible_check_mode tags: [docker, configure] - name: Take out the shutdown unit that ends containers ansible.builtin.include_tasks: unit_absent.yml vars: __docker_unit_name: docker-stop-at-shutdown __docker_unit_script: /usr/local/sbin/docker-stop-at-shutdown when: not (docker_stop_at_shutdown | bool) tags: [docker, configure]