tripleo_container_manage: trim down ansible tasks

- In container_running, replace 2 tasks by one task
- In podman/create, move the check_exit_code tasks into its own playbook
- Rework podman/systemd to only be included if systemd services are
  needed by the container configs and also reduce the tasks

Change-Id: Ief05797caf12084d7c1432bea037ccd56107dcde
This commit is contained in:
Emilien Macchi 2020-04-17 15:48:06 -04:00
parent db4a5dbca2
commit def911774d
5 changed files with 89 additions and 72 deletions

View File

@ -14,16 +14,12 @@
# License for the specific language governing permissions and limitations
# under the License.
#
- name: "Get {{ lookup('dict', container_exists_data).value.command.0 }} container status"
set_fact:
container_running: >-
{{ podman_containers.containers | selectattr('Name', 'equalto', lookup('dict', container_exists_data).value.command.0) |
map(attribute='State.Running') | first | default(false) }}
- name: "Fail if {{ lookup('dict', container_exists_data).value.command.0 }} is not running"
fail:
msg: >-
- name: "Assert that {{ lookup('dict', container_exists_data).value.command.0 }} is running before exec"
assert:
that:
- podman_containers.containers | selectattr('Name', 'equalto', lookup('dict', container_exists_data).value.command.0) |
map(attribute='State.Running') | first | default(false)
fail_msg: >-
Can't run container exec for {{ lookup('dict', container_exists_data).key }},
{{ lookup('dict', container_exists_data).value.command.0 }} is not running
when:
- not container_running|default(false)|bool
success_msg: "{{ lookup('dict', container_exists_data).value.command.0 }} is running"

View File

@ -22,5 +22,8 @@
- name: "Manage container systemd services and healthchecks for {{ tripleo_container_manage_config }}"
include_tasks: podman/systemd.yml
vars:
container_config: "{{ all_containers_hash | dict_to_list | haskey(attribute='restart', value=['always','unless-stopped'], any=True) | default([]) }}"
when:
- tripleo_container_manage_cli == 'podman'
- (container_config|length) > 0

View File

@ -0,0 +1,51 @@
---
# Copyright 2020 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
- name: "Wait for containers to be exited"
podman_container_info:
name: "{{ batched_container_data | haskey(attribute='action', reverse=True) | list_of_keys }}"
register: podman_containers_infos
until: ( podman_containers_infos.containers | selectattr('State.Running', 'equalto', True) |list|length ) == 0
# Retry 30 times every 10 seconds so we wait 5 min in total
retries: 30
delay: 10
# We need to ignore the failures since later we print some debug.
# We can't use "rescue" here because the debug tasks use
# "podman_containers_infos".
failed_when: false
no_log: "{{ not tripleo_container_manage_debug }}"
- name: Create a list of containers which didn't exit
set_fact:
running_containers: >-
{{ podman_containers_infos.containers |
selectattr('State.Running', 'equalto', True) | map(attribute='Name') | list }}
- name: Create a list of containers with bad Exit Codes
set_fact:
broken_containers: >-
{{ podman_containers_infos.containers |
rejectattr('State.ExitCode', 'in', tripleo_container_manage_valid_exit_code) | map(attribute='Name') | list }}
- name: "Print running containers"
fail:
msg: "Container(s) which are still running after 5 min: {{ running_containers }}, check logs in /var/log/containers/stdouts/"
when: running_containers|length != 0
- name: "Print failing containers"
fail:
msg: "Container(s) with bad ExitCode: {{ broken_containers }}, check logs in /var/log/containers/stdouts/"
when: broken_containers|length != 0

View File

@ -122,35 +122,4 @@
when:
- tripleo_container_manage_valid_exit_code|length != 0
- not ansible_check_mode|bool
block:
- name: "Wait for containers to be exited"
podman_container_info:
name: "{{ batched_container_data | haskey(attribute='action', reverse=True) | list_of_keys }}"
register: podman_containers_infos
until: ( podman_containers_infos.containers | selectattr('State.Running', 'equalto', True) |list|length ) == 0
# Retry 30 times every 10 seconds so we wait 5 min in total
retries: 30
delay: 10
# We need to ignore the failures since later we print some debug.
# We can't use "rescue" here because the debug tasks use
# "podman_containers_infos".
failed_when: false
no_log: "{{ not tripleo_container_manage_debug }}"
- name: Create a list of containers which didn't exit
set_fact:
running_containers: >-
{{ podman_containers_infos.containers |
selectattr('State.Running', 'equalto', True) | map(attribute='Name') | list }}
- name: Create a list of containers with bad Exit Codes
set_fact:
broken_containers: >-
{{ podman_containers_infos.containers |
rejectattr('State.ExitCode', 'in', tripleo_container_manage_valid_exit_code) | map(attribute='Name') | list }}
- name: "Print running containers"
fail:
msg: "Container(s) which are still running after 5 min: {{ running_containers }}, check logs in /var/log/containers/stdouts/"
when: running_containers|length != 0
- name: "Print failing containers"
fail:
msg: "Container(s) with bad ExitCode: {{ broken_containers }}, check logs in /var/log/containers/stdouts/"
when: broken_containers|length != 0
include_tasks: podman/check_exit_code.yml

View File

@ -14,36 +14,34 @@
# License for the specific language governing permissions and limitations
# under the License.
- name: Set container_config fact
set_fact:
container_config: "{{ all_containers_hash | dict_to_list | haskey(attribute='restart', value=['always','unless-stopped'], any=True) | default([]) }}"
- name: "Manage systemd files"
- name: "Remove trailing .requires"
no_log: "{{ not tripleo_container_manage_debug }}"
block:
- name: "Remove trailing .requires"
file:
path: "/etc/systemd/system/tripleo_{{ lookup('dict', container_data_requires).key }}.requires"
state: absent
loop: "{{ container_config }}"
loop_control:
loop_var: container_data_requires
- name: "Cleanup systemd healthchecks"
when:
- not tripleo_container_manage_healthcheck_disabled
include: podman/stat_healthcheck.yml container_systemd_healthcheck_name="{{ lookup('dict', item).key }}"
loop: "{{ container_config }}"
- name: "Create systemd services files"
template:
src: systemd-service.j2
dest: "/etc/systemd/system/tripleo_{{ lookup('dict', container_data_unit).key }}.service"
mode: '0644'
owner: root
group: root
register: systemd_file
loop: "{{ container_config }}"
loop_control:
loop_var: container_data_unit
file:
path: "/etc/systemd/system/tripleo_{{ lookup('dict', container_data_requires).key }}.requires"
state: absent
loop: "{{ container_config }}"
loop_control:
loop_var: container_data_requires
- name: "Cleanup systemd healthchecks"
no_log: "{{ not tripleo_container_manage_debug }}"
when:
- not tripleo_container_manage_healthcheck_disabled
include: podman/stat_healthcheck.yml container_systemd_healthcheck_name="{{ lookup('dict', item).key }}"
loop: "{{ container_config }}"
- name: "Create systemd services files"
no_log: "{{ not tripleo_container_manage_debug }}"
template:
src: systemd-service.j2
dest: "/etc/systemd/system/tripleo_{{ lookup('dict', container_data_unit).key }}.service"
mode: '0644'
owner: root
group: root
register: systemd_file
loop: "{{ container_config }}"
loop_control:
loop_var: container_data_unit
- name: "Force systemd daemon reload if a systemd file changed"
systemd: