Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2017, Joris Weijters <[email protected]>
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 author:
11 - Joris Weijters (@molekuul)
12 module: aix_inittab
13 short_description: Manages the C(inittab) on AIX
14 description:
15 - Manages the C(inittab) on AIX.
16 deprecated:
17 removed_in: 15.0.0
18 why: The module is not actively maintained.
19 alternative: >-
20 Use C(ibm.power_aix.inittab) instead.
21 See U(https://ibm.github.io/ansible-power-aix/modules/inittab.html) for details.
22 extends_documentation_fragment:
23 - community.general._attributes
24 attributes:
25 check_mode:
26 support: full
27 diff_mode:
28 support: none
29 options:
30 name:
31 description:
32 - Name of the C(inittab) entry.
33 type: str
34 required: true
35 aliases: [service]
36 runlevel:
37 description:
38 - Runlevel of the entry.
39 type: str
40 required: true
41 action:
42 description:
43 - Action the C(init) process performs for this entry.
44 type: str
45 choices:
46 - boot
47 - bootwait
48 - hold
49 - initdefault
50 - 'off'
51 - once
52 - ondemand
53 - powerfail
54 - powerwait
55 - respawn
56 - sysinit
57 - wait
58 command:
59 description:
60 - Command to be executed.
61 type: str
62 required: true
63 insertafter:
64 description:
65 - After which C(inittab) line should the new entry inserted.
66 type: str
67 state:
68 description:
69 - Whether the entry should be present or absent in the C(inittab) file.
70 type: str
71 choices: [absent, present]
72 default: present
73 notes:
74 - The changes are persistent across reboots.
75 - You need root rights to read or adjust the C(inittab) with the C(lsitab), C(chitab), C(mkitab) or C(rmitab) commands.
76 - Tested on AIX 7.1.
77 """
78
79 EXAMPLES = r"""
80 # Add service startmyservice to the inittab, directly after service existingservice.
81 - name: Add startmyservice to inittab
82 community.general.aix_inittab:
83 name: startmyservice
84 runlevel: 4
85 action: once
86 command: echo hello
87 insertafter: existingservice
88 state: present
89 become: true
90
91 # Change inittab entry startmyservice to runlevel "2" and processaction "wait".
92 - name: Change startmyservice to inittab
93 community.general.aix_inittab:
94 name: startmyservice
95 runlevel: 2
96 action: wait
97 command: echo hello
98 state: present
99 become: true
100
101 - name: Remove startmyservice from inittab
102 community.general.aix_inittab:
103 name: startmyservice
104 runlevel: 2
105 action: wait
106 command: echo hello
107 state: absent
108 become: true
109 """
110
111 RETURN = r"""
112 name:
113 description: Name of the adjusted C(inittab) entry.
114 returned: always
115 type: str
116 sample: startmyservice
117 """
118
119
120 from ansible.module_utils.basic import AnsibleModule
121
122
123 def check_current_entry(module):
124 # Check if entry exists, if not return False in exists in return dict,
125 # if true return True and the entry in return dict
126 existsdict = {"exist": False}
127 lsitab = module.get_bin_path("lsitab")
128 (rc, out, err) = module.run_command([lsitab, module.params["name"]])
129 if rc == 0:
130 keys = ("name", "runlevel", "action", "command")
131 values = out.split(":")
132 # strip non readable characters as \n
133 values = map(lambda s: s.strip(), values)
134 existsdict = dict(zip(keys, values))
135 existsdict.update({"exist": True})
136 return existsdict
137
138
139 def main():
140 # initialize
141 module = AnsibleModule(
142 argument_spec=dict(
143 name=dict(type="str", required=True, aliases=["service"]),
144 runlevel=dict(type="str", required=True),
145 action=dict(
146 type="str",
147 choices=[
148 "boot",
149 "bootwait",
150 "hold",
151 "initdefault",
152 "off",
153 "once",
154 "ondemand",
155 "powerfail",
156 "powerwait",
157 "respawn",
158 "sysinit",
159 "wait",
160 ],
161 ),
162 command=dict(type="str", required=True),
163 insertafter=dict(type="str"),
164 state=dict(type="str", default="present", choices=["absent", "present"]),
165 ),
166 supports_check_mode=True,
167 )
168
169 result = {"name": module.params["name"], "changed": False, "msg": ""}
170
171 # Find commandline strings
172 mkitab = module.get_bin_path("mkitab")
173 rmitab = module.get_bin_path("rmitab")
174 chitab = module.get_bin_path("chitab")
175 rc = 0
176 err = None
177
178 # check if the new entry exists
179 current_entry = check_current_entry(module)
180
181 # if action is install or change,
182 if module.params["state"] == "present":
183 # create new entry string
184 new_entry = (
185 f"{module.params['name']}:{module.params['runlevel']}:{module.params['action']}:{module.params['command']}"
186 )
187
188 # If current entry exists or fields are different(if the entry does not
189 # exists, then the entry will be created
190 if (not current_entry["exist"]) or (
191 module.params["runlevel"] != current_entry["runlevel"]
192 or module.params["action"] != current_entry["action"]
193 or module.params["command"] != current_entry["command"]
194 ):
195 # If the entry does exist then change the entry
196 if current_entry["exist"]:
197 if not module.check_mode:
198 (rc, out, err) = module.run_command([chitab, new_entry])
199 if rc != 0:
200 module.fail_json(msg="could not change inittab", rc=rc, err=err)
201 result["msg"] = f"changed inittab entry {current_entry['name']}"
202 result["changed"] = True
203
204 # If the entry does not exist create the entry
205 elif not current_entry["exist"]:
206 if module.params["insertafter"]:
207 if not module.check_mode:
208 (rc, out, err) = module.run_command([mkitab, "-i", module.params["insertafter"], new_entry])
209 else:
210 if not module.check_mode:
211 (rc, out, err) = module.run_command([mkitab, new_entry])
212
213 if rc != 0:
214 module.fail_json(msg="could not adjust inittab", rc=rc, err=err)
215 result["msg"] = f"add inittab entry {module.params['name']}"
216 result["changed"] = True
217
218 elif module.params["state"] == "absent":
219 # If the action is remove and the entry exists then remove the entry
220 if current_entry["exist"]:
221 if not module.check_mode:
222 (rc, out, err) = module.run_command([rmitab, module.params["name"]])
223 if rc != 0:
224 module.fail_json(msg="could not remove entry from inittab)", rc=rc, err=err)
225 result["msg"] = f"removed inittab entry {current_entry['name']}"
226 result["changed"] = True
227
228 module.exit_json(**result)
229
230
231 if __name__ == "__main__":
232 main()