Esempio n. 1
0
def main():
    argument_spec = spec.openstack_argument_spec(
        size=dict(required=True),
        volume_type=dict(default=None),
        display_name=dict(required=True),
        display_description=dict(default=None),
        image_id=dict(default=None),
        image_name=dict(default=None),
        snapshot_id=dict(default=None),
    )
    module_kwargs = spec.openstack_module_kwargs(
        mutually_exclusive=[
            ['image_id', 'snapshot_id'],
            ['image_name', 'snapshot_id'],
            ['image_id', 'image_name']
        ],
    )
    module = AnsibleModule(argument_spec=argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)
        cinder = cloud.cinder_client
        if module.params['state'] == 'present':
            _present_volume(module, cinder, cloud)
        if module.params['state'] == 'absent':
            _absent_volume(module, cinder, cloud)
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
def main():

    argument_spec = spec.openstack_argument_spec(
        name=dict(default=None),
        id=dict(default=None),
        mounts=dict(default={}),
    )
    module_kwargs = spec.openstack_module_kwargs(
        mutually_exclusive=[
            ['name', 'id'],
        ],
        required_one_of=[
            ['name', 'id'],
        ],
    )
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)
        if module.params['id']:
            server = cloud.get_server_by_id(module.params['id'])
        else:
            server = cloud.get_server_by_name(module.params['name'])
        hostvars = dict(openstack=meta.get_hostvars_from_server(
            cloud, server, mounts=module.params['mounts']))
        module.exit_json(changed=False, ansible_facts=hostvars)

    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
