summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSam Doran <sdoran@redhat.com>2019-06-26 17:04:26 -0400
committerToshio Kuratomi <a.badger@gmail.com>2019-06-28 09:16:24 -0700
commit8cc128b8f51ea6ab24418350f49f8be99918154b (patch)
tree8c319f014ae7230616f93dfb22a0fb48da96daeb /lib
parentab97f587648f2f88cae025df7c435f4e602102b5 (diff)
downloadansible-8cc128b8f51ea6ab24418350f49f8be99918154b.tar.gz
[stable-2.8] podman_image_info - Do not fail when nonexistant image name is provided (#57962)
* Account for older versions of Podman lacking 'exists' (cherry picked from commit 9ba7015458) Co-authored-by: Sam Doran <sdoran@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/ansible/modules/cloud/podman/podman_image_info.py50
1 files changed, 40 insertions, 10 deletions
diff --git a/lib/ansible/modules/cloud/podman/podman_image_info.py b/lib/ansible/modules/cloud/podman/podman_image_info.py
index 071a1cbbfc..f57869d495 100644
--- a/lib/ansible/modules/cloud/podman/podman_image_info.py
+++ b/lib/ansible/modules/cloud/podman/podman_image_info.py
@@ -154,20 +154,49 @@ import json
from ansible.module_utils.basic import AnsibleModule
-def get_image_info(module, executable, name):
-
+def image_exists(module, executable, name):
+ command = [executable, 'image', 'exists', name]
+ rc, out, err = module.run_command(command)
+ if rc == 1:
+ return False
+ elif 'Command "exists" not found' in err:
+ # The 'exists' test is available in podman >= 0.12.1
+ command = [executable, 'image', 'ls', '-q', name]
+ rc2, out2, err2 = module.run_command(command)
+ if rc2 != 0:
+ return False
+ return True
+
+
+def filter_invalid_names(module, executable, name):
+ valid_names = []
+ names = name
if not isinstance(name, list):
- name = [name]
+ names = [name]
- command = [executable, 'image', 'inspect']
- command.extend(name)
+ for name in names:
+ if image_exists(module, executable, name):
+ valid_names.append(name)
- rc, out, err = module.run_command(command)
+ return valid_names
- if rc != 0:
- module.fail_json(msg="Unable to gather info for '{0}': {1}".format(', '.join(name), err))
- return out
+def get_image_info(module, executable, name):
+ names = name
+ if not isinstance(name, list):
+ names = [name]
+
+ if len(names) > 0:
+ command = [executable, 'image', 'inspect']
+ command.extend(names)
+ rc, out, err = module.run_command(command)
+
+ if rc != 0:
+ module.fail_json(msg="Unable to gather info for '{0}': {1}".format(', '.join(names), err))
+ return out
+
+ else:
+ return json.dumps([])
def get_all_image_info(module, executable):
@@ -193,7 +222,8 @@ def main():
executable = module.get_bin_path(executable, required=True)
if name:
- results = json.loads(get_image_info(module, executable, name))
+ valid_names = filter_invalid_names(module, executable, name)
+ results = json.loads(get_image_info(module, executable, valid_names))
else:
results = json.loads(get_all_image_info(module, executable))