Debian Codegraph @

ansible_collections/community/general/plugins/modules/cloud_init_data_facts.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2018, René Moser <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 from __future__ import annotations
7
8 DOCUMENTATION = r"""
9 module: cloud_init_data_facts
10 short_description: Retrieve facts of cloud-init
11 description:
12 - Gathers facts by reading the C(status.json) and C(result.json) of cloud-init.
13 author: René Moser (@resmo)
14 extends_documentation_fragment:
15 - community.general._attributes
16 - community.general._attributes.facts
17 - community.general._attributes.facts_module
18 options:
19 filter:
20 description:
21 - Filter facts.
22 type: str
23 choices: [status, result]
24 notes:
25 - See http://cloudinit.readthedocs.io/ for more information about cloud-init.
26 """
27
28 EXAMPLES = r"""
29 - name: Gather all facts of cloud init
30 community.general.cloud_init_data_facts:
31 register: result
32
33 - ansible.builtin.debug:
34 var: result
35
36 - name: Wait for cloud init to finish
37 community.general.cloud_init_data_facts:
38 filter: status
39 register: res
40 until: "res.cloud_init_data_facts.status.v1.stage is defined and not res.cloud_init_data_facts.status.v1.stage"
41 retries: 50
42 delay: 5
43 """
44
45 RETURN = r"""
46 ansible_facts:
47 description: The returned facts.
48 returned: success
49 type: dict
50 contains:
51 cloud_init_data_facts:
52 description: Facts of result and status.
53 returned: success
54 type: dict
55 sample:
56 {
57 "status": {
58 "v1": {
59 "datasource": "DataSourceCloudStack",
60 "errors": []
61 }
62 },
63 "result": {
64 "v1": {
65 "datasource": "DataSourceCloudStack",
66 "init": {
67 "errors": [],
68 "finished": 1522066377.0185432,
69 "start": 1522066375.2648022
70 },
71 "init-local": {
72 "errors": [],
73 "finished": 1522066373.70919,
74 "start": 1522066373.4726632
75 },
76 "modules-config": {
77 "errors": [],
78 "finished": 1522066380.9097016,
79 "start": 1522066379.0011985
80 },
81 "modules-final": {
82 "errors": [],
83 "finished": 1522066383.56594,
84 "start": 1522066382.3449218
85 },
86 "stage": null
87 }
88 }
89 }
90 """
91
92 import os
93
94 from ansible.module_utils.basic import AnsibleModule
95 from ansible.module_utils.common.text.converters import to_text
96
97 CLOUD_INIT_PATH = "/var/lib/cloud/data"
98
99
100 def gather_cloud_init_data_facts(module):
101 res = {"cloud_init_data_facts": dict()}
102
103 for i in ["result", "status"]:
104 filter = module.params.get("filter")
105 if filter is None or filter == i:
106 res["cloud_init_data_facts"][i] = dict()
107 json_file = os.path.join(CLOUD_INIT_PATH, f"{i}.json")
108
109 if os.path.exists(json_file):
110 with open(json_file, "rb") as f:
111 contents = to_text(f.read(), errors="surrogate_or_strict")
112
113 if contents:
114 res["cloud_init_data_facts"][i] = module.from_json(contents)
115 return res
116
117
118 def main():
119 module = AnsibleModule(
120 argument_spec=dict(
121 filter=dict(choices=["result", "status"]),
122 ),
123 supports_check_mode=True,
124 )
125
126 facts = gather_cloud_init_data_facts(module)
127 result = dict(changed=False, ansible_facts=facts, **facts)
128 module.exit_json(**result)
129
130
131 if __name__ == "__main__":
132 main()