def compare_static_route(module, network_cidr, route): """ Compare two static routes :arg module: The Ansible module instance :arg network_cidr: The ip_network object :arg route: The dict containing the static route to compare to :returns: Any differences between the two static routes """ new_route = deepcopy(route) if module.params.get('name'): new_route['name'] = module.params.get('name') if module.params.get('description'): new_route['description'] = module.params.get('description') if module.params.get('network'): new_route['destinationNetworkAddress'] = str(network_cidr.network_address) if module.params.get('prefix'): new_route['destinationPrefixSize'] = network_cidr.prefixlen if module.params.get('next_hop'): new_route['nextHopAddress'] = module.params.get('next_hop') compare_result = compare_json(new_route, route, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result.get('changes')
def compare_scripts(module, snapshot_config): """ Compare the existing Snapshot config to the provided arguments :arg module: The Ansible module instance :arg snapshot_config: The dict containing the existing Snapshot config to compare to :returns: Any differences between the the Snapshot configs """ new_config = {} old_config = { 'preSnapshotScript': snapshot_config.get('preSnapshotScript') or {}, 'postSnapshotScript': snapshot_config.get('postSnapshotScript') or {} } if module.params.get('pre_path') is not None: new_config['preSnapshotScript'] = { 'path': module.params.get('pre_path'), 'description': module.params.get('pre_description'), 'timeoutSeconds': module.params.get('pre_timeout'), 'failureHandling': module.params.get('on_failure') } or {} if module.params.get('post_path') is not None: new_config['postSnapshotScript'] = { "path": module.params.get('post_path'), "description": module.params.get('post_description') } or {} compare_result = compare_json(new_config, old_config, None) # Implement Check Mode if module.check_mode: module.exit_json(msg='Check mode', data=compare_result) return compare_result['changes']
def compare_ssl_profile(module, profile): """ Compare the certificate object provided to the Ansible module arguments :arg module: The Ansible module instance :arg profile: The existing SSL profile """ new_profile = deepcopy(profile) if module.params.get('new_name'): new_profile['name'] = module.params.get('new_name') if module.params.get('description'): new_profile['description'] = module.params.get('description') if module.params.get('ciphers'): new_profile['ciphers'] = module.params.get('ciphers') if module.params.get('certificate'): new_profile.get('sslDomainCertificate')['name'] = module.params.get('certificate').get('name') if module.params.get('chain'): new_profile.get('sslCertificateChain')['name'] = module.params.get('chain').get('name') compare_result = compare_json(new_profile, profile, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result.get('changes')
def compare_disk(module, existing_disk): """ Compare two disks :arg module: The Ansible module instance :arg client: The CC API client instance :arg existing_disk: The dict of the existing disk to be compared to :returns: Any differences between the two disk """ new_disk = deepcopy(existing_disk) disk_size = module.params.get('size') disk_speed = module.params.get('speed') disk_iops = module.params.get('iops') if disk_size: new_disk['sizeGb'] = disk_size if disk_speed: new_disk['speed'] = disk_speed if disk_iops: new_disk['iops'] = disk_iops compare_result = compare_json(new_disk, existing_disk, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result.get('changes')
def compare_fw_rule(new_fw_rule, existing_fw_rule): """ Compare two firewall rules and return any differences. This uses the generic compare_json but first the schemas of the firewall rules must be matched :arg new_fw_rule: The new firewall rule to check :arg existing_fw_rule: The existing firewall rule to check against :returns: dict containing any differences """ existing_dst = existing_fw_rule['destination'] existing_src = existing_fw_rule['source'] existing_fw_rule['destination'] = {} existing_fw_rule['source'] = {} # Handle schema differences between create/update schema and the returned get/list schema if 'ipAddressList' in existing_dst: existing_fw_rule['destination']['ipAddressListId'] = existing_dst['ipAddressList']['id'] if 'portList' in existing_dst: existing_fw_rule['destination']['portListId'] = existing_dst['portList']['id'] if 'ipAddressList' in existing_src: existing_fw_rule['source']['ipAddressListId'] = existing_src['ipAddressList']['id'] if 'portList' in existing_src: existing_fw_rule['source']['portListId'] = existing_src['portList']['id'] existing_fw_rule.pop('ruleType', None) existing_fw_rule.pop('datacenterId', None) existing_fw_rule.pop('state', None) new_fw_rule.pop('placement', None) return compare_json(new_fw_rule, existing_fw_rule, None)
def compare_vip_node(module, node): """ Compare two VIP nodes :arg module: The Ansible module instance :arg node: The dict containing the VIP node to compare to :returns: Any differences between the two VIP nodes """ new_node = deepcopy(node) if module.params.get('name'): new_node['status'] = module.params.get('status') if module.params.get('connection_limit'): new_node['connectionLimit'] = module.params.get('connection_limit') if module.params.get('connection_rate_limit'): new_node['connectionRateLimit'] = module.params.get( 'connection_rate_limit') if module.params.get('health_monitor'): if not new_node.get('healthMonitor'): new_node['healthMonitor'] = {} new_node['healthMonitor']['id'] = module.params.get('health_monitor') compare_result = compare_json(new_node, node, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result['changes']
def compare_user_roles(module, roles): """ Compare the provided roles to an existing user's roles :arg module: The Ansible module instance :arg roles: The existing roles to compare to :returns: Compare result """ return compare_json({'role': module.params.get('roles')}, {'role': roles}, None)
def compare_vip_pool(module, pool): """ Compare two VIP Pools :arg module: The Ansible module instance :arg client: The CC API client instance :arg pool: The dict of the existing VIP pool to be compared to :returns: Any differences between the two VIP pools """ new_pool = deepcopy(pool) existing_pool = deepcopy(pool) existing_health_monitors = [] existing_members = None if module.params.get('description'): new_pool['description'] = module.params.get('description') if module.params.get('load_balancing'): new_pool['loadBalanceMethod'] = module.params.get('load_balancing') if module.params.get('service_down_action'): new_pool['serviceDownAction'] = module.params.get( 'service_down_action') if module.params.get('slow_ramp_time'): new_pool['slowRampTime'] = module.params.get('slow_ramp_time') if module.params.get('no_health_monitor'): new_pool['healthMonitorId'] = {'nil': True} elif module.params.get('health_monitor'): new_pool['healthMonitor'] = module.params.get('health_monitor') for profile in pool.get('healthMonitor', []): existing_health_monitors.append(profile.get('id')) existing_pool['healthMonitor'] = existing_health_monitors if module.params.get('members'): existing_members = [] for existing_member in existing_pool.get('members'): existing_member_tmp = {} existing_member_tmp['id'] = existing_member.get('node').get('id') existing_member_tmp['port'] = existing_member.get('port') existing_member_tmp['status'] = existing_member.get('status') existing_members.append(existing_member_tmp) existing_pool['members'] = existing_members new_pool['members'] = module.params.get('members') compare_result = compare_json(new_pool, existing_pool, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result['changes']
def compare_user(module, user, full_compare): """ Compare the provided arguments to an existing User :arg module: The Ansible module instance :arg user: The user object to compare to :returns: Compare result """ new_user = deepcopy(user) if module.params.get('fullname'): new_user['fullName'] = module.params.get('fullname') if module.params.get('lastname'): new_user['lastName'] = module.params.get('lastname') if module.params.get('firstname'): new_user['firstName'] = module.params.get('firstname') if module.params.get('email'): new_user['emailAddress'] = module.params.get('email') if full_compare: if module.params.get('roles'): new_user['role'] = module.params.get('roles') if module.params.get('remove_phone'): if new_user.get('phone'): new_user.pop('phone') else: if module.params.get('phone'): if not new_user.get('phone'): new_user['phone'] = dict() new_user['phone']['number'] = str(module.params.get('phone')) if module.params.get('phone_country_code'): if new_user.get('phone'): new_user['phone']['countryCode'] = str( module.params.get('phone_country_code')) if module.params.get('department'): new_user['department'] = module.params.get('department') if module.params.get('custom_1'): new_user['customDefined1'] = module.params.get('custom_1') if module.params.get('custom_2'): new_user['customDefined2'] = module.params.get('custom_2') compare_result = compare_json(new_user, user, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result
def compare_network_domain(module, network_domain): """ Compare two Cloud Network Domains :arg module: The Ansible module instance :arg network_domain: The existingCloud Network Domain object :returns: Any differences between the two Cloud Network Domains """ new_network_domain = deepcopy(network_domain) if module.params.get('new_name'): new_network_domain['name'] = module.params.get('new_name') if module.params.get('description'): new_network_domain['description'] = module.params.get('description') if module.params.get('network_type'): new_network_domain['type'] = module.params.get('network_type') return compare_json(new_network_domain, network_domain, None)
def compare_security_group(module, sec_group): """ Compare an existing Security Group to one using the supplied arguments :arg module: The Ansible module instance :arg vlan: The existing VLAN object :returns: the comparison result """ new_sec_group = deepcopy(sec_group) if module.params.get('new_name'): new_sec_group['name'] = module.params.get('new_name') if module.params.get('description'): new_sec_group['description'] = module.params.get('description') compare_result = compare_json(new_sec_group, sec_group, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result.get('changes')
def compare_snat_exclusion(module, new_network_cidr, snat): """ Compare two SNAT exclusions :arg module: The Ansible module instance :arg snat: The dict containing the SNAT exclusion to compare to :returns: Any differences between the two SNAT exclusions """ new_snat = deepcopy(snat) if module.params.get('description'): new_snat['description'] = module.params.get('description') if new_network_cidr: new_snat['destinationIpv4NetworkAddress'] = str( new_network_cidr.network_address) new_snat['destinationIpv4PrefixSize'] = new_network_cidr.prefixlen compare_result = compare_json(new_snat, snat, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result['changes']
def compare_port_list(module, client, network_domain_id, port_list, return_all=False): """ Compare two port lists :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of a Cloud Network Domain :arg port_list: The dict of the existing port list to be compared to :arg return_all: If True returns the full list of changes otherwise just True/False :returns: Any differences between the two port lists """ # Handle schema differences between the returned API object and the one required to be sent child_port_list_ids = [] for i in range(len(port_list['childPortList'])): child_port_list_ids.append(port_list['childPortList'][i].get('id')) port_list.pop('childPortList') port_list['childPortListId'] = child_port_list_ids port_list.pop('state') port_list.pop('createTime') new_port_list = client.port_list_args_to_dict( False, network_domain_id, port_list.get('id'), port_list.get('name'), module.params.get('description'), module.params.get('ports'), module.params.get('ports_nil'), module.params.get('child_port_lists'), module.params.get('child_port_lists_nil')) if module.params.get( 'child_port_lists_nil') or not port_list.get('childPortListId'): new_port_list['childPortListId'] = [] if module.params.get('ports_nil') and not port_list.get('ports'): new_port_list['ports'] = [] compare_result = compare_json(new_port_list, port_list, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) if return_all: return compare_result return compare_result.get('changes')
def compare_vlan(module, vlan): """ Compare an existing VLAN to one using the supplied arguments :arg module: The Ansible module instance :arg vlan: The existing VLAN object :returns: the comparison result """ new_vlan = deepcopy(vlan) if module.params.get('new_name'): new_vlan['name'] = module.params.get('new_name') if module.params.get('description'): new_vlan['description'] = module.params.get('description') if module.params.get('detached_vlan_gw'): new_vlan['ipv4GatewayAddress'] = module.params.get('detached_vlan_gw') if module.params.get('detached_vlan_gw_ipv6'): new_vlan['ipv6GatewayAddress'] = module.params.get('detached_vlan_gw_ipv6') compare_result = compare_json(new_vlan, vlan, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result.get('changes')
def compare_snapshot(module, snapshot_config): """ Compare the existing Snapshot config to the provided arguments :arg module: The Ansible module instance :arg snapshot_config: The dict containing the existing Snapshot config to compare to :returns: Tuple of any differences between the the Snapshot configs, service is item 0 and replication is item 1 """ service_change = replication_change = False new_config = deepcopy(snapshot_config) if module.params.get('plan'): new_config['servicePlan'] = module.params.get('plan') if module.params.get('replication'): new_config['replicationTargetDatacenterId'] = module.params.get( 'replication') if module.params.get('window'): new_config['window'] = { 'dayOfWeek': '', 'startHour': module.params.get('window') } if module.params.get('plan') in [ 'ONE_MONTH', 'THREE_MONTH', 'TWELVE_MONTH' ]: new_config['window']['dayOfWeek'] = 'DAILY' compare_result = compare_json(new_config, snapshot_config, None) # determine if the change is a service or replication or both change consolidate_changes = compare_result.get('added') consolidate_changes.update(compare_result.get('updated')) if 'servicePlan' in consolidate_changes or 'window' in consolidate_changes: service_change = True if 'replicationTargetDatacenterId' in consolidate_changes: replication_change = True # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return (service_change, replication_change)
def compare_ip_list(module, client, network_domain_id, ip_list, return_all=False): """ Compare two IP address lists :arg module: The Ansible module instance :arg client: The CC API client instance :arg new_fw_rule: The dict containing the specification for the new rule based on the supplied parameters :arg ip_list: The existing IP address list object to be compared :arg return_all: If True returns the full list of changes otherwise just True/False :returns: Any differences between the two IP address lists """ # Handle schema differences between the returned API object and the one required to be sent child_ip_list_ids = [] for i in range(len(ip_list['childIpAddressList'])): child_ip_list_ids.append(ip_list['childIpAddressList'][i].get('id')) ip_list.pop('childIpAddressList') ip_list['childIpAddressListId'] = child_ip_list_ids ip_list.pop('state') ip_list.pop('createTime') new_ip_list = client.ip_list_args_to_dict( False, network_domain_id, ip_list.get('id'), ip_list.get('name'), module.params.get('description'), module.params.get('ip_addresses'), module.params.get('ip_addresses_nil'), module.params.get('child_ip_lists'), module.params.get('child_ip_lists_nil'), module.params.get('version')) if module.params.get( 'child_ip_lists_nil') and not ip_list.get('childIpAddressListId'): new_ip_list['childIpAddressListId'] = [] if module.params.get('ip_addresses_nil') and not ip_list.get('ipAddress'): new_ip_list['ipAddress'] = [] # Handle case where no child IP address list is required but the schema still returns an empty list if not ip_list.get('childIpAddressListId') and not module.params.get( 'child_ip_lists'): ip_list.pop('childIpAddressListId') # Handle differences in IPv6 address formatting between Cloud Control and everything else if ip_list.get('ipVersion') == 'IPV6': if ip_list.get('ipAddress') and new_ip_list.get('ipAddress'): for num, ipv6 in enumerate(ip_list.get('ipAddress'), start=0): ip_list.get('ipAddress')[num]['begin'] = str( ipaddress.ip_address(unicode(ipv6.get('begin'))).exploded) if ipv6.get('end'): ip_list.get('ipAddress')[num]['end'] = str( ipaddress.ip_address(unicode( ipv6.get('end'))).exploded) for num, ipv6 in enumerate(new_ip_list.get('ipAddress'), start=0): new_ip_list.get('ipAddress')[num]['begin'] = str( ipaddress.ip_address(unicode(ipv6.get('begin'))).exploded) if ipv6.get('end'): new_ip_list.get('ipAddress')[num]['end'] = str( ipaddress.ip_address(unicode( ipv6.get('end'))).exploded) compare_result = compare_json(new_ip_list, ip_list, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) if return_all: return compare_result return compare_result['changes']
def update_vip_pool(module, client, pool): """ Update a VIP pool :arg module: The Ansible module instance :arg client: The CC API client instance :arg pool: The dict containing the existing pool to be updated :returns: The updated VIP pool UUID """ pool_result = {} try: pool_id = client.update_vip_pool( pool.get('id'), module.params.get('description'), module.params.get('load_balancing'), module.params.get('health_monitor'), module.params.get('no_health_monitor'), module.params.get('service_down_action'), module.params.get('slow_ramp_time')) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as exc: module.fail_json(msg='Could not update the VIP Pool - {0}'.format(exc)) # The next code block is not exactly stellar but is required to handle schema differences between objects/dicts # in order to be able to accurately compare two VIP pools and determine any differences if module.params.get('members'): try: updated_members = [] removed_members = [] added_members = [] for existing_member in pool.get('members'): existing_member_found = False for new_member in module.params.get('members'): if existing_member.get('node').get('id') == new_member.get( 'id'): existing_member_tmp = {} existing_member_tmp['id'] = existing_member.get( 'node').get('id') existing_member_tmp['port'] = existing_member.get( 'port') existing_member_tmp['status'] = existing_member.get( 'status') compare_result = compare_json(new_member, existing_member_tmp, None) if compare_result: if compare_result['changes']: # Save the actual pool member ID as it will be needed for the update new_member['member_id'] = existing_member.get( 'id') updated_members.append(new_member) existing_member_found = True if not existing_member_found: removed_members.append(existing_member) for new_member in module.params.get('members'): new_member_found = False for existing_member in pool.get('members'): if new_member.get('id') == existing_member.get('node').get( 'id'): new_member_found = True if not new_member_found: added_members.append(new_member) # Make the member changes. Induce a 0.5sec pause to stop the API from being spammed on large member lists for member in removed_members: client.remove_vip_pool_member(member.get('id')) sleep(0.5) for member in added_members: client.add_vip_pool_member(pool_id, member.get('id'), member.get('port'), member.get('status')) sleep(0.5) for member in updated_members: client.remove_vip_pool_member(member.get('member_id')) sleep(0.5) client.add_vip_pool_member(pool_id, member.get('id'), member.get('port'), member.get('status')) sleep(0.5) except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as exc: module.fail_json( msg= 'The VIP Pool was updated but there was a issue updating the members of the Pool. ' 'Check the list of member objects - {0}'.format(exc)) try: pool_result = client.get_vip_pool(pool_id).get('id') except (AttributeError, KeyError, NTTMCPAPIException): pass module.exit_json(changed=True, data=pool_result)
def compare_fw_rule(new_fw_rule, existing_fw_rule): """ Compare two firewall rules :arg new_fw_rule: The dict containing the specification for the new rule based on the supplied parameters :arg existing_fw_rule: The dict containing the speification for the existing firewall rule :returns: Any differences between the two firewall rules """ # Handle schema differences between the create/update schema and the returned get/list schema if existing_fw_rule.get('ruleType') != 'DEFAULT_RULE': existing_dst = existing_fw_rule['destination'] existing_src = existing_fw_rule['source'] existing_fw_rule['destination'] = {} existing_fw_rule['source'] = {} if 'ipAddressList' in existing_dst: existing_fw_rule['destination']['ipAddressListId'] = existing_dst[ 'ipAddressList']['id'] elif 'ip' in existing_dst: existing_fw_rule['destination']['ip'] = {} existing_fw_rule['destination']['ip']['address'] = existing_dst[ 'ip']['address'] if 'prefixSize' in existing_dst['ip']: existing_fw_rule['destination']['ip']['prefixSize'] = str( existing_dst['ip']['prefixSize']) if 'portList' in existing_dst: existing_fw_rule['destination']['portListId'] = existing_dst[ 'portList']['id'] elif 'port' in existing_dst: existing_fw_rule['destination']['port'] = {} existing_fw_rule['destination']['port']['begin'] = str( existing_dst['port']['begin']) if 'end' in existing_dst['port']: existing_fw_rule['destination']['port']['end'] = str( existing_dst['port']['end']) if 'ipAddressList' in existing_src: existing_fw_rule['source']['ipAddressListId'] = existing_src[ 'ipAddressList']['id'] elif 'ip' in existing_src: existing_fw_rule['source']['ip'] = {} existing_fw_rule['source']['ip']['address'] = existing_src['ip'][ 'address'] if 'prefixSize' in existing_src['ip']: existing_fw_rule['source']['ip']['prefixSize'] = str( existing_src['ip']['prefixSize']) if 'portList' in existing_src: existing_fw_rule['source']['portListId'] = existing_src[ 'portList']['id'] elif 'port' in existing_src: existing_fw_rule['source']['port'] = {} existing_fw_rule['source']['port']['begin'] = str( existing_src['port']['begin']) if 'end' in existing_src['port']: existing_fw_rule['source']['port']['end'] = str( existing_src['port']['end']) existing_fw_rule.pop('ruleType', None) existing_fw_rule.pop('datacenterId', None) existing_fw_rule.pop('state', None) existing_fw_rule.pop('ipVersion', None) existing_fw_rule.pop('name', None) existing_fw_rule.pop('networkDomainId', None) return compare_json(new_fw_rule, existing_fw_rule, None)
def compare_vip_listener(module, client, network_domain_id, vip_listener): """ Compare two VIP listeners :arg module: The Ansible module instance :arg client: The CC API client instance :arg network_domain_id: The UUID of a Cloud Network Domain :arg cip_listener: The dict of the existing VIP listener to be compared to :returns: Any differences between the two VIP listeners """ new_vip_listener = deepcopy(vip_listener) existing_vip_listener = deepcopy(vip_listener) (pool_id_1, pool_id_2) = get_vip_pools(module, client, network_domain_id) (persistence_profile_id_1, persistence_profile_id_2) = get_persistence_profiles( module, client, network_domain_id) ssl_profile_id = get_ssl_offload_profile(module, client, network_domain_id) optimization_profile = get_optomization_profile(module) irule_id_list = get_irules(module, client, network_domain_id) # Handle the schema differences if existing_vip_listener.get('description'): new_vip_listener['description'] = module.params.get('description') new_vip_listener['enabled'] = module.params.get('enabled') new_vip_listener['connectionLimit'] = module.params.get('connection_limit') new_vip_listener['connectionRateLimit'] = module.params.get( 'connection_rate_limit') new_vip_listener['sourcePortPreservation'] = module.params.get( 'preservation') if existing_vip_listener.get('pool'): existing_vip_listener['poolId'] = existing_vip_listener.get( 'pool').get('id') if not (existing_vip_listener.get('pool') is None and pool_id_1 is None): new_vip_listener['poolId'] = pool_id_1 if existing_vip_listener.get('clientClonePool'): existing_vip_listener['clientClonePoolId'] = existing_vip_listener.get( 'clientClonePool').get('id') if not (existing_vip_listener.get('clientClonePool') is None and pool_id_2 is None): new_vip_listener['clientClonePoolId'] = pool_id_2 if existing_vip_listener.get('persistenceProfile'): existing_vip_listener[ 'persistenceProfileId'] = existing_vip_listener.get( 'persistenceProfile').get('id') if not (existing_vip_listener.get('persistenceProfile') is None and persistence_profile_id_1 is None): new_vip_listener['persistenceProfileId'] = persistence_profile_id_1 if existing_vip_listener.get('fallbackPersistenceProfile'): existing_vip_listener[ 'fallbackPersistenceProfileId'] = existing_vip_listener.get( 'fallbackPersistenceProfile').get('id') if not (existing_vip_listener.get('fallbackPersistenceProfile') is None and persistence_profile_id_2 is None): new_vip_listener[ 'fallbackPersistenceProfileId'] = persistence_profile_id_2 new_vip_listener['optimizationProfile'] = optimization_profile if existing_vip_listener.get('sslOffloadProfile'): existing_vip_listener[ 'sslOffloadProfileId'] = existing_vip_listener.get( 'sslOffloadProfile').get('id') if not (existing_vip_listener.get('sslOffloadProfile') is None and ssl_profile_id is None): new_vip_listener['sslOffloadProfileId'] = ssl_profile_id if existing_vip_listener.get('irule'): existing_irules = existing_vip_listener.get('irule') existing_vip_listener['irule'] = [] for existing_irule in existing_irules: existing_vip_listener.get('irule').append(existing_irule.get('id')) new_vip_listener['irule'] = irule_id_list compare_result = compare_json(new_vip_listener, existing_vip_listener, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result['changes']
def compare_vapp(module, vapp): """ Compare an existing vApp configuration to one using the supplied arguments :arg module: The Ansible module instance :arg vapp: The existing vApp configuration :returns: the comparison result """ new_vapp = dict() old_vapp_props = list() vapp_spec = module.params.get('vapp') new_vapp['vAppProperty'] = list() try: for prop in vapp.get('vAppProperty', list()): if 'password' in prop.get('schema').get('type'): if prop.get('value'): del(prop['value']) if prop.get('schema').get('defaultValue'): del(prop['schema']['defaultValue']) new_vapp['isoTransport'] = module.params.get('iso') new_vapp['vmwareTransport'] = module.params.get('vmtools') for prop in vapp_spec: for old_prop in vapp.get('vAppProperty', list()): if prop.get('key') == old_prop.get('key'): old_vapp_props.append(old_prop) temp_prop = dict() temp_prop['key'] = prop.get('key') if 'password' not in prop.get('type'): temp_prop['value'] = prop.get('value') temp_prop['schema'] = dict() temp_prop['schema']['id'] = prop.get('id') temp_prop['schema']['type'] = prop.get('type') if prop.get('class_id') is not None: temp_prop['schema']['classId'] = prop.get('class_id') if prop.get('instance_id') is not None: temp_prop['schema']['instanceId'] = str(prop.get('instance_id')) if prop.get('category') is not None: temp_prop['schema']['category'] = prop.get('category') if prop.get('label') is not None: temp_prop['schema']['label'] = prop.get('label') if prop.get('description') is not None: temp_prop['schema']['description'] = prop.get('description') if prop.get('configurable') is not None: temp_prop['schema']['userConfigurable'] = prop.get('configurable') else: temp_prop['schema']['userConfigurable'] = True if prop.get('default_value') is not None and 'password' not in prop.get('type'): temp_prop['schema']['defaultValue'] = prop.get('default_value') new_vapp['vAppProperty'].append(temp_prop) vapp['vAppProperty'] = old_vapp_props new_vapp['vAppProperty'] = sorted(new_vapp.get('vAppProperty'), key=itemgetter('key')) vapp['vAppProperty'] = sorted(vapp.get('vAppProperty'), key=itemgetter('key')) compare_result = compare_json(new_vapp, vapp, None) # Implement Check Mode if module.check_mode: module.exit_json(data=compare_result) return compare_result.get('changes') except (KeyError, IndexError, AttributeError) as e: module.fail_json(msg='Error determining state changes (if any): {0}'.format(e))