Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vcenter_root_password_expiration.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Ansible Project
5 # Copyright: (c) 2023, Valentin Yonev <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 DOCUMENTATION = r'''
10 ---
11 module: vcenter_root_password_expiration
12 version_added: '3.10.0'
13 short_description: root password expiration of vCSA
14 description: Manages password expiration configuration for root user of vCSA appliance
15 author:
16 - Valentin Yonev (@valentinJonev)
17 options:
18 state:
19 description:
20 - present - represents that password expiration must be configured
21 - absent - represents no expiration for root user
22 choices: [ present, absent ]
23 default: 'present'
24 type: str
25 email:
26 description:
27 - e-mail to send password expiration warnings to
28 type: str
29 required: false
30 max_days_between_password_change:
31 description:
32 - Maximum days between password change
33 type: int
34 required: false
35 min_days_between_password_change:
36 description:
37 - Minimum days between password change
38 type: int
39 required: false
40 warn_days_before_password_expiration:
41 description:
42 - Days before password expires and password expiration e-mail should be sent
43 type: int
44 required: false
45 extends_documentation_fragment:
46 - vmware.vmware.base_options
47 '''
48
49 EXAMPLES = r'''
50 - name: Configures expiring root password
51 vcenter_root_password_expiration:
52 hostname: '{{ vcenter_hostname }}'
53 username: '{{ vcenter_localos_username }}'
54 password: '{{ vcenter_password }}'
55 max_days_between_password_change: 60
56 min_days_between_password_change: 6
57 warn_days_before_password_expiration: 7
58 email: [email protected]
59 state: present
60 delegate_to: localhost
61
62 - name: Configures non-expiring root password
63 vcenter_root_password_expiration:
64 hostname: '{{ vcenter_hostname }}'
65 username: '{{ vcenter_localos_username }}'
66 password: '{{ vcenter_localos_password }}'
67 state: absent
68 delegate_to: localhost
69 '''
70
71 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
72
73 from ansible.module_utils.basic import AnsibleModule
74 from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient
75
76
77 class VcRootPasswordExpiration(VmwareRestClient):
78 def __init__(self, module: AnsibleModule):
79 super(VcRootPasswordExpiration, self).__init__(module)
80 self.module = module
81 self._state = True if self.module.params['state'] == 'present' else False
82
83 def configure_root_account_password_policy(self):
84 default_config = self.api_client.appliance.LocalAccounts.UpdateConfig()
85
86 current_vcenter_info = self.api_client.appliance.LocalAccounts.get('root').to_dict()
87
88 if self._state and self.module.params['min_days_between_password_change'] > self.module.params['max_days_between_password_change']:
89 self.module.fail_json("min_days_between_password_change cannot be higher than max_days_between_password_change")
90
91 if self._state:
92 _password_expiration_config = {
93 "email": self.module.params['email'],
94 "min_days_between_password_change": self.module.params['min_days_between_password_change'],
95 "max_days_between_password_change": self.module.params['max_days_between_password_change'],
96 "warn_days_before_password_expiration": self.module.params['warn_days_before_password_expiration'],
97
98 }
99 else:
100 _password_expiration_config = {
101 "max_days_between_password_change": -1
102 }
103
104 _changes_dict = dict()
105 for k, v in _password_expiration_config.items():
106 try:
107 if current_vcenter_info[k] != v:
108 _changes_dict[k] = v
109 if k == 'fullname':
110 setattr(default_config, 'full_name', v)
111 continue
112 except KeyError:
113 '''
114 Handles the case of newly installed vCenter when email field isn't present in the current config,
115 because it was never set befores
116 '''
117 _changes_dict[k] = v
118 setattr(default_config, k, v)
119 _change_result_key = 'values_would_be_changed'
120 if _changes_dict:
121 if not self.module.check_mode:
122 _change_result_key = 'values_changed'
123 self.api_client.appliance.LocalAccounts.update('root', default_config)
124 self.module.exit_json(changed=True, result={_change_result_key: _changes_dict})
125 self.module.exit_json(changed=False, result="No configuration changes needed")
126
127
128 def main():
129 argument_spec = base_argument_spec()
130 argument_spec.update(
131 dict(
132 state=dict(default='present',
133 choices=['present', 'absent'],
134 type='str')
135 ),
136 email=dict(required=False, type='str'),
137 max_days_between_password_change=dict(type='int', no_log=False),
138 min_days_between_password_change=dict(type='int', no_log=False),
139 warn_days_before_password_expiration=dict(type='int', no_log=False),
140 )
141
142 module = AnsibleModule(argument_spec=argument_spec,
143 required_if=[
144 (
145 'state',
146 'present',
147 ('email', 'max_days_between_password_change', 'min_days_between_password_change', 'warn_days_before_password_expiration')
148 ),
149 ],
150 supports_check_mode=True)
151
152 vc_root_password_policy_manager = VcRootPasswordExpiration(module)
153 vc_root_password_policy_manager.configure_root_account_password_policy()
154
155
156 if __name__ == '__main__':
157 main()