Esempio n. 3
0
def main():

    argument_spec = openstack_argument_spec(
        name=dict(required=True),
        network_name=dict(required=True),
        cidr=dict(required=True),
        ip_version=dict(default='4', choices=['4', '6']),
        enable_dhcp=dict(default='true', type='bool'),
        gateway_ip=dict(default=None),
        dns_nameservers=dict(default=None),
        allocation_pool_start=dict(default=None),
        allocation_pool_end=dict(default=None),
    )
    module_kwargs = spec.openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)
        neutron = cloud.neutron_client
        if module.params['state'] == 'present':
            subnet_id = _get_subnet_id(module, neutron)
            if not subnet_id:
                subnet_id = _create_subnet(module, neutron)
                module.exit_json(changed=True, result="Created", id=subnet_id)
            else:
                module.exit_json(changed=False, result="success", id=subnet_id)
        else:
            subnet_id = _get_subnet_id(module, neutron)
            if not subnet_id:
                module.exit_json(changed=False, result="success")
            else:
                _delete_subnet(module, neutron, subnet_id)
                module.exit_json(changed=True, result="deleted")
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
Esempio n. 4
0
def main():
    argument_spec = openstack_argument_spec(
        name=dict(required=True),
        state=dict(default='present', choices=['absent', 'present']),
        admin_state_up=dict(type='bool', default=True),
    )
    module_kwargs = spec.openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)
        neutron = cloud.neutron_client

        if module.params['state'] == 'present':
            router_id = _get_router_id(module, neutron)
            if not router_id:
                router_id = _create_router(module, neutron)
                module.exit_json(changed=True, result="Created", id=router_id)
            else:
                module.exit_json(changed=False, result="success", id=router_id)

        else:
            router_id = _get_router_id(module, neutron)
            if not router_id:
                module.exit_json(changed=False, result="success")
            else:
                _delete_router(module, neutron, router_id)
                module.exit_json(changed=True, result="deleted")
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
Esempio n. 5
0
def main():

    argument_spec = spec.openstack_argument_spec(
        name=dict(required=True),
        disk_format=dict(
            default='qcow2',
            choices=['aki', 'vhd', 'vmdk', 'raw', 'qcow2', 'vdi', 'iso']
        ),
        container_format=dict(
            default='bare',
            choices=['aki', 'ari', 'bare', 'ovf']
        ),
        owner=dict(default=None),
        min_disk=dict(default=None),
        min_ram=dict(default=None),
        is_public=dict(default=True),
        copy_from=dict(default=None),
        file=dict(default=None),
    )
    module_kwargs = spec.openstack_module_kwargs(
        mutually_exclusive=[['file', 'copy_from']],
    )
    module = AnsibleModule(argument_spec, **module_kwargs)

    if module.params['state'] == 'present':
        if not module.params['file'] and not module.params['copy_from']:
            module.fail_json(msg="Either file or copy_from variable should "
                                 "be set to create the image")

    try:
        cloud = shade.openstack_cloud(**module.params)

        id = cloud.get_image_id(module.params['name'])

        if module.params['state'] == 'present':
            if not id:
                _glance_image_create(
                    module,
                    module.params,
                    cloud.glance_client
                )
            module.exit_json(changed=False, id=id, result="success")

        if module.params['state'] == 'absent':
            if not id:
                module.exit_json(changed=False, result="Success")
            else:
                _glance_delete_image(
                    module,
                    module.params,
                    cloud.glance_client
                )
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
Esempio n. 6
0
def main():

    argument_spec = spec.openstack_argument_spec(
        name=dict(required=True),
        tenant_name=dict(default=None),
        provider_network_type=dict(
            default=None,
            choices=['local', 'vlan', 'flat', 'gre']
        ),
        provider_physical_network=dict(default=None),
        provider_segmentation_id=dict(default=None),
        router_external=dict(default=False, type='bool'),
        shared=dict(default=False, type='bool'),
        admin_state_up=dict(default=True, type='bool'),
    )
    module_kwargs = spec.openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    if module.params['provider_network_type'] in ['vlan', 'flat']:
        if not module.params['provider_physical_network']:
            module.fail_json(msg=" for vlan and flat networks, variable "
                                 "provider_physical_network should be set.")

    if module.params['provider_network_type'] in ['vlan', 'gre']:
            if not module.params['provider_segmentation_id']:
                module.fail_json(msg=" for vlan & gre networks, variable "
                                     "provider_segmentation_id should "
                                     "be set.")

    try:
        cloud = shade.openstack_cloud(**module.params)
        neutron = cloud.neutron_client

        _set_tenant_id(module)

        if module.params['state'] == 'present':
            network_id = _get_net_id(neutron, module)
            if not network_id:
                network_id = _create_network(module, neutron)
                module.exit_json(changed=True, result="Created", id=network_id)
            else:
                module.exit_json(changed=False, result="Success",
                                 id=network_id)

        if module.params['state'] == 'absent':
            network_id = _get_net_id(neutron, module)
            if not network_id:
                module.exit_json(changed=False, result="Success")
            else:
                _delete_network(module, network_id, neutron)
                module.exit_json(changed=True, result="Deleted")
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
Esempio n. 7
0
def main():

    argument_spec = spec.openstack_argument_spec(
        name=dict(required=True),
        image_id=dict(default=None),
        image_name=dict(default=None),
        image_exclude=dict(default='(deprecated)'),
        flavor_id=dict(default=None),
        flavor_ram=dict(default=None, type='int'),
        flavor_include=dict(default=None),
        key_name=dict(default=None),
        security_groups=dict(default='default'),
        nics=dict(default=None),
        meta=dict(default=None),
        userdata=dict(default=None),
        config_drive=dict(default=False, type='bool'),
        auto_floating_ip=dict(default=True, type='bool'),
        floating_ips=dict(default=None),
        floating_ip_pools=dict(default=None),
    )
    module_kwargs = spec.openstack_module_kwargs(mutually_exclusive=[
        ['auto_floating_ip', 'floating_ips'],
        ['auto_floating_ip', 'floating_ip_pools'],
        ['floating_ips', 'floating_ip_pools'],
        ['image_id', 'image_name'],
        ['flavor_id', 'flavor_ram'],
    ], )
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)

        if module.params['state'] == 'present':
            if (not module.params['image_id']
                    and not module.params['image_name']):
                module.fail_json(msg="Parameter 'image_id' or `image_name`"
                                 " is required if state == 'present'")
            else:
                _get_server_state(module, cloud)
                _create_server(module, cloud)
        if module.params['state'] == 'absent':
            _get_server_state(module, cloud)
            _delete_server(module, cloud)
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
def main():

    argument_spec = spec.openstack_argument_spec(
        network_name=dict(required=True),
        instance_name=dict(required=True),
        internal_network_name=dict(default=None),
    )
    module_kwargs = spec.openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)
        nova = cloud.nova_client
        neutron = cloud.neutron_client

        server_info, server_obj = _get_server_state(module, nova)
        if not server_info:
            module.fail_json(msg="The instance name provided cannot be found")

        fixed_ip, port_id = _get_port_info(
            neutron, module, server_info['id'],
            module.params['internal_network_name'])
        if not port_id:
            module.fail_json(
                msg="Cannot find a port for this instance,"
                    " maybe fixed ip is not assigned")

        floating_id, floating_ip = _get_floating_ip(module, neutron, fixed_ip)

        if module.params['state'] == 'present':
            if floating_ip:
                module.exit_json(changed=False, public_ip=floating_ip)
            net_id = _get_net_id(neutron, module)
            if not net_id:
                module.fail_json(msg="cannot find the network specified,"
                                     " please check")
            _create_floating_ip(neutron, module, port_id, net_id, fixed_ip)

        if module.params['state'] == 'absent':
            if floating_ip:
                _update_floating_ip(neutron, module, None, floating_id)
            module.exit_json(changed=False)
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
Esempio n. 9
0
def main():
    argument_spec = spec.openstack_argument_spec(
        uuid=dict(required=True),
        driver=dict(required=True),
        driver_info=dict(type='dict', required=True),
        nics=dict(type='list', required=True),
        properties=dict(type='dict', default={}),
    )
    module_kwargs = spec.openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.operator_cloud(**module.params)
        server = cloud.get_machine_by_uuid(module.params['uuid'])

        if module.params['state'] == 'present':
            properties = _parse_properties(module)
            driver_info = _parse_driver_info(module)
            kwargs = dict(
                uuid=module.params['uuid'],
                driver=module.params['driver'],
                properties=properties,
                driver_info=driver_info,
            )
            if server is None:
                server = cloud.register_machine(module.params['nics'],
                                                **kwargs)
                module.exit_json(changed=True, uuid=server.uuid)
            else:
                # TODO: compare properties here and update if necessary
                #       ... but the interface for that is terrible!
                module.exit_json(changed=False,
                                 result="Server already present")
        if module.params['state'] == 'absent':
            if server is not None:
                cloud.unregister_machine(module.params['nics'],
                                         module.params['uuid'])
                module.exit_json(changed=True, result="deleted")
            else:
                module.exit_json(changed=False, result="Server not found")
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
Esempio n. 10
0
def main():
    argument_spec = spec.openstack_argument_spec(
        name=dict(required=True),
        public_key=dict(default=None),
    )
    module_kwargs = spec.openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        nova = shade.openstack_cloud(**module.params)

        if module.params['state'] == 'present':
            for key in nova.list_keypairs():
                if key.name == module.params['name']:
                    if (module.params['public_key'] and
                        (module.params['public_key'] != key.public_key)):
                        module.fail_json(
                            msg="name {} present but key hash not "
                            "the same as offered. "
                            "Delete key first.".format(key['name']))
                    else:
                        module.exit_json(changed=False, result="Key present")
            try:
                key = nova.create_keypair(module.params['name'],
                                          module.params['public_key'])
            except Exception, e:
                module.exit_json(msg="Error in creating the "
                                 "keypair: %s" % e.message)
            if not module.params['public_key']:
                module.exit_json(changed=True, key=key.private_key)
            module.exit_json(changed=True, key=None)
        if module.params['state'] == 'absent':
            for key in nova.list_keypairs():
                if key.name == module.params['name']:
                    try:
                        nova.delete_keypair(module.params['name'])
                    except Exception, e:
                        module.fail_json(msg="The keypair deletion has "
                                         "failed: %s" % e.message)
                    module.exit_json(changed=True, result="deleted")
            module.exit_json(changed=False, result="not present")
