Adds basic container state managementto linux_container role

This commit is contained in:
Emil 2024-03-06 02:35:07 +01:00
parent 8daf94c1be
commit e529037269
3 changed files with 18 additions and 19 deletions

View File

@ -1,18 +1,14 @@
--- ---
- name: Create - name: Destroy testing resources
hosts: localhost hosts: localhost
connection: local connection: local
gather_facts: false gather_facts: false
tasks: tasks:
- name: find current container state - name: Stop and remove the test container
ansible.builtin.command: incus ls "{{ item.name }}-container" ansible.builtin.include_role:
register: state name: >-
changed_when: false theliberatededge.test_linux_containers.linux_container
with_items: '{{ molecule_yml.platforms }}' vars:
- name: stop and remove running containers lte_container_state: absent
ansible.builtin.shell: lte_container_name: '{{ item.name }}-container'
cmd: |
incus stop "{{ item.name }}-container" && \
incus rm "{{ item.name }}-container"
when: state.results[0].rc == 0 and "RUNNING" in state.results[0].stdout
with_items: '{{ molecule_yml.platforms }}' with_items: '{{ molecule_yml.platforms }}'

View File

@ -9,9 +9,10 @@
incus stop {{ lte_container_name }} && \ incus stop {{ lte_container_name }} && \
incus rm {{ lte_container_name }} incus rm {{ lte_container_name }}
when: > when: >
lte_container_state == "absent" and lte_container_state == 'absent' and
state.results[0].rc == 0 and 'results' in container_state and
"RUNNING" in state.results[0].stdout container_state.results[0].rc == 0 and
'RUNNING' in container_state.results[0].stdout
- name: launch debian bookworm linux container - name: launch debian bookworm linux container
ansible.builtin.shell: ansible.builtin.shell:
cmd: | cmd: |
@ -19,14 +20,14 @@
images:debian/{{ lte_debian_version }}/cloud \ images:debian/{{ lte_debian_version }}/cloud \
{{ lte_container_name }} {{ lte_container_name }}
when: > when: >
lte_container_state == "present" and lte_container_state == 'present' and
container_state.rc == 1 or (container_state.rc == 1) or
"RUNNING" not in container_state.stdout ("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" when: lte_container_state == 'present'
changed_when: false changed_when: false

View File

@ -1,4 +1,6 @@
--- ---
- ansible.builtin.include_tasks: container.yml - ansible.builtin.include_tasks: container.yml
- ansible.builtin.include_tasks: ume.yml - ansible.builtin.include_tasks: ume.yml
when: lte_container_state == "present"
- ansible.builtin.include_tasks: openssh-server.yml - ansible.builtin.include_tasks: openssh-server.yml
when: lte_container_state == "present"