Debian Codegraph @

ansible_collections/community/docker/plugins/modules/current_container_facts.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2020 Matt Clay <[email protected]>
4 # Copyright (c) 2020 Felix Fontein <[email protected]>
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 module: current_container_facts
12 short_description: Return facts about whether the module runs in a container
13 version_added: 1.1.0
14 description:
15 - Return facts about whether the module runs in a Docker or podman container.
16 - This module attempts a best-effort detection. There might be special cases where it does not work; if you encounter one,
17 make sure that this is still a problem with the latest community.docker release, and if it is,
18 L(please file an issue, https://github.com/ansible-collections/community.docker/issues/new?assignees=&labels=&template=bug_report.md).
19 author:
20 - Felix Fontein (@felixfontein)
21 extends_documentation_fragment:
22 - community.docker._attributes
23 - community.docker._attributes.facts
24 - community.docker._attributes.facts_module
25 - community.docker._attributes.idempotent_not_modify_state
26 """
27
28 EXAMPLES = r"""
29 ---
30 - name: Get facts on current container
31 community.docker.current_container_facts:
32
33 - name: Print information on current container when running in a container
34 ansible.builtin.debug:
35 msg: "Container ID is {{ ansible_module_container_id }}"
36 when: ansible_module_running_in_container
37 """
38
39 RETURN = r"""
40 ansible_facts:
41 description: Ansible facts returned by the module.
42 type: dict
43 returned: always
44 contains:
45 ansible_module_running_in_container:
46 description:
47 - Whether the module was able to detect that it runs in a container or not.
48 returned: always
49 type: bool
50 ansible_module_container_id:
51 description:
52 - The detected container ID.
53 - Contains an empty string if no container was detected.
54 returned: always
55 type: str
56 ansible_module_container_type:
57 description:
58 - The detected container environment.
59 - Contains an empty string if no container was detected, or a non-empty string identifying the container environment.
60 - V(docker) indicates that the module ran inside a regular Docker container.
61 - V(azure_pipelines) indicates that the module ran on Azure Pipelines. This seems to no longer be reported.
62 - V(github_actions) indicates that the module ran inside a Docker container on GitHub Actions. It is supported since
63 community.docker 2.4.0.
64 - V(podman) indicates that the module ran inside a regular Podman container. It is supported since community.docker
65 3.3.0.
66 returned: always
67 type: str
68 choices:
69 - ''
70 - docker
71 - azure_pipelines
72 - github_actions
73 - podman
74 """
75
76 import os
77 import re
78
79 from ansible.module_utils.basic import AnsibleModule
80
81
82 def main() -> None:
83 module = AnsibleModule({}, supports_check_mode=True)
84
85 cpuset_path = "/proc/self/cpuset"
86 mountinfo_path = "/proc/self/mountinfo"
87
88 container_id = ""
89 container_type = ""
90
91 contents = None
92 if os.path.exists(cpuset_path):
93 # File content varies based on the environment:
94 # No Container: /
95 # Docker: /docker/c86f3732b5ba3d28bb83b6e14af767ab96abbc52de31313dcb1176a62d91a507
96 # Azure Pipelines (Docker): /azpl_job/0f2edfed602dd6ec9f2e42c867f4d5ee640ebf4c058e6d3196d4393bb8fd0891
97 # Podman: /../../../../../..
98 # While this was true and worked well for a long time, this seems to be no longer accurate
99 # with newer Docker / Podman versions and/or with cgroupv2. That's why the /proc/self/mountinfo
100 # detection further down is done when this test is inconclusive.
101 with open(cpuset_path, "rb") as f:
102 contents = f.read().decode("utf-8")
103
104 cgroup_path, cgroup_name = os.path.split(contents.strip())
105
106 if cgroup_path == "/docker":
107 container_id = cgroup_name
108 container_type = "docker"
109
110 if cgroup_path == "/azpl_job":
111 container_id = cgroup_name
112 container_type = "azure_pipelines"
113
114 if cgroup_path == "/actions_job":
115 container_id = cgroup_name
116 container_type = "github_actions"
117
118 if not container_id and os.path.exists(mountinfo_path):
119 with open(mountinfo_path, "rb") as f:
120 contents = f.read().decode("utf-8")
121
122 # As to why this works, see the explanations by Matt Clay in
123 # https://github.com/ansible/ansible/blob/80d2f8da02052f64396da6b8caaf820eedbf18e2/test/lib/ansible_test/_internal/docker_util.py#L571-L610
124
125 for line in contents.splitlines():
126 parts = line.split()
127 if len(parts) >= 5 and parts[4] == "/etc/hostname":
128 m = re.match(".*/([a-f0-9]{64})/hostname$", parts[3])
129 if m:
130 container_id = m.group(1)
131 container_type = "docker"
132
133 m = re.match(".*/([a-f0-9]{64})/userdata/hostname$", parts[3])
134 if m:
135 container_id = m.group(1)
136 container_type = "podman"
137
138 module.exit_json(
139 ansible_facts={
140 "ansible_module_running_in_container": container_id != "",
141 "ansible_module_container_id": container_id,
142 "ansible_module_container_type": container_type,
143 }
144 )
145
146
147 if __name__ == "__main__":
148 main()