def main():
    argument_spec = spec.openstack_argument_spec(
        server_id=dict(default=None),
        server_name=dict(default=None),
        volume_id=dict(default=None),
        volume_name=dict(default=None),
        device=dict(default=None),
    )
    module_kwargs = spec.openstack_module_kwargs(
        mutually_exclusive=[
            ['server_id', 'server_name'],
            ['volume_id', 'volume_name'],
        ],
        required_one_of=[
            ['server_id', 'server_name'],
            ['volume_id', 'volume_name'],
        ],
    )

    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)
        cinder = cloud.cinder_client
        nova = cloud.nova_client

        if module.params['volume_name'] is not None:
            module.params['volume_id'] = cloud.get_volume_id(
                module.params['volume_name'])

        if module.params['server_name'] is not None:
            module.params['server_id'] = cloud.get_server_id(
                module.params['server_name'])

        if module.params['state'] == 'present':
            _present_volume(cloud, nova, cinder, module)
        if module.params['state'] == 'absent':
            _absent_volume(nova, cinder, module)

    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
Esempio n. 12
0
def main():

    argument_spec = spec.openstack_argument_spec(
        ip_address=dict(required=True),
        instance_name=dict(required=True),
    )
    module_kwargs = spec.openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)
        nova = cloud.nova_client
        neutron = cloud.neutron_client

        state, floating_ip_id = _get_floating_ip_id(module, neutron)
        if module.params['state'] == 'present':
            if state == 'attached':
                module.exit_json(
                    changed=False,
                    result='attached',
                    public_ip=module.params['ip_address']
                )
            server_info, server_obj = _get_server_state(module, nova)
            if not server_info:
                module.fail_json(msg="The instance name provided"
                                     " cannot be found")
            port_id = _get_port_id(neutron, module, server_info['id'])
            if not port_id:
                module.fail_json(msg="Cannot find a port for this instance,"
                                     " maybe fixed ip is not assigned")
            _update_floating_ip(neutron, module, port_id, floating_ip_id)

        if module.params['state'] == 'absent':
            if state == 'detached':
                module.exit_json(changed=False, result='detached')
            if state == 'attached':
                _update_floating_ip(neutron, module, None, floating_ip_id)
            module.exit_json(changed=True, result="detached")
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
def main():

    argument_spec = openstack_argument_spec(
        router_name=dict(required=True),
        network_name=dict(required=True),
    )
    module_kwargs = spec.openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)
        neutron = cloud.neutron_client

        router_id = _get_router_id(module, neutron)

        if not router_id:
            module.fail_json(msg="failed to get the router id, please check "
                             "the router name")

        network_id = _get_net_id(neutron, module)
        if not network_id:
            module.fail_json(msg="failed to get the network id, please check "
                             "the network name and make sure it is "
                             "external")

        if module.params['state'] == 'present':
            port_id = _get_port_id(neutron, module, router_id, network_id)
            if not port_id:
                _add_gateway_router(neutron, module, router_id, network_id)
                module.exit_json(changed=True, result="created")
            module.exit_json(changed=False, result="success")

        if module.params['state'] == 'absent':
            port_id = _get_port_id(neutron, module, router_id, network_id)
            if not port_id:
                module.exit_json(changed=False, result="Success")
            _remove_gateway_router(neutron, module, router_id)
            module.exit_json(changed=True, result="Deleted")
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)
Esempio n. 14
0
def main():
    argument_spec = openstack_argument_spec(
        router_name=dict(required=True),
        subnet_name=dict(required=True),
    )
    module_kwargs = spec.openstack_module_kwargs()
    module = AnsibleModule(argument_spec, **module_kwargs)

    try:
        cloud = shade.openstack_cloud(**module.params)
        neutron = cloud.neutron_client

        router_id = _get_router_id(module, neutron)
        if not router_id:
            module.fail_json(msg="failed to get the router id, "
                             "please check the router name")

        subnet_id = _get_subnet_id(module, neutron)
        if not subnet_id:
            module.fail_json(msg="failed to get the subnet id, "
                             "please check the subnet name")

        if module.params['state'] == 'present':
            port_id = _get_port_id(neutron, module, router_id, subnet_id)
            if not port_id:
                _add_interface_router(neutron, module, router_id, subnet_id)
                module.exit_json(changed=True, result="created", id=port_id)
            module.exit_json(changed=False, result="success", id=port_id)

        if module.params['state'] == 'absent':
            port_id = _get_port_id(neutron, module, router_id, subnet_id)
            if not port_id:
                module.exit_json(changed=False, result="Success")
            _remove_interface_router(neutron, module, router_id, subnet_id)
            module.exit_json(changed=True, result="Deleted")
    except shade.OpenStackCloudException as e:
        module.fail_json(msg=e.message)