Skip to content

Commit

Permalink
Merge pull request freeipa#310 from t-woerner/fail_on_duplicate_names
Browse files Browse the repository at this point in the history
ipa[user,host]: Fail on duplucate names in the users and hosts lists
  • Loading branch information
t-woerner authored Jun 29, 2020
2 parents 0303f15 + 1d7fb31 commit 464eae1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plugins/modules/ipahost.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,10 +799,15 @@ def main():
server_realm = api_get_realm()

commands = []
host_set = set()

for host in names:
if isinstance(host, dict):
name = host.get("name")
if name in host_set:
ansible_module.fail_json(
msg="host '%s' is used more than once" % name)
host_set.add(name)
description = host.get("description")
locality = host.get("locality")
location = host.get("location")
Expand Down Expand Up @@ -1337,6 +1342,8 @@ def main():
else:
ansible_module.fail_json(msg="Unkown state '%s'" % state)

del host_set

# Execute commands

errors = []
Expand Down
7 changes: 7 additions & 0 deletions plugins/modules/ipauser.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,10 +958,15 @@ def main():
# commands

commands = []
user_set = set()

for user in names:
if isinstance(user, dict):
name = user.get("name")
if name in user_set:
ansible_module.fail_json(
msg="user '%s' is used more than once" % name)
user_set.add(name)
# present
first = user.get("first")
last = user.get("last")
Expand Down Expand Up @@ -1370,6 +1375,8 @@ def main():
else:
ansible_module.fail_json(msg="Unkown state '%s'" % state)

del user_set

# Execute commands

errors = []
Expand Down
15 changes: 15 additions & 0 deletions tests/host/test_hosts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,18 @@
state: absent
register: result
failed_when: result.changed

- name: Duplicate names in hosts failure test
ipahost:
ipaadmin_password: SomeADMINpassword
hosts:
- name: "{{ host1_fqdn }}"
force: yes
- name: "{{ host2_fqdn }}"
force: yes
- name: "{{ host3_fqdn }}"
force: yes
- name: "{{ host3_fqdn }}"
force: yes
register: result
failed_when: result.changed or "is used more than once" not in result.msg
19 changes: 19 additions & 0 deletions tests/user/test_users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@
register: result
failed_when: result.changed

- name: Duplicate names in users failure test
ipauser:
ipaadmin_password: SomeADMINpassword
users:
- name: user1
givenname: user1
last: Last
- name: user2
first: user2
last: Last
- name: user3
first: user3
last: Last
- name: user3
first: user3
last: Last
register: result
failed_when: result.changed or "is used more than once" not in result.msg

- name: Remove test users
ipauser:
ipaadmin_password: SomeADMINpassword
Expand Down

0 comments on commit 464eae1

Please sign in to comment.