Create an action plugin for podman_container module

When we want to create a large set of containers at the same time, an
action plugin can now be used which will call the podman_container
module.

The podman_containers action plugin takes a list of containers with
pre-defined parameters ready to be consumed by the podman_container
module.

The podman_containers module is just useful for Ansible documentation.

The molecule tests will ensure that the action plugin actually works and
creates the 3 containers.

Change-Id: I1e6881f3871a7a125db9374c608ecad0bafcb882
This commit is contained in:
Sagi Shnaidman 2020-03-31 02:34:06 +03:00 committed by Emilien Macchi
parent 05e35ddbed
commit 86f1746120
3 changed files with 160 additions and 0 deletions

View File

@ -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'}

View File

@ -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
'''

View File

@ -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