Skip to content

Commit

Permalink
zabbix_user: Fix unable create a user since Zabbix 5.2 (#260)
Browse files Browse the repository at this point in the history
* fix unable create a user since Zabbix 5.2

* add changelog file

* update changelog file

* fix to support role_name parameter, and the lang can choose the default value.

* update changelog file

* add the default value of role_name parameter

* fix some pointed out
  • Loading branch information
sky-joker authored Nov 14, 2020
1 parent 472f289 commit c8c225f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/260-zabbix_user.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bugfixes:
- zabbix_user - fixed issue where module couldn't create a user since Zabbix 5.2 (https://github.com/ansible-collections/community.zabbix/pull/260).
minor_changes:
- zabbix_user - added new parameters to set timezone and role_name for users (https://github.com/ansible-collections/community.zabbix/pull/260).
70 changes: 59 additions & 11 deletions plugins/modules/zabbix_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
lang:
description:
- Language code of the user's language.
- C(default) can be used with Zabbix version 5.2 or higher.
default: 'en_GB'
choices:
- 'en_GB'
Expand All @@ -77,6 +78,7 @@
- 'sk_SK'
- 'tr_TR'
- 'uk_UA'
- 'default'
type: str
theme:
description:
Expand Down Expand Up @@ -188,12 +190,28 @@
type:
description:
- Type of the user.
- I(type) is necessary if Zabbix version is 5.0 or lower.
default: 'Zabbix user'
choices:
- 'Zabbix user'
- 'Zabbix admin'
- 'Zabbix super admin'
type: str
timezone:
description:
- User's time zone.
- I(timezone) can be used with Zabbix version 5.2 or higher.
- For the full list of supported time zones please refer to U(https://www.php.net/manual/en/timezones.php)
default: default
type: str
version_added: 1.2.0
role_name:
description:
- User's role.
- I(role_name) is required when Zabbix version is 5.2 or higher.
default: 'User role'
type: str
version_added: 1.2.0
state:
description:
- State of the user.
Expand Down Expand Up @@ -335,9 +353,17 @@ def convert_user_medias_parameter_types(self, user_medias):

return copy_user_medias

def get_roleid_by_name(self, role_name):
roles = self._zapi.role.get({'output': 'extend'})
for role in roles:
if role['name'] == role_name:
return role['roleid']

self._module.fail_json(msg="Role not found: %s" % role_name)

def user_parameter_difference_check(self, zbx_user, alias, name, surname, user_group_ids, passwd, lang, theme,
autologin, autologout, refresh, rows_per_page, url, user_medias, user_type,
override_passwd):
timezone, role_name, override_passwd):

user_medias = self.convert_user_medias_parameter_types(user_medias)

Expand Down Expand Up @@ -382,12 +408,18 @@ def user_parameter_difference_check(self, zbx_user, alias, name, surname, user_g
'rows_per_page': rows_per_page,
'url': url,
'user_medias': sorted(user_medias, key=lambda x: x['sendto']),
'type': user_type
}

if override_passwd:
request_data['passwd'] = passwd

# The type key has changed to roleid key since Zabbix 5.2
if LooseVersion(self._zbx_api_version) < LooseVersion('5.2'):
request_data['type'] = user_type
else:
request_data['roleid'] = self.get_roleid_by_name(role_name)
request_data['timezone'] = timezone

user_parameter_difference_check_result = True
if existing_data == request_data:
user_parameter_difference_check_result = False
Expand All @@ -400,7 +432,7 @@ def user_parameter_difference_check(self, zbx_user, alias, name, surname, user_g
return user_parameter_difference_check_result, diff_params

def add_user(self, alias, name, surname, user_group_ids, passwd, lang, theme, autologin, autologout, refresh,
rows_per_page, url, user_medias, user_type, not_ldap):
rows_per_page, url, user_medias, user_type, not_ldap, timezone, role_name):

user_medias = self.convert_user_medias_parameter_types(user_medias)

Expand All @@ -419,12 +451,18 @@ def add_user(self, alias, name, surname, user_group_ids, passwd, lang, theme, au
'rows_per_page': rows_per_page,
'url': url,
'user_medias': user_medias,
'type': user_type
}

if LooseVersion(self._zbx_api_version) < LooseVersion('4.0') or not_ldap:
request_data['passwd'] = passwd

# The type key has changed to roleid key since Zabbix 5.2
if LooseVersion(self._zbx_api_version) < LooseVersion('5.2'):
request_data['type'] = user_type
else:
request_data['roleid'] = self.get_roleid_by_name(role_name)
request_data['timezone'] = timezone

diff_params = {}
if not self._module.check_mode:
try:
Expand All @@ -440,7 +478,7 @@ def add_user(self, alias, name, surname, user_group_ids, passwd, lang, theme, au
return user_ids, diff_params

def update_user(self, zbx_user, alias, name, surname, user_group_ids, passwd, lang, theme, autologin, autologout,
refresh, rows_per_page, url, user_medias, user_type, override_passwd):
refresh, rows_per_page, url, user_medias, user_type, timezone, role_name, override_passwd):

user_medias = self.convert_user_medias_parameter_types(user_medias)

Expand All @@ -459,12 +497,18 @@ def update_user(self, zbx_user, alias, name, surname, user_group_ids, passwd, la
'refresh': refresh,
'rows_per_page': rows_per_page,
'url': url,
'type': user_type
}

if override_passwd:
request_data['passwd'] = passwd

# The type key has changed to roleid key since Zabbix 5.2
if LooseVersion(self._zbx_api_version) < LooseVersion('5.2'):
request_data['type'] = user_type
else:
request_data['roleid'] = self.get_roleid_by_name(role_name)
request_data['timezone'] = timezone

# In the case of zabbix 3.2 or less, it is necessary to use updatemedia method to update media.
if LooseVersion(self._zbx_api_version) <= LooseVersion('3.2'):
try:
Expand Down Expand Up @@ -519,7 +563,7 @@ def main():
lang=dict(type='str', default='en_GB', choices=['en_GB', 'en_US', 'zh_CN', 'cs_CZ', 'fr_FR',
'he_IL', 'it_IT', 'ko_KR', 'ja_JP', 'nb_NO',
'pl_PL', 'pt_BR', 'pt_PT', 'ru_RU', 'sk_SK',
'tr_TR', 'uk_UA']),
'tr_TR', 'uk_UA', 'default']),
theme=dict(type='str', default='default', choices=['default', 'blue-theme', 'dark-theme']),
autologin=dict(type='bool', default=False),
autologout=dict(type='str', default='0'),
Expand All @@ -546,6 +590,8 @@ def main():
high=True,
disaster=True)),
active=dict(type='bool', default=True))),
timezone=dict(type='str', default='default'),
role_name=dict(type='str', default='User role'),
type=dict(type='str', default='Zabbix user', choices=['Zabbix user', 'Zabbix admin', 'Zabbix super admin']),
state=dict(type='str', default="present", choices=['present', 'absent'])
))
Expand All @@ -572,6 +618,8 @@ def main():
after_login_url = module.params['after_login_url']
user_medias = module.params['user_medias']
user_type = module.params['type']
timezone = module.params['timezone']
role_name = module.params['role_name']
state = module.params['state']

if autologin:
Expand Down Expand Up @@ -601,18 +649,18 @@ def main():
user_group_ids, passwd, lang, theme,
autologin, autologout, refresh,
rows_per_page, after_login_url,
user_medias, user_type,
override_passwd)
user_medias, user_type, timezone,
role_name, override_passwd)

if not module.check_mode and diff_check_result:
user_ids = user.update_user(zbx_user, alias, name, surname, user_group_ids, passwd, lang,
theme, autologin, autologout, refresh, rows_per_page, after_login_url,
user_medias, user_type, override_passwd)
user_medias, user_type, timezone, role_name, override_passwd)
else:
diff_check_result = True
user_ids, diff_params = user.add_user(alias, name, surname, user_group_ids, passwd, lang, theme, autologin,
autologout, refresh, rows_per_page, after_login_url, user_medias,
user_type, not_ldap)
user_type, not_ldap, timezone, role_name)

if state == 'absent':
if zbx_user:
Expand Down

0 comments on commit c8c225f

Please sign in to comment.