| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) Ansible project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: github_secrets_info |
| 11 |
short_description: List GitHub repository or organization secrets |
| 12 |
description: |
| 13 |
- List secrets in a GitHub repository or organization. |
| 14 |
author: |
| 15 |
- Thomas Sjögren (@konstruktoid) |
| 16 |
version_added: '12.5.0' |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
- community.general._attributes.info_module |
| 20 |
options: |
| 21 |
organization: |
| 22 |
description: |
| 23 |
- The GitHub username or organization name. |
| 24 |
type: str |
| 25 |
required: true |
| 26 |
aliases: ["org", "username"] |
| 27 |
repository: |
| 28 |
description: |
| 29 |
- The name of the repository. |
| 30 |
- If not provided, the listing will be at organization level. |
| 31 |
type: str |
| 32 |
aliases: ["repo"] |
| 33 |
api_url: |
| 34 |
description: |
| 35 |
- The base URL for the GitHub API. |
| 36 |
type: str |
| 37 |
default: "https://api.github.com" |
| 38 |
token: |
| 39 |
description: |
| 40 |
- The GitHub token used for authentication. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
""" |
| 44 |
|
| 45 |
EXAMPLES = r""" |
| 46 |
- name: List Github secret |
| 47 |
community.general.github_secrets_info: |
| 48 |
token: "{{ lookup('ansible.builtin.env', 'GITHUB_TOKEN') }}" |
| 49 |
repository: "ansible" |
| 50 |
organization: "ansible" |
| 51 |
""" |
| 52 |
|
| 53 |
RETURN = r""" |
| 54 |
secrets: |
| 55 |
description: The list of currently existing secrets. |
| 56 |
type: list |
| 57 |
elements: dict |
| 58 |
returned: success |
| 59 |
sample: [ |
| 60 |
{ |
| 61 |
"created_at": "2026-01-11T23:19:00Z", |
| 62 |
"name": "ANSIBLE", |
| 63 |
"updated_at": "2026-02-15T22:18:16Z" |
| 64 |
}, |
| 65 |
] |
| 66 |
contains: |
| 67 |
name: |
| 68 |
description: The name of the secret. |
| 69 |
type: str |
| 70 |
created_at: |
| 71 |
description: The date and time when the secret was created. |
| 72 |
type: str |
| 73 |
updated_at: |
| 74 |
description: The date and time when the secret was last updated. |
| 75 |
type: str |
| 76 |
""" |
| 77 |
|
| 78 |
import json |
| 79 |
import typing as t |
| 80 |
from http import HTTPStatus |
| 81 |
|
| 82 |
from ansible.module_utils.basic import AnsibleModule |
| 83 |
from ansible.module_utils.urls import fetch_url |
| 84 |
|
| 85 |
from ansible_collections.community.general.plugins.module_utils import _deps as deps |
| 86 |
|
| 87 |
|
| 88 |
def list_secrets( |
| 89 |
module: AnsibleModule, |
| 90 |
api_url: str, |
| 91 |
headers: dict[str, str], |
| 92 |
organization: str, |
| 93 |
repository: str, |
| 94 |
) -> dict[str, list]: |
| 95 |
url = ( |
| 96 |
f"{api_url}/repos/{organization}/{repository}/actions/secrets" |
| 97 |
if repository |
| 98 |
else f"{api_url}/orgs/{organization}/actions/secrets" |
| 99 |
) |
| 100 |
|
| 101 |
resp, info = fetch_url(module, url, headers=headers, method="GET") |
| 102 |
|
| 103 |
if info["status"] == HTTPStatus.OK: |
| 104 |
body = resp.read() |
| 105 |
return {"secrets": json.loads(body).get("secrets", [])} |
| 106 |
elif info["status"] == HTTPStatus.NOT_FOUND: |
| 107 |
return { |
| 108 |
"secrets": [], |
| 109 |
} |
| 110 |
else: |
| 111 |
module.fail_json(msg=f"Failed to list secrets: {info}") |
| 112 |
|
| 113 |
|
| 114 |
def main() -> None: |
| 115 |
"""Ansible module entry point.""" |
| 116 |
argument_spec = { |
| 117 |
"organization": { |
| 118 |
"type": "str", |
| 119 |
"aliases": ["org", "username"], |
| 120 |
"required": True, |
| 121 |
}, |
| 122 |
"repository": {"type": "str", "aliases": ["repo"]}, |
| 123 |
"api_url": {"type": "str", "default": "https://api.github.com"}, |
| 124 |
"token": {"type": "str", "required": True, "no_log": True}, |
| 125 |
} |
| 126 |
|
| 127 |
module = AnsibleModule( |
| 128 |
argument_spec=argument_spec, |
| 129 |
supports_check_mode=True, |
| 130 |
) |
| 131 |
|
| 132 |
deps.validate(module) |
| 133 |
|
| 134 |
organization: str = module.params["organization"] |
| 135 |
repository: str = module.params["repository"] |
| 136 |
api_url: str = module.params["api_url"] |
| 137 |
token: str = module.params["token"] |
| 138 |
|
| 139 |
result: dict[str, t.Any] = {} |
| 140 |
|
| 141 |
headers = { |
| 142 |
"Accept": "application/vnd.github+json", |
| 143 |
"Authorization": f"Bearer {token}", |
| 144 |
"Content-Type": "application/json", |
| 145 |
} |
| 146 |
|
| 147 |
secrets = list_secrets(module, api_url, headers, organization, repository) |
| 148 |
|
| 149 |
result["changed"] = False |
| 150 |
result.update( |
| 151 |
secrets=secrets["secrets"], |
| 152 |
) |
| 153 |
|
| 154 |
module.exit_json(**result) |
| 155 |
|
| 156 |
|
| 157 |
if __name__ == "__main__": |
| 158 |
main() |