group - fix explicit git scenario (#52769)

* group - fix explicit git scenario

* Use str for simplicity and remove changelog

* dynamically get gid and remove Jinja2 check
This commit is contained in:
Jordan Borean 2019-02-22 12:40:42 +10:00 committed by GitHub
parent 63c22f6eb0
commit aab1f30906
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 220 additions and 99 deletions

View file

@ -120,7 +120,7 @@ class Group(object):
for key in kwargs:
if key == 'gid' and kwargs[key] is not None:
cmd.append('-g')
cmd.append(kwargs[key])
cmd.append(str(kwargs[key]))
elif key == 'system' and kwargs[key] is True:
cmd.append('-r')
cmd.append(self.name)
@ -137,7 +137,7 @@ class Group(object):
if key == 'gid':
if kwargs[key] is not None and info[2] != int(kwargs[key]):
cmd.append('-g')
cmd.append(kwargs[key])
cmd.append(str(kwargs[key]))
if len(cmd) == 1:
return (None, '', '')
if self.module.check_mode:
@ -182,7 +182,7 @@ class SunOS(Group):
for key in kwargs:
if key == 'gid' and kwargs[key] is not None:
cmd.append('-g')
cmd.append(kwargs[key])
cmd.append(str(kwargs[key]))
cmd.append(self.name)
return self.execute_command(cmd)
@ -211,7 +211,7 @@ class AIX(Group):
cmd = [self.module.get_bin_path('mkgroup', True)]
for key in kwargs:
if key == 'gid' and kwargs[key] is not None:
cmd.append('id=' + kwargs[key])
cmd.append('id=' + str(kwargs[key]))
elif key == 'system' and kwargs[key] is True:
cmd.append('-a')
cmd.append(self.name)
@ -223,7 +223,7 @@ class AIX(Group):
for key in kwargs:
if key == 'gid':
if kwargs[key] is not None and info[2] != int(kwargs[key]):
cmd.append('id=' + kwargs[key])
cmd.append('id=' + str(kwargs[key]))
if len(cmd) == 1:
return (None, '', '')
if self.module.check_mode:
@ -256,7 +256,7 @@ class FreeBsdGroup(Group):
cmd = [self.module.get_bin_path('pw', True), 'groupadd', self.name]
if self.gid is not None:
cmd.append('-g')
cmd.append('%d' % int(self.gid))
cmd.append(str(self.gid))
return self.execute_command(cmd)
def group_mod(self, **kwargs):
@ -265,7 +265,7 @@ class FreeBsdGroup(Group):
cmd_len = len(cmd)
if self.gid is not None and int(self.gid) != info[2]:
cmd.append('-g')
cmd.append('%d' % int(self.gid))
cmd.append(str(self.gid))
# modify the group if cmd will do anything
if cmd_len != len(cmd):
if self.module.check_mode:
@ -304,12 +304,12 @@ class DarwinGroup(Group):
cmd = [self.module.get_bin_path('dseditgroup', True)]
cmd += ['-o', 'create']
if self.gid is not None:
cmd += ['-i', self.gid]
cmd += ['-i', str(self.gid)]
elif 'system' in kwargs and kwargs['system'] is True:
gid = self.get_lowest_available_system_gid()
if gid is not False:
self.gid = str(gid)
cmd += ['-i', self.gid]
cmd += ['-i', str(self.gid)]
cmd += ['-L', self.name]
(rc, out, err) = self.execute_command(cmd)
return (rc, out, err)
@ -327,7 +327,7 @@ class DarwinGroup(Group):
cmd = [self.module.get_bin_path('dseditgroup', True)]
cmd += ['-o', 'edit']
if gid is not None:
cmd += ['-i', gid]
cmd += ['-i', str(gid)]
cmd += ['-L', self.name]
(rc, out, err) = self.execute_command(cmd)
return (rc, out, err)
@ -376,7 +376,7 @@ class OpenBsdGroup(Group):
cmd = [self.module.get_bin_path('groupadd', True)]
if self.gid is not None:
cmd.append('-g')
cmd.append('%d' % int(self.gid))
cmd.append(str(self.gid))
cmd.append(self.name)
return self.execute_command(cmd)
@ -385,7 +385,7 @@ class OpenBsdGroup(Group):
info = self.group_info()
if self.gid is not None and int(self.gid) != info[2]:
cmd.append('-g')
cmd.append('%d' % int(self.gid))
cmd.append(str(self.gid))
if len(cmd) == 1:
return (None, '', '')
if self.module.check_mode:
@ -418,7 +418,7 @@ class NetBsdGroup(Group):
cmd = [self.module.get_bin_path('groupadd', True)]
if self.gid is not None:
cmd.append('-g')
cmd.append('%d' % int(self.gid))
cmd.append(str(self.gid))
cmd.append(self.name)
return self.execute_command(cmd)
@ -427,7 +427,7 @@ class NetBsdGroup(Group):
info = self.group_info()
if self.gid is not None and int(self.gid) != info[2]:
cmd.append('-g')
cmd.append('%d' % int(self.gid))
cmd.append(str(self.gid))
if len(cmd) == 1:
return (None, '', '')
if self.module.check_mode:

View file

@ -0,0 +1,12 @@
#!/usr/bin/env python
import grp
gids = [g.gr_gid for g in grp.getgrall()]
i = 0
while True:
if i not in gids:
print(i)
break
i += 1

View file

@ -16,92 +16,23 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
- name: get the jinja2 version
shell: python -c 'import jinja2; print(jinja2.__version__)'
register: jinja2_version
delegate_to: localhost
- debug: var=jinja2_version
##
## group add
##
- name: try to create group
- name: ensure test groups are deleted before the test
group:
name: ansibullgroup
state: present
register: group_test0
name: '{{ item }}'
state: absent
loop:
- ansibullgroup
- ansibullgroup2
- name: make a list of groups
script: grouplist.sh "{{ ansible_distribution }}"
register: group_names
- block:
- name: run tests
include_tasks: tests.yml
- name: show group names
debug: var=group_names
- name: validate results for testcase 0
assert:
that:
- '"ansibullgroup" in group_names.stdout_lines'
##
## group check
##
- name: run existing group check tests
group:
name: "{{ group_names.stdout_lines|random }}"
state: present
with_sequence: start=1 end=5
register: group_test1
- debug: var=group_test1
- name: validate results for testcase 1
assert:
that:
- 'group_test1.results is defined'
- 'group_test1.results|length == 5'
- name: validate change results for testcase 1 (jinja2 >= 2.6)
assert:
that:
- "group_test1.results|map(attribute='changed')|unique|list == [False]"
- "group_test1.results|map(attribute='state')|unique|list == ['present']"
when: "jinja2_version.stdout is version('2.6', '>=')"
- name: validate change results for testcase 1 (jinja2 < 2.6)
assert:
that:
- "not group_test1.results[0]['changed']"
- "not group_test1.results[1]['changed']"
- "not group_test1.results[2]['changed']"
- "not group_test1.results[3]['changed']"
- "not group_test1.results[4]['changed']"
when: "jinja2_version.stdout is version('2.6', '<')"
##
## group remove
##
- name: try to delete the group
group:
name: ansibullgroup
always:
- name: remove test groups after test
group:
name: '{{ item }}'
state: absent
register: group_test2
- name: make a new list of groups
shell: |
cat /etc/group | cut -d: -f1
register: group_names2
when: 'ansible_distribution != "MacOSX"'
- name: make a list of groups
script: grouplist.sh "{{ ansible_distribution }}"
register: group_names2
- debug: var=group_names2
- name: validate results for testcase 2
assert:
that:
- '"ansibullgroup" not in group_names2.stdout_lines'
loop:
- ansibullgroup
- ansibullgroup2

View file

@ -0,0 +1,178 @@
---
##
## group add
##
- name: create group (check mode)
group:
name: ansibullgroup
state: present
register: create_group_check
check_mode: True
- name: get result of create group (check mode)
script: grouplist.sh "{{ ansible_distribution }}"
register: create_group_actual_check
- name: assert create group (check mode)
assert:
that:
- create_group_check is changed
- '"ansibullgroup" not in create_group_actual_check.stdout_lines'
- name: create group
group:
name: ansibullgroup
state: present
register: create_group
- name: get result of create group
script: grouplist.sh "{{ ansible_distribution }}"
register: create_group_actual
- name: assert create group
assert:
that:
- create_group is changed
- create_group.gid is defined
- '"ansibullgroup" in create_group_actual.stdout_lines'
- name: create group (idempotent)
group:
name: ansibullgroup
state: present
register: create_group_again
- name: assert create group (idempotent)
assert:
that:
- not create_group_again is changed
##
## group check
##
- name: run existing group check tests
group:
name: "{{ create_group_actual.stdout_lines|random }}"
state: present
with_sequence: start=1 end=5
register: group_test1
- name: validate results for testcase 1
assert:
that:
- group_test1.results is defined
- group_test1.results|length == 5
- name: validate change results for testcase 1
assert:
that:
- not group_test1 is changed
##
## group add with gid
##
- name: get the next available gid
script: gidget.py
args:
executable: '{{ ansible_python_interpreter }}'
register: gid
- name: create a group with a gid (check mode)
group:
name: ansibullgroup2
gid: '{{ gid.stdout_lines[0] }}'
state: present
register: create_group_gid_check
check_mode: True
- name: get result of create a group with a gid (check mode)
script: grouplist.sh "{{ ansible_distribution }}"
register: create_group_gid_actual_check
- name: assert create group with a gid (check mode)
assert:
that:
- create_group_gid_check is changed
- '"ansibullgroup2" not in create_group_gid_actual_check.stdout_lines'
- name: create a group with a gid
group:
name: ansibullgroup2
gid: '{{ gid.stdout_lines[0] }}'
state: present
register: create_group_gid
- name: get gid of created group
command: "{{ ansible_python_interpreter | quote }} -c \"import grp; print(grp.getgrnam('ansibullgroup2').gr_gid)\""
register: create_group_gid_actual
- name: assert create group with a gid
assert:
that:
- create_group_gid is changed
- create_group_gid.gid | int == gid.stdout_lines[0] | int
- create_group_gid_actual.stdout | trim | int == gid.stdout_lines[0] | int
- name: create a group with a gid (idempotent)
group:
name: ansibullgroup2
gid: '{{ gid.stdout_lines[0] }}'
state: present
register: create_group_gid_again
- name: assert create group with a gid (idempotent)
assert:
that:
- not create_group_gid_again is changed
- create_group_gid_again.gid | int == gid.stdout_lines[0] | int
##
## group remove
##
- name: delete group (check mode)
group:
name: ansibullgroup
state: absent
register: delete_group_check
check_mode: True
- name: get result of delete group (check mode)
script: grouplist.sh "{{ ansible_distribution }}"
register: delete_group_actual_check
- name: assert delete group (check mode)
assert:
that:
- delete_group_check is changed
- '"ansibullgroup" in delete_group_actual_check.stdout_lines'
- name: delete group
group:
name: ansibullgroup
state: absent
register: delete_group
- name: get result of delete group
script: grouplist.sh "{{ ansible_distribution }}"
register: delete_group_actual
- name: assert delete group
assert:
that:
- delete_group is changed
- '"ansibullgroup" not in delete_group_actual.stdout_lines'
- name: delete group (idempotent)
group:
name: ansibullgroup
state: absent
register: delete_group_again
- name: assert delete group (idempotent)
assert:
that:
- not delete_group_again is changed