diff --git a/tripleo_ansible/ansible_plugins/action/podman_containers.py b/tripleo_ansible/ansible_plugins/action/podman_containers.py new file mode 100644 index 000000000..f1ae990e2 --- /dev/null +++ b/tripleo_ansible/ansible_plugins/action/podman_containers.py @@ -0,0 +1,84 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# 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. + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from ansible.plugins.action import ActionBase +from ansible.utils.display import Display + +DISPLAY = Display() + + +class ActionModule(ActionBase): + """Action plugin for podman_container module""" + + _VALID_ARGS = frozenset(( + 'containers', + )) + + def __init__(self, *args, **kwargs): + super(ActionModule, self).__init__(*args, **kwargs) + + def run(self, tmp=None, task_vars=None): + self._supports_check_mode = True + self._supports_async = True + del tmp # tmp no longer has any effect + if task_vars is None: + task_vars = {} + + if 'containers' not in self._task.args: + return {'failed': True, + 'msg': 'Task must have "containers" argument!'} + containers = self._task.args.get('containers') + if not containers: + return {'failed': True, + 'msg': 'Task must have non empty "containers" argument!'} + DISPLAY.vvvv('Running for containers: %s' % str(containers)) + wrap_async = self._task.async_val and ( + not self._connection.has_native_async) + results = [self._execute_module( + module_name='podman_container', + module_args=container, + task_vars=task_vars, wrap_async=wrap_async + ) for container in containers] + + changed = any([i.get('changed', False) for i in results]) + skipped = all([i.get('skipped', False) for i in results]) + failed = any([i.get('failed', False) for i in results]) + + try: + if not wrap_async: + # remove a temporary path we created + self._remove_tmp_path(self._connection._shell.tmpdir) + except Exception: + pass + finally: + if skipped: + return {'results': results, + 'changed': False, + 'skipped': skipped} + if failed: + msg = "\n".join([i.get('msg', '') for i in results]) + return {'results': results, + 'changed': changed, + 'failed': failed, + 'msg': msg} + return {'results': results, + 'changed': changed, + 'failed': False, + 'msg': 'All items completed'} diff --git a/tripleo_ansible/ansible_plugins/modules/podman_containers.py b/tripleo_ansible/ansible_plugins/modules/podman_containers.py new file mode 100644 index 000000000..ab5ba079c --- /dev/null +++ b/tripleo_ansible/ansible_plugins/modules/podman_containers.py @@ -0,0 +1,61 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# 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. + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = { + 'metadata_version': '1.0', + 'status': ['preview'], + 'supported_by': 'community' +} + +DOCUMENTATION = ''' +--- +module: podman_containers +author: + - "Sagi Shnaidman (@sshnaidm)" +version_added: '2.9' +short_description: Manage podman containers in a batch +notes: [] +description: + - Manage groups of podman containers +requirements: + - "Podman installed on host" +options: + containers: + description: + - List of dictionaries with data for running containers + for podman_container module. + required: True + type: list + elements: dict +''' + +EXAMPLES = ''' +- name: Run three containers at once + podman_containers: + containers: + - name: alpine + image: alpine + command: sleep 1d + - name: web + image: nginx + - name: test + image: python:3-alpine + command: python -V +''' diff --git a/tripleo_ansible/ansible_plugins/tests/molecule/podman_container/converge.yml b/tripleo_ansible/ansible_plugins/tests/molecule/podman_container/converge.yml index adbe6f7db..64e88d716 100644 --- a/tripleo_ansible/ansible_plugins/tests/molecule/podman_container/converge.yml +++ b/tripleo_ansible/ansible_plugins/tests/molecule/podman_container/converge.yml @@ -423,6 +423,18 @@ name: testidem-pod state: absent + - name: Run three containers at once + podman_containers: + containers: + - name: alpine + image: alpine + command: sleep 1d + - name: web + image: nginx + - name: test + image: python:3-alpine + command: python -V + always: - name: Delete all container leftovers from tests podman_container: @@ -432,6 +444,9 @@ - "alpine:3.7" - "container" - "container2" + - "alpine" + - "web" + - "test" - name: Remove pod shell: podman pod rm -f testidempod