def main(): # define the available arguments/parameters that a user can pass to # the module argument_spec = nfvis_argument_spec() argument_spec.update( state=dict(type='str', choices=['absent', 'present'], default='present'), name=dict(type='str', required=True, aliases=['network']), bridge=dict(type='str'), trunk=dict(type='bool', default=True), sriov=dict(type='bool', default=False), native_tagged=dict(type='bool'), native_vlan=dict(type='str'), vlan=dict(type='str'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) nfvis = nfvisModule(module) payload = None port = None nfvis.result['changed'] = False # Get the list of existing networks url_path = '/config/networks?deep' response = nfvis.request(url_path, method='GET') nfvis.result['current'] = response nfvis.result['what_changed'] = [] # Turn the list of dictionaries returned in the call into a dictionary of dictionaries hashed by the network name network_dict = {} try: for item in response['network:networks']['network']: name = item['name'] network_dict[name] = item # nfvis.result['debug'] = network_dict except TypeError: pass except KeyError: pass if nfvis.params['state'] == 'present': if nfvis.params['name'] not in network_dict: # Construct the payload payload = {'network': {}} payload['network']['name'] = nfvis.params['name'] if nfvis.params['bridge']: payload['network']['bridge'] = nfvis.params['bridge'] else: module.fail_json( msg="bridge must be specified when state is present") if nfvis.params['trunk'] == False: payload['network']['trunk'] = nfvis.params['trunk'] if nfvis.params['vlan']: payload['network']['vlan'] = nfvis.params['vlan'] if nfvis.params['sriov']: payload['network']['sriov'] = nfvis.params['sriov'] if nfvis.params['native_vlan']: payload['network']['native-vlan'] = nfvis.params['native_vlan'] # The network does not exist on the device, so add it url_path = '/config/networks' if not module.check_mode: response = nfvis.request(url_path, method='POST', payload=json.dumps(payload)) nfvis.result['changed'] = True else: # The bridge exists on the device, so let's start with the original payload and see if anything changed payload = {'network': network_dict[nfvis.params['name']]} if payload['network']['bridge'] != nfvis.params['bridge']: payload['network']['bridge'] = nfvis.params['bridge'] nfvis.result['what_changed'].append('bridge') if nfvis.params['trunk'] == False: if 'trunk' not in payload['network'] or payload['network'][ 'trunk'] == True: payload['network']['trunk'] = False nfvis.result['what_changed'].append('trunk') if nfvis.params['vlan']: if 'vlan' not in payload['network'] or nfvis.params[ 'vlan'] not in payload['network']['vlan']: payload['network']['vlan'] = nfvis.params['vlan'] nfvis.result['what_changed'].append('vlan') if nfvis.params['sriov']: if 'sriov' not in payload['network'] or nfvis.params[ 'sriov'] != payload['network']['sriov']: payload['network']['sriov'] = nfvis.params['sriov'] nfvis.result['what_changed'].append('sriov') if nfvis.params['native_tagged']: if 'native_tagged' not in payload['network'] or nfvis.params[ 'native_tagged'] != payload['network']['native_tagged']: payload['network']['native_tagged'] = nfvis.params[ 'native_tagged'] nfvis.result['what_changed'].append('native_tagged') if nfvis.params['native_vlan']: if 'native_vlan' not in payload['network'] or nfvis.params[ 'native_vlan'] != payload['network']['native_vlan']: payload['network']['native_vlan'] = nfvis.params[ 'native_vlan'] nfvis.result['what_changed'].append('native_vlan') if nfvis.result['what_changed']: url_path = '/config/networks/network/{0}'.format( nfvis.params['name']) nfvis.result['changed'] = True if not module.check_mode: response = nfvis.request(url_path, method='PUT', payload=json.dumps(payload)) else: if nfvis.params['name'] in network_dict: url_path = '/config/networks/network/{0}'.format( nfvis.params['name']) nfvis.result['changed'] = True if not module.check_mode: response = nfvis.request(url_path, 'DELETE') # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results nfvis.exit_json(**nfvis.result)
def main(): # define the available arguments/parameters that a user can pass to # the module argument_spec = nfvis_argument_spec() argument_spec.update(hostname=dict(type='str', required=True), trusted_source=dict(type='list'), dpdk=dict(type='bool'), mgmt=dict(type='str', required=True), default_gw=dict(type='str')) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) if not HAS_NETADDR: module.fail_json( msg= 'Could not import the python library netaddr required by this module' ) nfvis = nfvisModule(module) payload = None port = None nfvis.result['changed'] = False # Make sure that we have a righteous mgmt IP address try: mgmt_ip = netaddr.IPNetwork(nfvis.params['mgmt']) except ValueError: module.fail_json(msg="mgmt address/netmask is invalid: {0}".format( nfvis.params['mgmt'])) # Get the list of existing vlans response = nfvis.request('/config/system/settings') nfvis.result['current'] = response nfvis.result['what_changed'] = [] payload = {'settings': response['system:settings']} if nfvis.params['hostname'] and nfvis.params['hostname'].split( '.')[0] != payload['settings']['hostname']: payload['settings']['hostname'] = nfvis.params['hostname'].split( '.')[0] nfvis.result['what_changed'].append('hostname') if nfvis.params['trusted_source']: ip_receive_acl = [] for network in nfvis.params['trusted_source']: ip_receive_acl.append({ 'source': network, 'action': 'accept', 'priority': 0, 'service': ['https', 'icmp', 'netconf', 'scpd', 'snmp', 'ssh'] }) if 'ip-receive-acls' in payload[ 'settings'] and 'ip-receive-acl' in payload['settings'][ 'ip-receive-acls']: if payload['settings']['ip-receive-acls'][ 'ip-receive-acl'] != ip_receive_acl: nfvis.result['what_changed'].append('trusted_source') payload['settings']['ip-receive-acls'] = { 'ip-receive-acl': ip_receive_acl } else: nfvis.result['what_changed'].append('trusted_source') payload['settings']['ip-receive-acls'] = { 'ip-receive-acl': ip_receive_acl } if nfvis.params['dpdk'] and ( ('dpdk' in payload['settings'] and nfvis.params['dpdk'] != payload['settings']['dpdk']) or ('dpdk' not in payload['settings'])): payload['settings']['dpdk'] = ['disable', 'enable'][nfvis.params['dpdk'] == True] nfvis.result['what_changed'].append('dpdk') if 'mgmt' in payload['settings']: if nfvis.params['mgmt'] == 'dhcp' and 'dhcp' not in payload[ 'settings']['mgmt']: payload['settings']['mgmt']['dhcp'] = None nfvis.result['what_changed'].append('mgmt') else: if 'address' not in payload['settings']['mgmt'][ 'ip'] or 'netmask' not in payload['settings']['mgmt']['ip']: payload['settings']['mgmt']['ip'] = { 'address': str(mgmt_ip.ip), 'netmask': str(mgmt_ip.netmask) } nfvis.result['what_changed'].append('mgmt_added') elif payload['settings']['mgmt']['ip']['address'] != str( mgmt_ip.ip): payload['settings']['mgmt']['ip'] = { 'address': str(mgmt_ip.ip), 'netmask': str(mgmt_ip.netmask) } nfvis.result['what_changed'].append('mgmt_ip') elif payload['settings']['mgmt']['ip']['netmask'] != str( mgmt_ip.netmask): payload['settings']['mgmt']['ip'] = { 'address': str(mgmt_ip.ip), 'netmask': str(mgmt_ip.netmask) } nfvis.result['what_changed'].append('mgmt_netmask') else: payload['settings']['mgmt'] = {} payload['settings']['mgmt']['ip'] = { 'address': str(mgmt_ip.ip), 'netmask': str(mgmt_ip.netmask) } nfvis.result['what_changed'].append('mgmt') if nfvis.params['default_gw']: if 'default-gw' in payload['settings'] and payload['settings'][ 'default-gw'] != nfvis.params['default_gw']: payload['settings']['default-gw'] = nfvis.params['default_gw'] nfvis.result['what_changed'].append('default_gw') elif 'default-gw' not in payload['settings']: payload['settings']['default-gw'] = nfvis.params['default_gw'] nfvis.result['what_changed'].append('default_gw') if nfvis.result['what_changed']: nfvis.result['changed'] = True url_path = '/config/system/settings' if not module.check_mode: response = nfvis.request(url_path, method='PUT', payload=json.dumps(payload)) nfvis.exit_json(**nfvis.result)
def run_module(): # define available arguments/parameters a user can pass to the module argument_spec = nfvis_argument_spec() argument_spec.update(state=dict(type='str', choices=['absent', 'present'], default='present'), name=dict(type='str', required=True), file=dict(type='str', required=True), dest=dict(type='str', default='/data/intdatastore/uploads'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, original_message='', message='' ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) nfvis = nfvisModule(module) if not HAS_PARAMIKO: nfvis.fail_json( msg='library paramiko is required when file_pull is False but does not appear to be ' 'installed. It can be installed using `pip install paramiko`' ) if not HAS_SCP: nfvis.fail_json( msg='library scp is required when file_pull is False but does not appear to be ' 'installed. It can be installed using `pip install scp`' ) # Get the list of existing packages response = nfvis.request('/config/vm_lifecycle/images?deep') nfvis.result['current'] = response # Turn the list of dictionaries returned in the call into a dictionary of dictionaries hashed by the deployment name images_dict = {} try: for item in response['vmlc:images']['image']: name = item['name'] images_dict[name] = item except TypeError: pass except KeyError: pass if nfvis.params['state'] == 'present': if nfvis.params['name'] not in images_dict: if not module.check_mode: try: ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.load_system_host_keys() ssh.connect(hostname=module.params['host'], port=22222, username=module.params['user'], password=module.params['password'], look_for_keys=False) except paramiko.AuthenticationException: nfvis.fail_json(msg = 'Authentication failed, please verify your credentials') except paramiko.SSHException as sshException: nfvis.fail_json(msg = 'Unable to establish SSH connection: %s' % sshException) except paramiko.BadHostKeyException as badHostKeyException: nfvis.fail_json(msg='Unable to verify servers host key: %s' % badHostKeyException) except Exception as e: nfvis.fail_json(msg=e.args) try: with SCPClient(ssh.get_transport()) as scp: scp.put(module.params['file'], '/data/intdatastore/uploads/{0}.tar.gz'.format(nfvis.params['name'])) except Exception as e: nfvis.fail_json(msg="Operation error: %s" % e) scp.close() payload = {'image': {}} payload['image']['name'] = nfvis.params['name'] payload['image']['src'] = 'file://{0}/{1}.tar.gz'.format(nfvis.params['dest'], nfvis.params['name']) url_path = '/config/vm_lifecycle/images' if not module.check_mode: response = nfvis.request(url_path, method='POST', payload=json.dumps(payload)) nfvis.result['changed'] = True else: if nfvis.params['name'] in images_dict: # Delete the image url_path = '/config/vm_lifecycle/images/image/{0}'.format(nfvis.params['name']) if not module.check_mode: response = nfvis.request(url_path, method='DELETE') # Delete the file filename = (images_dict[nfvis.params['name']]['src'].split('://'))[1] payload = { 'input': { 'name': filename } } url_path = '/operations/system/file-delete/file' if not module.check_mode: response = nfvis.request(url_path, method='POST', payload=json.dumps(payload)) nfvis.result['changed'] = True else: nfvis.result['changed'] = False nfvis.exit_json(**nfvis.result)
def main(): # define the available arguments/parameters that a user can pass to # the module argument_spec = nfvis_argument_spec() argument_spec.update(state=dict(type='str', choices=['absent', 'present'], default='present'), name=dict(type='str', aliases=['bridge']), ports=dict(type='list'), ip=dict(type='list'), vlan=dict(type='int'), purge=dict(type='bool', default=False), dhcp=dict(type='bool')) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) nfvis = nfvisModule(module) payload = None port = None nfvis.result['changed'] = False # Get the list of existing bridges response = nfvis.request('/config/bridges?deep') nfvis.result['current'] = response nfvis.result['what_changed'] = [] # Turn the list of dictionaries returned in the call into a dictionary of dictionaries hashed by the bridge name bridge_dict = {} try: for item in response['network:bridges']['bridge']: name = item['name'] bridge_dict[name] = item nfvis.result['debug'] = bridge_dict except TypeError: pass except KeyError: pass if nfvis.params['state'] == 'present': if nfvis.params['name'] not in bridge_dict or nfvis.params[ 'purge'] == True: # If the # Construct the payload payload = {'bridge': {}} payload['bridge']['name'] = nfvis.params['name'] if nfvis.params['dhcp'] == True: payload['bridge']['dhcp'] = [None] payload['bridge']['port'] = [] if nfvis.params['ports']: for port in nfvis.params['ports']: payload['bridge']['port'].append({'name': port}) payload['bridge'].pop('vlan', None) if nfvis.params['vlan']: payload['bridge']['vlan'] = nfvis.params['vlan'] if nfvis.params['ip']: payload['bridge']['ip'] = {} if 'address' in nfvis.params['ip']: payload['bridge']['ip']['address'] = nfvis.params['ip'][ 'address'] else: module.fail_json(msg="address must be specified for ip") if 'netmask' in nfvis.params['ip']: payload['bridge']['ip']['netmask'] = nfvis.params['ip'][ 'netmask'] else: module.fail_json(msg="netmask must be specified for ip") if nfvis.params['name'] in bridge_dict: # We are overwritting (purging) what is on the NFVIS host url_path = '/config/bridges/bridge/{0}'.format( nfvis.params['name']) if not module.check_mode: response = nfvis.request(url_path, method='PUT', payload=json.dumps(payload)) else: url_path = '/config/bridges' if not module.check_mode: response = nfvis.request(url_path, method='POST', payload=json.dumps(payload)) nfvis.result['changed'] = True else: # The bridge exists on the device, so let's start with the original payload and see if anything changed payload = {'bridge': bridge_dict[nfvis.params['name']]} if nfvis.params['ports']: # Check ports if 'port' not in payload['bridge']: payload['bridge']['port'] = [] # No ports are on the NFVIS host, so add them all for port in nfvis.params['ports']: payload['bridge']['port'].append({'name': port}) nfvis.result['what_changed'].append('port') else: # Add the ports that are not already on the NFVIS host existing_ports = [] for item in payload['bridge']['port']: existing_ports.append(item['name']) for port in nfvis.params['ports']: if port not in existing_ports: payload['bridge']['port'].append({'name': port}) nfvis.result['what_changed'].append('port') if nfvis.params['vlan']: if 'vlan' not in payload['bridge'] or nfvis.params[ 'vlan'] != payload['bridge']['vlan']: payload['bridge']['vlan'] = nfvis.params['vlan'] nfvis.result['what_changed'].append('vlan') if nfvis.params['dhcp']: if nfvis.params['dhcp'] == True and 'dhcp' not in payload[ 'bridge']: payload['bridge']['dhcp'] = [nfvis.params['dhcp']] nfvis.result['what_changed'].append('dhcp') elif nfvis.params['dhcp'] == False and 'dhcp' in payload[ 'bridge']: payload['bridge']['dhcp'] = None nfvis.result['what_changed'].append('dhcp') if nfvis.params['ip']: if 'ip' not in payload['bridge']: # No ip on the NFVIS host, so add the entire dict payload['bridge']['ip'] = nfvis.params['ip'] nfvis.result['what_changed'].append('ip') else: if 'address' in nfvis.params['ip']: if payload['bridge']['ip']['address'] != nfvis.params[ 'ip']['address']: payload['bridge']['ip']['address'] = nfvis.params[ 'ip']['address'] nfvis.result['what_changed'].append('ip') else: module.fail_json( msg="address must be specified for ip") if 'netmask' in nfvis.params['ip']: if payload['bridge']['ip']['netmask'] != nfvis.params[ 'ip']['netmask']: payload['bridge']['ip']['netmask'] = nfvis.params[ 'ip']['netmask'] nfvis.result['what_changed'].append('ip') else: module.fail_json( msg="netmask must be specified for ip") if nfvis.result['what_changed']: url_path = '/config/bridges/bridge/{0}'.format( nfvis.params['name']) nfvis.result['changed'] = True if not module.check_mode: response = nfvis.request(url_path, method='PUT', payload=json.dumps(payload)) else: if nfvis.params['name'] in bridge_dict: url_path = '/config/bridges/bridge/{0}'.format( nfvis.params['name']) nfvis.result['changed'] = True if not module.check_mode: response = nfvis.request(url_path, 'DELETE') # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results nfvis.exit_json(**nfvis.result)
def main(): # define the available arguments/parameters that a user can pass to # the module argument_spec = nfvis_argument_spec() argument_spec.update(state=dict(type='str', choices=['absent', 'present'], default='present'), name=dict(type='str', aliases=['deployment']), image=dict(type='str'), flavor=dict(type='str'), bootup_time=dict(type='int', default=-1), recovery_wait_time=dict(type='int', default=0), kpi_data=dict(type='bool', default=False), scaling=dict(type='bool', default=False), scaling_min_active=dict(type='int', default=1), scaling_max_active=dict(type='int', default=1), placement_type=dict(type='str', default='zone_host'), placement_enforcement=dict(type='str', default='strict'), placement_host=dict(type='str', default='datastore1'), recovery_type=dict(type='str', default='AUTO'), action_on_recovery=dict(type='str', default='REBOOT_ONLY'), interfaces=dict(type='list'), port_forwarding=dict(type='list'), config_data=dict(type='list'), tenant=dict(type='str', default='admin'), ) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict( changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, ) nfvis = nfvisModule(module) payload = None port = None response = {} # Get the list of existing deployments url_path = '/config/vm_lifecycle/tenants/tenant/{0}/deployments?deep'.format(nfvis.params['tenant']) response = nfvis.request(url_path, method='GET') # nfvis.result['current'] = response # Turn the list of dictionaries returned in the call into a dictionary of dictionaries hashed by the deployment name deployment_dict = {} try: for item in response['vmlc:deployments']['deployment']: name = item['name'] deployment_dict[name] = item except TypeError: pass except KeyError: pass if nfvis.params['state'] == 'present': if nfvis.params['name'] in deployment_dict: # The deployment exists on the device, so check to see if it is the same configuration nfvis.result['changed'] = False else: # The deployment does not exist on the device, so add it # Construct the payload payload = {'deployment': {}} payload['deployment']['name'] = nfvis.params['name'] payload['deployment']['vm_group'] = {} payload['deployment']['vm_group']['name'] = nfvis.params['name'] if nfvis.params['image']: payload['deployment']['vm_group']['image'] = nfvis.params['image'] else: module.fail_json(msg="image must be specified when state is present") if nfvis.params['flavor']: payload['deployment']['vm_group']['flavor'] = nfvis.params['flavor'] else: module.fail_json(msg="flavor must be specified when state is present") payload['deployment']['vm_group']['bootup_time'] = nfvis.params['bootup_time'] payload['deployment']['vm_group']['recovery_wait_time'] = nfvis.params['recovery_wait_time'] payload['deployment']['vm_group']['kpi_data'] = {} payload['deployment']['vm_group']['kpi_data']['enabled'] = nfvis.params['kpi_data'] payload['deployment']['vm_group']['scaling'] = {} payload['deployment']['vm_group']['scaling']['min_active'] = nfvis.params['scaling_min_active'] payload['deployment']['vm_group']['scaling']['max_active'] = nfvis.params['scaling_max_active'] payload['deployment']['vm_group']['scaling']['elastic'] = nfvis.params['scaling'] payload['deployment']['vm_group']['placement'] = {} payload['deployment']['vm_group']['placement']['type'] = nfvis.params['placement_type'] payload['deployment']['vm_group']['placement']['enforcement'] = nfvis.params['placement_enforcement'] payload['deployment']['vm_group']['placement']['host'] = nfvis.params['placement_host'] payload['deployment']['vm_group']['recovery_policy'] = {} payload['deployment']['vm_group']['recovery_policy']['recovery_type'] = nfvis.params['recovery_type'] payload['deployment']['vm_group']['recovery_policy']['action_on_recovery'] = nfvis.params['action_on_recovery'] port_forwarding = {} if nfvis.params['port_forwarding']: for item in nfvis.params['port_forwarding']: port_forwarding['port'] = {} port_forwarding['port']['type'] = item.get('type', 'ssh') port_forwarding['port']['vnf_port'] = item.get('vnf_port', 22) port_forwarding['port']['external_port_range'] = {} if 'proxy_port' in item: port_forwarding['port']['external_port_range']['start'] = item['proxy_port'] port_forwarding['port']['external_port_range']['end'] = item['proxy_port'] else: module.fail_json(msg="proxy_port must be specified for port_forwarding") port_forwarding['port']['protocol'] = item.get('protocol', 'tcp') port_forwarding['port']['source_bridge'] = item.get('source_bridge', 'MGMT') if nfvis.params['interfaces']: payload['deployment']['vm_group']['interfaces'] = [] for index, item in enumerate(nfvis.params['interfaces']): entry = {} entry['interface'] = {} entry['interface']['nicid'] = item.get('nicid', index) if 'network' in item: entry['interface']['network'] = item['network'] else: module.fail_json(msg="network must be specified for interface") if 'model' in item: entry['interface']['model'] = item['model'] if index == 0 and 'port' in port_forwarding: entry['interface']['port_forwarding'] = port_forwarding payload['deployment']['vm_group']['interfaces'].append(entry) if nfvis.params['config_data']: payload['deployment']['vm_group']['config_data'] = [] for item in nfvis.params['config_data']: entry = {'configuration': {}} if 'dst' in item: entry['configuration']['dst'] = item['dst'] else: module.fail_json(msg="dst must be specified for config_data") if 'data' in item: if isinstance(item['data'], str): entry['configuration']['data'] = item['data'] else: entry['configuration']['data'] = json.dumps(item['data']) else: module.fail_json(msg="data must be specified for config_data") payload['deployment']['vm_group']['config_data'].append(entry) if nfvis.params['kpi_data'] == True or nfvis.params['bootup_time'] > 0: payload['deployment']['vm_group']['kpi_data']['kpi'] = {} payload['deployment']['vm_group']['kpi_data']['kpi']['event_name'] = 'VM_ALIVE' payload['deployment']['vm_group']['kpi_data']['kpi']['metric_value'] = 1 payload['deployment']['vm_group']['kpi_data']['kpi']['metric_cond'] = 'GT' payload['deployment']['vm_group']['kpi_data']['kpi']['metric_type'] = 'UINT32' payload['deployment']['vm_group']['kpi_data']['kpi']['metric_collector'] = {} payload['deployment']['vm_group']['kpi_data']['kpi']['metric_collector']['type'] = 'ICMPPing' payload['deployment']['vm_group']['kpi_data']['kpi']['metric_collector']['nicid'] = 0 payload['deployment']['vm_group']['kpi_data']['kpi']['metric_collector']['poll_frequency'] = 3 payload['deployment']['vm_group']['kpi_data']['kpi']['metric_collector']['polling_unit'] = 'seconds' payload['deployment']['vm_group']['kpi_data']['kpi']['metric_collector']['continuous_alarm'] = False payload['deployment']['vm_group']['rules'] = {} payload['deployment']['vm_group']['rules']['admin_rules'] = {} payload['deployment']['vm_group']['rules']['admin_rules']['rule'] = {} payload['deployment']['vm_group']['rules']['admin_rules']['rule']['event_name'] = 'VM_ALIVE' payload['deployment']['vm_group']['rules']['admin_rules']['rule']['action'] = [ "ALWAYS log", "FALSE recover autohealing", "TRUE servicebooted.sh" ] nfvis.result['payload'] = payload url_path = '/config/vm_lifecycle/tenants/tenant/{0}/deployments'.format(nfvis.params['tenant']) if not module.check_mode: response = nfvis.request(url_path, method='POST', payload=json.dumps(payload)) nfvis.result['changed'] = True else: if nfvis.params['name'] in deployment_dict: url_path = '/config/vm_lifecycle/tenants/tenant/{0}/deployments/deployment/{1}'.format(nfvis.params['tenant'], nfvis.params['name']) if not module.check_mode: response = nfvis.request(url_path, 'DELETE') nfvis.result['changed'] = True # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications # FIXME: Work with nfvis so they can implement a check mode # if module.check_mode: # nfvis.exit_json(**nfvis.result) # execute checks for argument completeness # manipulate or modify the state as needed (this is going to be the # part where your module will do what it needs to do) # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results nfvis.exit_json(**nfvis.result)
def main(): # define the available arguments/parameters that a user can pass to # the module argument_spec = nfvis_argument_spec() # argument_spec.update(state=dict(type='str', choices=['absent', 'present'], default='present'), # vlan_id=dict(type='int', required=True)) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) nfvis = nfvisModule(module) payload = None # Get the platform details response = nfvis.request('/operational/platform-detail') if 'platform_info:platform-detail' in response: nfvis.result['platform-detail'] = response[ 'platform_info:platform-detail'] else: nfvis.result['platform-detail'] = [] # Get CPU allocation information response = nfvis.request('/operational/resources/cpu-info/allocation') if 'resources:allocation' in response: nfvis.result['cpu-info'] = response['resources:allocation'] else: nfvis.result['cpu-info'] = [] # Get deployment information response = nfvis.request( '/config/vm_lifecycle/tenants/tenant/admin/deployments?deep') if isinstance(response, dict) and 'vmlc:deployments' in response: nfvis.result['deployments'] = response['vmlc:deployments'] else: nfvis.result['deployments'] = [] # Get the bridge information response = nfvis.request('/config/bridges?deep') if 'network:bridges' in response: nfvis.result['bridges'] = response['network:bridges'] else: nfvis.result['bridges'] = [] # Get the network information response = nfvis.request('/config/networks?deep') if 'network:networks' in response: nfvis.result['networks'] = response['network:networks'] else: nfvis.result['networks'] = [] # Check Mode makes to sense with a facts module, just ignore if module.check_mode: nfvis.exit_json(**nfvis.result) nfvis.exit_json(**nfvis.result)
def main(): # define the available arguments/parameters that a user can pass to # the module argument_spec = nfvis_argument_spec() argument_spec.update(state=dict(type='str', choices=['absent', 'present'], default='present'), vlan_id=dict(type='int', required=True)) # seed the result dict in the object # we primarily care about changed and state # change is if this module effectively modified the target # state will include any data that you want your module to pass back # for consumption, for example, in a subsequent task result = dict(changed=False, ) # the AnsibleModule object will be our abstraction working with Ansible # this includes instantiation, a couple of common attr would be the # args/params passed to the execution, as well as if the module # supports check mode module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) nfvis = nfvisModule(module) payload = None nfvis.result['changed'] = False # Get the list of existing vlans url_path = '/running/switch/vlan?deep' response = nfvis.request(url_path, method='GET', operation='get_vlan') nfvis.result['current'] = response # Turn the list of dictionaries returned in the call into a dictionary of dictionaries hashed by the bridge name vlan_dict = {} try: for item in response['collection']['switch:vlan']: name = item['vlan-id'] vlan_dict[name] = item except TypeError: pass except KeyError: pass if nfvis.params['state'] == 'present': # Construct the payload payload = {'vlan': {}} payload['vlan']['vlan-id'] = nfvis.params['vlan_id'] if nfvis.params['vlan_id'] not in vlan_dict: # The vlan does not exist on the device, so add it url_path = '/running/switch' if not module.check_mode: response = nfvis.request(url_path, method='POST', payload=json.dumps(payload)) nfvis.result['changed'] = True else: if nfvis.params['name'] in vlan_dict: url = '/running/switch/vlan/{0}'.format(nfvis.params['vlan_id']) if not module.check_mode: response = nfvis.request(url, 'DELETE') nfvis.result['changed'] = True # if the user is working with this module in only check mode we do not # want to make any changes to the environment, just return the current # state with no modifications # FIXME: Work with nfvis so they can implement a check mode # if module.check_mode: # nfvis.exit_json(**nfvis.result) # execute checks for argument completeness # manipulate or modify the state as needed (this is going to be the # part where your module will do what it needs to do) # in the event of a successful module execution, you will want to # simple AnsibleModule.exit_json(), passing the key/value results nfvis.exit_json(**nfvis.result)