| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2020 Red Hat |
| 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_pod_info |
| 12 |
author: |
| 13 |
- "Sagi Shnaidman (@sshnaidm)" |
| 14 |
version_added: '1.0.0' |
| 15 |
short_description: Gather info about podman pods |
| 16 |
notes: [] |
| 17 |
description: |
| 18 |
- Gather info about podman pods with podman inspect command. |
| 19 |
requirements: |
| 20 |
- "Podman installed on host" |
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Name of the pod |
| 25 |
type: str |
| 26 |
executable: |
| 27 |
description: |
| 28 |
- Path to C(podman) executable if it is not in the C($PATH) on the |
| 29 |
machine running C(podman) |
| 30 |
default: 'podman' |
| 31 |
type: str |
| 32 |
""" |
| 33 |
|
| 34 |
EXAMPLES = r""" |
| 35 |
- name: Gather info about all present pods |
| 36 |
containers.podman.podman_pod_info: |
| 37 |
|
| 38 |
- name: Gather info about specific pods |
| 39 |
containers.podman.podman_pod_info: |
| 40 |
name: special_pod |
| 41 |
""" |
| 42 |
|
| 43 |
RETURN = r""" |
| 44 |
pods: |
| 45 |
description: Facts from all or specified pods |
| 46 |
returned: always |
| 47 |
type: list |
| 48 |
sample: [ |
| 49 |
{ |
| 50 |
"Id": "a99a41b8fa77d8c7ff1c432a7a21bc0c2afd8c13b94a9d9b9b19b66ae97920c1", |
| 51 |
"Name": "pod_name", |
| 52 |
"Created": "2024-05-28T20:41:09.946926613+03:00", |
| 53 |
"CreateCommand": [ |
| 54 |
"podman", |
| 55 |
"pod", |
| 56 |
"create", |
| 57 |
"--name", |
| 58 |
"pod_name", |
| 59 |
"--infra=True", |
| 60 |
"--userns", |
| 61 |
"auto", |
| 62 |
"--security-opt", |
| 63 |
"seccomp=unconfined", |
| 64 |
"--security-opt", |
| 65 |
"apparmor=unconfined", |
| 66 |
"--hostname", |
| 67 |
"mypod", |
| 68 |
"--dns", |
| 69 |
"1.1.1.2", |
| 70 |
"--label", |
| 71 |
"key=cval", |
| 72 |
"--label", |
| 73 |
"otherkey=kddkdk", |
| 74 |
"--label", |
| 75 |
"somekey=someval", |
| 76 |
"--add-host", |
| 77 |
"google:5.5.5.5", |
| 78 |
"--volume", |
| 79 |
"/tmp/test//:/data2" |
| 80 |
], |
| 81 |
"ExitPolicy": "continue", |
| 82 |
"State": "Created", |
| 83 |
"Hostname": "mypod", |
| 84 |
"Labels": { |
| 85 |
"key": "cval", |
| 86 |
"otherkey": "kddkdk", |
| 87 |
"somekey": "someval" |
| 88 |
}, |
| 89 |
"CreateCgroup": true, |
| 90 |
"CgroupParent": "user.slice", |
| 91 |
"CgroupPath": "user.slice/user-1000.slice/[email protected]/user.slice/....slice", |
| 92 |
"CreateInfra": true, |
| 93 |
"InfraContainerID": "37f960e6c8accc6b5b41945b1dcf03a28d3a366f7f37049748f18b21c44f577e", |
| 94 |
"InfraConfig": { |
| 95 |
"PortBindings": {}, |
| 96 |
"HostNetwork": false, |
| 97 |
"StaticIP": "", |
| 98 |
"StaticMAC": "", |
| 99 |
"NoManageResolvConf": false, |
| 100 |
"DNSServer": [ |
| 101 |
"1.1.1.2" |
| 102 |
], |
| 103 |
"DNSSearch": null, |
| 104 |
"DNSOption": null, |
| 105 |
"NoManageHosts": false, |
| 106 |
"HostAdd": [ |
| 107 |
"google:5.5.5.5" |
| 108 |
], |
| 109 |
"Networks": null, |
| 110 |
"NetworkOptions": null, |
| 111 |
"pid_ns": "private", |
| 112 |
"userns": "host", |
| 113 |
"uts_ns": "private" |
| 114 |
}, |
| 115 |
"SharedNamespaces": [ |
| 116 |
"user", |
| 117 |
"uts", |
| 118 |
"ipc", |
| 119 |
"net" |
| 120 |
], |
| 121 |
"NumContainers": 1, |
| 122 |
"Containers": [ |
| 123 |
{ |
| 124 |
"Id": "37f960e6c8accc6b5b41945b1dcf03a28d3a366f7f37049748f18b21c44f577e", |
| 125 |
"Name": "a99a49b8fa77-infra", |
| 126 |
"State": "created" |
| 127 |
} |
| 128 |
], |
| 129 |
"mounts": [ |
| 130 |
{ |
| 131 |
"Type": "bind", |
| 132 |
"Source": "/tmp/test", |
| 133 |
"Destination": "/data2", |
| 134 |
"Driver": "", |
| 135 |
"Mode": "", |
| 136 |
"Options": [ |
| 137 |
"nosuid", |
| 138 |
"nodev", |
| 139 |
"rbind" |
| 140 |
], |
| 141 |
"RW": true, |
| 142 |
"Propagation": "rprivate" |
| 143 |
} |
| 144 |
], |
| 145 |
"security_opt": [ |
| 146 |
"seccomp=unconfined", |
| 147 |
"apparmor=unconfined" |
| 148 |
], |
| 149 |
"LockNumber": 1 |
| 150 |
} |
| 151 |
] |
| 152 |
""" |
| 153 |
|
| 154 |
import json |
| 155 |
from ansible.module_utils.basic import AnsibleModule |
| 156 |
|
| 157 |
|
| 158 |
def get_pod_info(module, executable, name): |
| 159 |
command = [executable, "pod", "inspect"] |
| 160 |
pods = [name] |
| 161 |
result = [] |
| 162 |
errs = [] |
| 163 |
rcs = [] |
| 164 |
if not name: |
| 165 |
all_names = [executable, "pod", "ls", "-q"] |
| 166 |
rc, out, err = module.run_command(all_names) |
| 167 |
if rc != 0: |
| 168 |
module.fail_json(msg="Unable to get list of pods: %s" % err) |
| 169 |
name = out.split() |
| 170 |
if not name: |
| 171 |
return [], [err], [rc] |
| 172 |
pods = name |
| 173 |
for pod in pods: |
| 174 |
rc, out, err = module.run_command(command + [pod]) |
| 175 |
errs.append(err.strip()) |
| 176 |
rcs += [rc] |
| 177 |
data = json.loads(out) if out else None |
| 178 |
if isinstance(data, list) and data: |
| 179 |
data = data[0] |
| 180 |
if not out or data is None or not data: |
| 181 |
continue |
| 182 |
result.append(data) |
| 183 |
return result, errs, rcs |
| 184 |
|
| 185 |
|
| 186 |
def main(): |
| 187 |
module = AnsibleModule( |
| 188 |
argument_spec=dict(executable=dict(type="str", default="podman"), name=dict(type="str")), |
| 189 |
supports_check_mode=True, |
| 190 |
) |
| 191 |
|
| 192 |
name = module.params["name"] |
| 193 |
executable = module.get_bin_path(module.params["executable"], required=True) |
| 194 |
|
| 195 |
inspect_results, errs, rcs = get_pod_info(module, executable, name) |
| 196 |
|
| 197 |
if len(rcs) > 1 and 0 not in rcs: |
| 198 |
module.fail_json(msg="Failed to inspect pods", stderr="\n".join(errs)) |
| 199 |
|
| 200 |
results = { |
| 201 |
"changed": False, |
| 202 |
"pods": inspect_results, |
| 203 |
"stderr": "\n".join(errs), |
| 204 |
} |
| 205 |
|
| 206 |
module.exit_json(**results) |
| 207 |
|
| 208 |
|
| 209 |
if __name__ == "__main__": |
| 210 |
main() |