Update Pure Storage FlashArray hostgroup module (#38503)

Allow for deletion and additon of hosts and volumes to a
volume group.
pull/22850/merge
Simon Dodsley 7 years ago committed by Adam Miller
parent d4e5e385ca
commit cc250156c4

@ -41,12 +41,37 @@ extends_documentation_fragment:
''' '''
EXAMPLES = r''' EXAMPLES = r'''
- name: Create new hostgroup - name: Create empty hostgroup
purefa_hg: purefa_hg:
hostgroup: foo hostgroup: foo
fa_url: 10.10.10.2 fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592 api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Add hosts and volumes to existing or new hostgroup
purefa_hg:
hostgroup: foo
host:
- host1
- host2
volume:
- vol1
- vol2
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
- name: Delete hosts and volumes from hostgroup
purefa_hg:
hostgroup: foo
host:
- host1
- host2
volume:
- vol1
- vol2
fa_url: 10.10.10.2
api_token: e31060a7-21fc-e277-6240-25983c6c4592
state: absent
# This will disconnect all hosts and volumes in the hostgroup # This will disconnect all hosts and volumes in the hostgroup
- name: Delete hostgroup - name: Delete hostgroup
purefa_hg: purefa_hg:
@ -86,9 +111,9 @@ def get_hostgroup(module, array):
hostgroup = None hostgroup = None
for h in array.list_hgroups(): for host in array.list_hgroups():
if h["name"] == module.params['hostgroup']: if host["name"] == module.params['hostgroup']:
hostgroup = h hostgroup = host
break break
return hostgroup return hostgroup
@ -98,30 +123,79 @@ def make_hostgroup(module, array):
changed = True changed = True
if not module.check_mode: try:
host = array.create_hgroup(module.params['hostgroup']) array.create_hgroup(module.params['hostgroup'])
if module.params['host']: except:
array.set_hgroup(module.params['hostgroup'], hostlist=module.params['host']) module.fail_json(msg='Failed to create hostgroup {0}'.format(module.params['hostgroup']))
if module.params['volume']: if module.params['host']:
for v in module.params['volume']: array.set_hgroup(module.params['hostgroup'], hostlist=module.params['host'])
array.connect_hgroup(module.params['hostgroup'], v) if module.params['volume']:
for vol in module.params['volume']:
array.connect_hgroup(module.params['hostgroup'], vol)
module.exit_json(changed=changed) module.exit_json(changed=changed)
def update_hostgroup(module, array): def update_hostgroup(module, array):
changed = False changed = False
hostgroup = module.params['hostgroup'] hgroup = get_hostgroup(module, array)
volumes = array.list_hgroup_connections(module.params['hostgroup'])
if module.params['state'] == "present":
if module.params['host']:
new_hosts = list(set(module.params['host']).difference(hgroup['hosts']))
if new_hosts:
try:
array.set_hgroup(module.params['hostgroup'], addhostlist=new_hosts)
changed = True
except:
module.fail_josn(msg='Failed to add host(s) to hostgroup')
if module.params['volume']:
if volumes:
current_vols = [vol['vol'] for vol in volumes]
new_volumes = list(set(module.params['volume']).difference(set(current_vols)))
for cvol in new_volumes:
try:
array.connect_hgroup(module.params['hostgroup'], cvol)
changed = True
except:
changed = False
else:
for cvol in module.params['volume']:
try:
array.connect_hgroup(module.params['hostgroup'], cvol)
changed = True
except:
changed = False
else:
if module.params['host']:
old_hosts = list(set(module.params['host']).intersection(hgroup['hosts']))
if old_hosts:
try:
array.set_hgroup(module.params['hostgroup'], remhostlist=old_hosts)
changed = True
except:
changed = False
if module.params['volume']:
old_volumes = list(set(module.params['volume']).difference(set([vol['name'] for vol in volumes])))
for cvol in old_volumes:
try:
array.disconnect_hgroup(module.params['hostgroup'], cvol)
changed = True
except:
changed = False
module.exit_json(changed=changed) module.exit_json(changed=changed)
def delete_hostgroup(module, array): def delete_hostgroup(module, array):
changed = True changed = True
if not module.check_mode: for vol in array.list_hgroup_connections(module.params['hostgroup']):
for vol in array.list_hgroup_connections(module.params['hostgroup']): array.disconnect_hgroup(module.params['hostgroup'], vol["vol"])
array.disconnect_hgroup(module.params['hostgroup'], vol["vol"]) host = array.get_hgroup(module.params['hostgroup'])
host = array.get_hgroup(module.params['hostgroup']) array.set_hgroup(module.params['hostgroup'], remhostlist=host['hosts'])
array.set_hgroup(module.params['hostgroup'], remhostlist=host['hosts']) try:
array.delete_hgroup(module.params['hostgroup']) array.delete_hgroup(module.params['hostgroup'])
except:
module.fail_json(msg='Failed to delete hostgroup {0}'.format(module.params['hostgroup']))
module.exit_json(changed=changed) module.exit_json(changed=changed)
@ -134,7 +208,7 @@ def main():
volume=dict(type='list'), volume=dict(type='list'),
)) ))
module = AnsibleModule(argument_spec, supports_check_mode=True) module = AnsibleModule(argument_spec, supports_check_mode=False)
if not HAS_PURESTORAGE: if not HAS_PURESTORAGE:
module.fail_json(msg='purestorage sdk is required for this module in host') module.fail_json(msg='purestorage sdk is required for this module in host')
@ -145,20 +219,24 @@ def main():
if module.params['host']: if module.params['host']:
try: try:
for h in module.params['host']: for hst in module.params['host']:
array.get_host(h) array.get_host(hst)
except: except:
module.fail_json(msg='Host not found') module.fail_json(msg='Host {0} not found'.format(hst))
if module.params['volume']: if module.params['volume']:
try: try:
for v in module.params['volume']: for vol in module.params['volume']:
array.get_volume(v) array.get_volume(vol)
except: except:
module.fail_json(msg='Volume not found') module.fail_json(msg='Volume {0} not found'.format(vol))
if hostgroup and state == 'present': if hostgroup and state == 'present':
update_hostgroup(module, array) update_hostgroup(module, array)
elif hostgroup and module.params['volume'] and state == 'absent':
update_hostgroup(module, array)
elif hostgroup and module.params['host'] and state == 'absent':
update_hostgroup(module, array)
elif hostgroup and state == 'absent': elif hostgroup and state == 'absent':
delete_hostgroup(module, array) delete_hostgroup(module, array)
elif hostgroup is None and state == 'absent': elif hostgroup is None and state == 'absent':
@ -166,6 +244,5 @@ def main():
else: else:
make_hostgroup(module, array) make_hostgroup(module, array)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save