mirror of
https://github.com/containers/ansible-podman-collections.git
synced 2026-02-04 07:11:49 +00:00
Fixes #328 Co-authored-by: Christian Bourque <christian.bourque@expretio.com>
This commit is contained in:
parent
3e7f7a00a0
commit
f2882ecb19
2 changed files with 127 additions and 0 deletions
91
plugins/modules/podman_tag.py
Normal file
91
plugins/modules/podman_tag.py
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
#!/usr/bin/python
|
||||
# coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2021, Sagi Shnaidman <sshnaidm@redhat.com>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
DOCUMENTATION = r'''
|
||||
module: podman_tag
|
||||
short_description: Add an additional name to a local image
|
||||
author: Christian Bourque (@ocafebabe)
|
||||
description:
|
||||
- podman tag adds one or more additional names to locally-stored image.
|
||||
options:
|
||||
image:
|
||||
description:
|
||||
- Image to tag.
|
||||
type: str
|
||||
required: true
|
||||
target_names:
|
||||
description:
|
||||
- Additional names.
|
||||
type: list
|
||||
elements: str
|
||||
required: true
|
||||
executable:
|
||||
description:
|
||||
- Path to C(podman) executable if it is not in the C($PATH) on the
|
||||
machine running C(podman)
|
||||
default: 'podman'
|
||||
type: str
|
||||
requirements:
|
||||
- "Podman installed on host"
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
# What modules does for example
|
||||
- containers.podman.podman_tag:
|
||||
image: docker.io/continuumio/miniconda3
|
||||
target_names:
|
||||
- miniconda3
|
||||
- miniconda
|
||||
'''
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule # noqa: E402
|
||||
|
||||
|
||||
def tag(module, executable):
|
||||
changed = False
|
||||
command = [executable, 'tag']
|
||||
command.append(module.params['image'])
|
||||
command.extend(module.params['target_names'])
|
||||
if module.check_mode:
|
||||
return changed, '', ''
|
||||
rc, out, err = module.run_command(command)
|
||||
if rc == 0:
|
||||
changed = True
|
||||
else:
|
||||
module.fail_json(msg="Error tagging local image %s: %s" % (
|
||||
module.params['image'], err))
|
||||
return changed, out, err
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
image=dict(type='str', required=True),
|
||||
target_names=dict(type='list', elements='str', required=True),
|
||||
executable=dict(type='str', default='podman')
|
||||
),
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
executable = module.get_bin_path(module.params['executable'], required=True)
|
||||
changed, out, err = tag(module, executable)
|
||||
|
||||
results = {
|
||||
"changed": changed,
|
||||
"stdout": out,
|
||||
"stderr": err,
|
||||
}
|
||||
module.exit_json(**results)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
36
tests/integration/targets/podman_tag/tasks/main.yml
Normal file
36
tests/integration/targets/podman_tag/tasks/main.yml
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
---
|
||||
- name: Test podman tag
|
||||
block:
|
||||
- name: Pull image
|
||||
containers.podman.podman_image:
|
||||
name: docker.io/library/openjdk:8-jdk-alpine
|
||||
|
||||
- name: Tag image
|
||||
containers.podman.podman_tag:
|
||||
image: docker.io/library/openjdk:8-jdk-alpine
|
||||
target_names:
|
||||
- openjdk8
|
||||
- jdk8
|
||||
|
||||
- name: Get tagged image info
|
||||
containers.podman.podman_image_info:
|
||||
name: docker.io/library/openjdk:8-jdk-alpine
|
||||
register: tagged_image_result
|
||||
|
||||
- name: Check results
|
||||
assert:
|
||||
that:
|
||||
- tagged_image_result.images | length == 1
|
||||
- "'docker.io/library/openjdk:8-jdk-alpine' in tagged_image_result.images[0]['RepoTags'][0]"
|
||||
- "'localhost/openjdk8:latest' in tagged_image_result.images[0]['RepoTags'][1]"
|
||||
- "'localhost/jdk8:latest' in tagged_image_result.images[0]['RepoTags'][2]"
|
||||
|
||||
always:
|
||||
- name: Cleanup image
|
||||
containers.podman.podman_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
loop:
|
||||
- docker.io/library/openjdk:8-jdk-alpine
|
||||
- localhost/openjdk8
|
||||
- localhost/jdk8
|
||||
Loading…
Add table
Add a link
Reference in a new issue