| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2019 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
|
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: podman_image_info |
| 12 |
author: |
| 13 |
- Sam Doran (@samdoran) |
| 14 |
short_description: Gather info about images using podman |
| 15 |
notes: |
| 16 |
- Podman may required elevated privileges in order to run properly. |
| 17 |
description: |
| 18 |
- Gather info about images using C(podman) |
| 19 |
options: |
| 20 |
executable: |
| 21 |
description: |
| 22 |
- Path to C(podman) executable if it is not in the C($PATH) on the machine running C(podman) |
| 23 |
default: 'podman' |
| 24 |
type: str |
| 25 |
name: |
| 26 |
description: |
| 27 |
- List of tags or UID to gather info about. If no name is given return info about all images. |
| 28 |
type: list |
| 29 |
elements: str |
| 30 |
|
| 31 |
""" |
| 32 |
|
| 33 |
EXAMPLES = r""" |
| 34 |
- name: Gather info for all images |
| 35 |
containers.podman.podman_image_info: |
| 36 |
|
| 37 |
- name: Gather info on a specific image |
| 38 |
containers.podman.podman_image_info: |
| 39 |
name: nginx |
| 40 |
|
| 41 |
- name: Gather info on several images |
| 42 |
containers.podman.podman_image_info: |
| 43 |
name: |
| 44 |
- redis |
| 45 |
- quay.io/bitnami/wildfly |
| 46 |
""" |
| 47 |
|
| 48 |
RETURN = r""" |
| 49 |
images: |
| 50 |
description: info from all or specified images |
| 51 |
returned: always |
| 52 |
type: list |
| 53 |
sample: [ |
| 54 |
{ |
| 55 |
"Id": "029bed813f07be84fae0344cbf8076ced5ea3c929d5f064ba617ac7d8c610a4b", |
| 56 |
"Digest": "sha256:3362f865019db5f14ac5154cb0db2c3741ad1cce0416045be422ad4de441b081", |
| 57 |
"RepoTags": [ |
| 58 |
"docker.io/library/alpine:3.15", |
| 59 |
"quay.io/podman/alpine:3.15", |
| 60 |
"quay.io/podman/alpine:3.15" |
| 61 |
], |
| 62 |
"RepoDigests": [ |
| 63 |
"docker.io/library/alpine@sha256:3362f865019db5f14ac5154cb0db2c3741ad1cce0416045be422ad4de441b081", |
| 64 |
"docker.io/library/alpine@sha256:c58a2fce65cb3487f965d2fb08eec4843384dbe29264f427b665421db1aabef2", |
| 65 |
"quay.io/podman/alpine@sha256:3362f865019db5f14ac5154cb0db2c3741ad1cce0416045be422ad4de441b081", |
| 66 |
"quay.io/podman/alpine@sha256:c58a2fce65cb3487f965d2fb08eec4843384dbe29264f427b665421db1aabef2", |
| 67 |
"quay.io/podman/alpine@sha256:3362f865019db5f14ac5154cb0db2c3741ad1cce0416045be422ad4de441b081", |
| 68 |
"quay.io/podman/alpine@sha256:c58a2fce65cb3487f965d2fb08eec4843384dbe29264f427b665421db1aabef2" |
| 69 |
], |
| 70 |
"Parent": "", |
| 71 |
"Comment": "", |
| 72 |
"Created": "2023-06-14T20:42:13.993588153Z", |
| 73 |
"Config": { |
| 74 |
"Env": [ |
| 75 |
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" |
| 76 |
], |
| 77 |
"Cmd": [ |
| 78 |
"/bin/sh" |
| 79 |
] |
| 80 |
}, |
| 81 |
"Version": "20.10.23", |
| 82 |
"Author": "", |
| 83 |
"Architecture": "amd64", |
| 84 |
"Os": "linux", |
| 85 |
"Size": 11745350, |
| 86 |
"VirtualSize": 11745350, |
| 87 |
"GraphDriver": { |
| 88 |
"Name": "overlay", |
| 89 |
"Data": { |
| 90 |
"UpperDir": "/home/podman/.local/share/containers/storage/overlay/579b.../diff", |
| 91 |
"WorkDir": "/home/podman/.local/share/containers/storage/overlay/579b.../work" |
| 92 |
} |
| 93 |
}, |
| 94 |
"RootFS": { |
| 95 |
"Type": "layers", |
| 96 |
"Layers": [ |
| 97 |
"sha256:579b..." |
| 98 |
] |
| 99 |
}, |
| 100 |
"Labels": null, |
| 101 |
"Annotations": {}, |
| 102 |
"ManifestType": "application/vnd.docker.distribution.manifest.v2+json", |
| 103 |
"User": "", |
| 104 |
"History": [ |
| 105 |
{ |
| 106 |
"created": "2023-06-14T20:42:13.893052319Z", |
| 107 |
"created_by": "/bin/sh -c #(nop) ADD file:234234234 in / " |
| 108 |
}, |
| 109 |
{ |
| 110 |
"created": "2023-06-14T20:42:13.993588153Z", |
| 111 |
"created_by": "/bin/sh -c #(nop) CMD [\"/bin/sh\"]", |
| 112 |
"empty_layer": true |
| 113 |
} |
| 114 |
], |
| 115 |
"NamesHistory": [ |
| 116 |
"quay.io/podman/alpine:3.15", |
| 117 |
"quay.io/podman/alpine:3.15", |
| 118 |
"docker.io/library/alpine:3.15" |
| 119 |
] |
| 120 |
} |
| 121 |
] |
| 122 |
""" |
| 123 |
|
| 124 |
import json |
| 125 |
|
| 126 |
from ansible.module_utils.basic import AnsibleModule |
| 127 |
|
| 128 |
|
| 129 |
def image_exists(module, executable, name): |
| 130 |
command = [executable, "image", "exists", name] |
| 131 |
rc, out, err = module.run_command(command) |
| 132 |
if rc == 1: |
| 133 |
return False |
| 134 |
elif 'Command "exists" not found' in err: |
| 135 |
# The 'exists' test is available in podman >= 0.12.1 |
| 136 |
command = [executable, "image", "ls", "-q", name] |
| 137 |
rc2, out2, err2 = module.run_command(command) |
| 138 |
if rc2 != 0: |
| 139 |
return False |
| 140 |
return True |
| 141 |
|
| 142 |
|
| 143 |
def filter_invalid_names(module, executable, name): |
| 144 |
valid_names = [] |
| 145 |
names = name |
| 146 |
if not isinstance(name, list): |
| 147 |
names = [name] |
| 148 |
|
| 149 |
for name in names: |
| 150 |
if image_exists(module, executable, name): |
| 151 |
valid_names.append(name) |
| 152 |
|
| 153 |
return valid_names |
| 154 |
|
| 155 |
|
| 156 |
def get_image_info(module, executable, name): |
| 157 |
names = name |
| 158 |
if not isinstance(name, list): |
| 159 |
names = [name] |
| 160 |
|
| 161 |
if len(names) > 0: |
| 162 |
command = [executable, "image", "inspect"] |
| 163 |
command.extend(names) |
| 164 |
rc, out, err = module.run_command(command) |
| 165 |
|
| 166 |
if rc != 0: |
| 167 |
module.fail_json(msg="Unable to gather info for '{0}': {1}".format(", ".join(names), err)) |
| 168 |
return out |
| 169 |
|
| 170 |
else: |
| 171 |
return json.dumps([]) |
| 172 |
|
| 173 |
|
| 174 |
def get_all_image_info(module, executable): |
| 175 |
command = [executable, "image", "ls", "-q"] |
| 176 |
rc, out, err = module.run_command(command) |
| 177 |
out = out.strip() |
| 178 |
if out: |
| 179 |
name = out.split("\n") |
| 180 |
res = get_image_info(module, executable, name) |
| 181 |
return res |
| 182 |
return json.dumps([]) |
| 183 |
|
| 184 |
|
| 185 |
def main(): |
| 186 |
module = AnsibleModule( |
| 187 |
argument_spec=dict( |
| 188 |
executable=dict(type="str", default="podman"), |
| 189 |
name=dict(type="list", elements="str"), |
| 190 |
), |
| 191 |
supports_check_mode=True, |
| 192 |
) |
| 193 |
|
| 194 |
executable = module.params["executable"] |
| 195 |
name = module.params.get("name") |
| 196 |
executable = module.get_bin_path(executable, required=True) |
| 197 |
|
| 198 |
if name: |
| 199 |
valid_names = filter_invalid_names(module, executable, name) |
| 200 |
results = json.loads(get_image_info(module, executable, valid_names)) |
| 201 |
else: |
| 202 |
results = json.loads(get_all_image_info(module, executable)) |
| 203 |
|
| 204 |
results = dict(changed=False, images=results) |
| 205 |
|
| 206 |
module.exit_json(**results) |
| 207 |
|
| 208 |
|
| 209 |
if __name__ == "__main__": |
| 210 |
main() |