From 8daf94c1be2f87e67b7ed9be97723a29c23c7462 Mon Sep 17 00:00:00 2001 From: Emil Date: Wed, 6 Mar 2024 02:08:32 +0100 Subject: [PATCH] Adds container state to container role. --- roles/linux_container/defaults/main.yml | 1 + roles/linux_container/tasks/container.yml | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/roles/linux_container/defaults/main.yml b/roles/linux_container/defaults/main.yml index 2df44ba..85a16e8 100644 --- a/roles/linux_container/defaults/main.yml +++ b/roles/linux_container/defaults/main.yml @@ -1,2 +1,3 @@ --- lte_debian_version: bookworm +lte_container_state: present diff --git a/roles/linux_container/tasks/container.yml b/roles/linux_container/tasks/container.yml index 8e58a1f..9a90ea5 100644 --- a/roles/linux_container/tasks/container.yml +++ b/roles/linux_container/tasks/container.yml @@ -3,17 +3,30 @@ ansible.builtin.command: incus ls {{ lte_container_name }} register: container_state changed_when: false +- name: stop and remove running container + ansible.builtin.shell: + cmd: | + incus stop {{ lte_container_name }} && \ + incus rm {{ lte_container_name }} + when: > + lte_container_state == "absent" and + state.results[0].rc == 0 and + "RUNNING" in state.results[0].stdout - name: launch debian bookworm linux container ansible.builtin.shell: cmd: | incus launch \ images:debian/{{ lte_debian_version }}/cloud \ {{ lte_container_name }} - when: (container_state.rc == 1) or ("RUNNING" not in container_state.stdout) + when: > + lte_container_state == "present" and + container_state.rc == 1 or + "RUNNING" not in container_state.stdout - name: update apt cache and upgrade system packages ansible.builtin.shell: cmd: |- incus exec {{ lte_container_name }} -- \ bash -c \ '/usr/bin/apt update && /usr/bin/apt upgrade -y' + when: lte_container_state == "present" changed_when: false