Adds container state to container role.

This commit is contained in:
Emil 2024-03-06 02:08:32 +01:00
parent 4dcdf45a12
commit 8daf94c1be
2 changed files with 15 additions and 1 deletions

View File

@ -1,2 +1,3 @@
--- ---
lte_debian_version: bookworm lte_debian_version: bookworm
lte_container_state: present

View File

@ -3,17 +3,30 @@
ansible.builtin.command: incus ls {{ lte_container_name }} ansible.builtin.command: incus ls {{ lte_container_name }}
register: container_state register: container_state
changed_when: false 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 - name: launch debian bookworm linux container
ansible.builtin.shell: ansible.builtin.shell:
cmd: | cmd: |
incus launch \ incus launch \
images:debian/{{ lte_debian_version }}/cloud \ images:debian/{{ lte_debian_version }}/cloud \
{{ lte_container_name }} {{ 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 - name: update apt cache and upgrade system packages
ansible.builtin.shell: ansible.builtin.shell:
cmd: |- cmd: |-
incus exec {{ lte_container_name }} -- \ incus exec {{ lte_container_name }} -- \
bash -c \ bash -c \
'/usr/bin/apt update && /usr/bin/apt upgrade -y' '/usr/bin/apt update && /usr/bin/apt upgrade -y'
when: lte_container_state == "present"
changed_when: false changed_when: false