Debian Codegraph @

ansible_collections/containers/podman/plugins/modules/podman_login_info.py

raw
1 #!/usr/bin/python
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import absolute_import, division, print_function
5
6 __metaclass__ = type
7
8
9 DOCUMENTATION = r"""
10 module: podman_login_info
11 author:
12 - "Clemens Lange (@clelange)"
13 version_added: '1.0.0'
14 short_description: Return the logged-in user if any for a given registry
15 notes: []
16 description:
17 - Return the logged-in user if any for a given registry.
18 requirements:
19 - "Podman installed on host"
20 options:
21 registry:
22 description:
23 - Registry server.
24 type: str
25 required: true
26 authfile:
27 description:
28 - Path of the authentication file. Default is
29 ``${XDG_RUNTIME_DIR}/containers/auth.json``
30 (Not available for remote commands) You can also override the default
31 path of the authentication file by setting the ``REGISTRY_AUTH_FILE``
32 environment variable. ``export REGISTRY_AUTH_FILE=path``
33 type: path
34 executable:
35 description:
36 - Path to C(podman) executable if it is not in the C($PATH) on the
37 machine running C(podman)
38 default: 'podman'
39 type: str
40 """
41
42 EXAMPLES = r"""
43 - name: Return the logged-in user for docker hub registry
44 containers.podman.podman_login_info:
45 registry: docker.io
46
47 - name: Return the logged-in user for quay.io registry
48 containers.podman.podman_login_info:
49 registry: quay.io
50 """
51
52 RETURN = r"""
53 login:
54 description: Logged in user for a registry
55 returned: always
56 type: dict
57 sample: {
58 "logged_in": true,
59 "registry": "docker.io",
60 "username": "clelange",
61 }
62 """
63
64 from ansible.module_utils.basic import AnsibleModule
65
66
67 def get_login_info(module, executable, authfile, registry):
68 command = [executable, "login", "--get-login"]
69 result = dict(
70 registry=registry,
71 username="",
72 logged_in=False,
73 )
74 if authfile:
75 command.extend(["--authfile", authfile])
76 if registry:
77 command.append(registry)
78 rc, out, err = module.run_command(command)
79 if rc != 0:
80 if "Error: not logged into" in err:
81 # The error message is e.g. 'Error: not logged into docker.io'
82 # Therefore get last word to extract registry name
83 result["registry"] = err.split()[-1]
84 err = ""
85 return result
86 module.fail_json(msg="Unable to gather info for %s: %s" % (registry, err))
87 result["username"] = out.strip()
88 result["logged_in"] = True
89 return result
90
91
92 def main():
93 module = AnsibleModule(
94 argument_spec=dict(
95 executable=dict(type="str", default="podman"),
96 authfile=dict(type="path"),
97 registry=dict(type="str", required=True),
98 ),
99 supports_check_mode=True,
100 )
101
102 registry = module.params["registry"]
103 authfile = module.params["authfile"]
104 executable = module.get_bin_path(module.params["executable"], required=True)
105
106 inspect_results = get_login_info(module, executable, authfile, registry)
107
108 results = {
109 "changed": False,
110 "login": inspect_results,
111 }
112
113 module.exit_json(**results)
114
115
116 if __name__ == "__main__":
117 main()