示例#1
0
def run_module():

    module = AnsibleModule(
        argument_spec=yaml.safe_load(DOCUMENTATION)['options'],
        supports_check_mode=True,
        required_if=[['state', 'present', ['data', 'metadata']]],
    )

    # Gather module parameters in variables
    name = module.params.get('name')
    state = module.params.get('state')
    max_mds = module.params.get('max_mds')

    if module.check_mode:
        module.exit_json(
            changed=False,
            stdout='',
            stderr='',
            rc=0,
            start='',
            end='',
            delta='',
        )

    startd = datetime.datetime.now()
    changed = False

    # will return either the image name or None
    container_image = is_containerized()

    if state == "present":
        rc, cmd, out, err = exec_command(module, get_fs(module, container_image=container_image))
        if rc == 0:
            fs = json.loads(out)
            if max_mds and fs["mdsmap"]["max_mds"] != max_mds:
                rc, cmd, out, err = exec_command(module, set_fs(module, container_image=container_image))
                if rc == 0:
                    changed = True
        else:
            rc, cmd, out, err = exec_command(module, create_fs(module, container_image=container_image))
            if max_mds and max_mds > 1:
                exec_command(module, set_fs(module, container_image=container_image))
            if rc == 0:
                changed = True

    elif state == "absent":
        rc, cmd, out, err = exec_command(module, get_fs(module, container_image=container_image))
        if rc == 0:
            exec_command(module, fail_fs(module, container_image=container_image))
            rc, cmd, out, err = exec_command(module, remove_fs(module, container_image=container_image))
            if rc == 0:
                changed = True
        else:
            rc = 0
            out = "Ceph File System {} doesn't exist".format(name)

    elif state == "info":
        rc, cmd, out, err = exec_command(module, get_fs(module, container_image=container_image))

    exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed)
示例#2
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str', required=True),
            cluster=dict(type='str', required=False, default='ceph'),
            style=dict(type='str', required=False, default='legacy'),
            image=dict(type='str', required=False),
            docker=dict(type='bool', required=False, default=False),
            pull=dict(type='bool', required=False, default=True),
            firewalld=dict(type='bool', required=False, default=True),
        ),
        supports_check_mode=True,
    )

    name = module.params.get('name')
    cluster = module.params.get('cluster')
    style = module.params.get('style')
    docker = module.params.get('docker')
    image = module.params.get('image')
    pull = module.params.get('pull')
    firewalld = module.params.get('firewalld')

    startd = datetime.datetime.now()

    cmd = ['cephadm']

    if docker:
        cmd.append('--docker')

    if image:
        cmd.extend(['--image', image])

    cmd.extend(
        ['adopt', '--cluster', cluster, '--name', name, '--style', style])

    if not pull:
        cmd.append('--skip-pull')

    if not firewalld:
        cmd.append('--skip-firewalld')

    if module.check_mode:
        exit_module(module=module,
                    out='',
                    rc=0,
                    cmd=cmd,
                    err='',
                    startd=startd,
                    changed=False)
    else:
        rc, out, err = module.run_command(cmd)
        exit_module(module=module,
                    out=out,
                    rc=rc,
                    cmd=cmd,
                    err=err,
                    startd=startd,
                    changed=True)
示例#3
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            ids=dict(type='list', required=True),
            cluster=dict(type='str', required=False, default='ceph'),
            state=dict(type='str',
                       required=True,
                       choices=['destroy', 'down', 'in', 'out', 'purge',
                                'rm']),  # noqa: E501
        ),
        supports_check_mode=True,
    )

    ids = module.params.get('ids')
    cluster = module.params.get('cluster')
    state = module.params.get('state')

    if state in ['destroy', 'purge'] and len(ids) > 1:
        module.fail_json(
            msg='destroy and purge only support one OSD at at time',
            rc=1)  # noqa: E501

    startd = datetime.datetime.now()

    container_image = is_containerized()

    cmd = generate_ceph_cmd(['osd', state],
                            ids,
                            cluster=cluster,
                            container_image=container_image)  # noqa: E501

    if state in ['destroy', 'purge']:
        cmd.append('--yes-i-really-mean-it')

    if module.check_mode:
        exit_module(module=module,
                    out='',
                    rc=0,
                    cmd=cmd,
                    err='',
                    startd=startd,
                    changed=False)
    else:
        rc, out, err = module.run_command(cmd)
        changed = True
        if state in ['down', 'in', 'out'] and 'marked' not in err:
            changed = False
        exit_module(module=module,
                    out=out,
                    rc=rc,
                    cmd=cmd,
                    err=err,
                    startd=startd,
                    changed=changed)
示例#4
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str',
                      required=True,
                      choices=[
                          'noup', 'nodown', 'noout', 'nobackfill',
                          'norebalance', 'norecover', 'noscrub', 'nodeep-scrub'
                      ]),
            cluster=dict(type='str', required=False, default='ceph'),
            state=dict(type='str',
                       required=False,
                       default='present',
                       choices=['present', 'absent']),
        ),
        supports_check_mode=True,
    )

    name = module.params.get('name')
    cluster = module.params.get('cluster')
    state = module.params.get('state')

    startd = datetime.datetime.now()

    container_image = is_containerized()

    if state == 'present':
        cmd = generate_ceph_cmd(['osd', 'set'], [name],
                                cluster=cluster,
                                container_image=container_image)
    else:
        cmd = generate_ceph_cmd(['osd', 'unset'], [name],
                                cluster=cluster,
                                container_image=container_image)

    if module.check_mode:
        exit_module(module=module,
                    out='',
                    rc=0,
                    cmd=cmd,
                    err='',
                    startd=startd,
                    changed=False)
    else:
        rc, out, err = module.run_command(cmd)
        exit_module(module=module,
                    out=out,
                    rc=rc,
                    cmd=cmd,
                    err=err,
                    startd=startd,
                    changed=True)
示例#5
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str', required=True),
            cluster=dict(type='str', required=False, default='ceph'),
            state=dict(type='str',
                       required=False,
                       default='enable',
                       choices=['enable', 'disable']),  # noqa: E501
        ),
        supports_check_mode=True,
    )

    name = module.params.get('name')
    cluster = module.params.get('cluster')
    state = module.params.get('state')

    startd = datetime.datetime.now()

    container_image = is_containerized()

    cmd = generate_ceph_cmd(['mgr', 'module'], [state, name],
                            cluster=cluster,
                            container_image=container_image)

    if module.check_mode:
        exit_module(module=module,
                    out='',
                    rc=0,
                    cmd=cmd,
                    err='',
                    startd=startd,
                    changed=False)
    else:
        rc, out, err = module.run_command(cmd)
        if 'is already enabled' in err:
            changed = False
        else:
            changed = True
        exit_module(module=module,
                    out=out,
                    rc=rc,
                    cmd=cmd,
                    err=err,
                    startd=startd,
                    changed=changed)
示例#6
0
def run_module():

    module = AnsibleModule(
        argument_spec=yaml.safe_load(DOCUMENTATION)['options'],
        supports_check_mode=True,
        required_if=[['apply', True, ['render_path']]],
    )

    # Gather module parameters in variables
    cluster = module.params.get('cluster')
    service_type = module.params.get('service_type')
    service_id = module.params.get('service_type')
    service_name = module.params.get('service_name')
    hosts = module.params.get('hosts')
    host_pattern = module.params.get('host_pattern')
    networks = module.params.get('networks')
    labels = module.params.get('labels')
    spec = module.params.get('spec')
    extra = module.params.get('extra')
    apply = module.params.get('apply')
    render_path = module.params.get('render_path')

    if module.check_mode:
        module.exit_json(
            changed=False,
            stdout='',
            stderr='',
            rc=0,
            start='',
            end='',
            delta='',
        )

    startd = datetime.datetime.now()
    changed = False

    # PROCESSING PARAMETERS
    if service_id is None:
        service_id = service_type
    if service_name is None:
        service_name = "{}.{}".format(service_type, service_id)

    # no spec is provided
    if spec is None:
        spec = {}

    # no spec is provided
    if extra is None:
        extra = {}

    # no labels are defined
    if labels is None:
        labels = []

    # no networks are defined
    if networks is None:
        networks = []

    d = ceph_spec.CephDaemonSpec(service_type, service_id, service_name,
                                 hosts, host_pattern, networks, spec, labels, **extra)

    if apply:
        container_image = is_containerized()
        render('{}/{}'.format(render_path, service_type), d.make_daemon_spec())
        cmd = generate_orch_cli(cluster, '{}/{}'.format(render_path, service_type), container_image)
        rc, cmd, out, err = exec_command(module, cmd)
        exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed)
    else:
        # render the dict as the output of the module
        module.exit_json(changed=True, result=d.make_daemon_spec())
示例#7
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            mon_ip=dict(type='str', required=True),
            image=dict(type='str', required=False),
            docker=dict(type='bool', required=False, default=False),
            fsid=dict(type='str', required=False),
            pull=dict(type='bool', required=False, default=True),
            dashboard=dict(type='bool', required=False, default=True),
            dashboard_user=dict(type='str', required=False),
            dashboard_password=dict(type='str', required=False, no_log=True),
            monitoring=dict(type='bool', required=False, default=True),
            firewalld=dict(type='bool', required=False, default=True),
            registry_url=dict(type='str', require=False),
            registry_username=dict(type='str', require=False),
            registry_password=dict(type='str', require=False, no_log=True),
            registry_json=dict(type='path', require=False),
        ),
        supports_check_mode=True,
        mutually_exclusive=[
            ('registry_json', 'registry_url'),
            ('registry_json', 'registry_username'),
            ('registry_json', 'registry_password'),
        ],
        required_together=[
            ('registry_url', 'registry_username', 'registry_password')
        ],
    )

    mon_ip = module.params.get('mon_ip')
    docker = module.params.get('docker')
    image = module.params.get('image')
    fsid = module.params.get('fsid')
    pull = module.params.get('pull')
    dashboard = module.params.get('dashboard')
    dashboard_user = module.params.get('dashboard_user')
    dashboard_password = module.params.get('dashboard_password')
    monitoring = module.params.get('monitoring')
    firewalld = module.params.get('firewalld')
    registry_url = module.params.get('registry_url')
    registry_username = module.params.get('registry_username')
    registry_password = module.params.get('registry_password')
    registry_json = module.params.get('registry_json')

    startd = datetime.datetime.now()

    cmd = ['cephadm']

    if docker:
        cmd.append('--docker')

    if image:
        cmd.extend(['--image', image])

    cmd.extend(['bootstrap', '--mon-ip', mon_ip])

    if fsid:
        cmd.extend(['--fsid', fsid])

    if not pull:
        cmd.append('--skip-pull')

    if dashboard:
        if dashboard_user:
            cmd.extend(['--initial-dashboard-user', dashboard_user])
        if dashboard_password:
            cmd.extend(['--initial-dashboard-password', dashboard_password])
    else:
        cmd.append('--skip-dashboard')

    if not monitoring:
        cmd.append('--skip-monitoring-stack')

    if not firewalld:
        cmd.append('--skip-firewalld')

    if registry_url and registry_username and registry_password:
        cmd.extend(['--registry-url', registry_url,
                    '--registry-username', registry_username,
                    '--registry-password', registry_password])

    if registry_json:
        cmd.extend(['--registry-json', registry_json])

    if module.check_mode:
        exit_module(
            module=module,
            out='',
            rc=0,
            cmd=cmd,
            err='',
            startd=startd,
            changed=False
        )
    else:
        rc, out, err = module.run_command(cmd)
        exit_module(
            module=module,
            out=out,
            rc=rc,
            cmd=cmd,
            err=err,
            startd=startd,
            changed=True
        )
def run_module():
    module_args = dict(
        cluster=dict(type='str', required=False, default='ceph'),
        name=dict(type='str', required=True),
        state=dict(type='str', required=False, choices=['present', 'absent', 'info'], default='present'),
        password=dict(type='str', required=False, no_log=True),
        roles=dict(type='list',
                   required=False,
                   choices=['administrator', 'read-only', 'block-manager', 'rgw-manager', 'cluster-manager', 'pool-manager', 'cephfs-manager'],
                   default=[]),
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True,
        required_if=[['state', 'present', ['password']]]
    )

    # Gather module parameters in variables
    name = module.params.get('name')
    state = module.params.get('state')
    roles = module.params.get('roles')
    password = module.params.get('password')

    if module.check_mode:
        module.exit_json(
            changed=False,
            stdout='',
            stderr='',
            rc=0,
            start='',
            end='',
            delta='',
        )

    startd = datetime.datetime.now()
    changed = False

    # will return either the image name or None
    container_image = is_containerized()

    if state == "present":
        rc, cmd, out, err = exec_command(module, get_user(module, container_image=container_image))
        if rc == 0:
            user = json.loads(out)
            user['roles'].sort()
            roles.sort()
            if user['roles'] != roles:
                rc, cmd, out, err = exec_command(module, set_roles(module, container_image=container_image))
                changed = True
            rc, cmd, out, err = exec_command(module, set_password(module, container_image=container_image), stdin=password)
        else:
            rc, cmd, out, err = exec_command(module, create_user(module, container_image=container_image), stdin=password)
            rc, cmd, out, err = exec_command(module, set_roles(module, container_image=container_image))
            changed = True

    elif state == "absent":
        rc, cmd, out, err = exec_command(module, get_user(module, container_image=container_image))
        if rc == 0:
            rc, cmd, out, err = exec_command(module, remove_user(module, container_image=container_image))
            changed = True
        else:
            rc = 0
            out = "Dashboard User {} doesn't exist".format(name)

    elif state == "info":
        rc, cmd, out, err = exec_command(module, get_user(module, container_image=container_image))

    exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed)
示例#9
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str', required=True),
            cluster=dict(type='str', required=False, default='ceph'),
            style=dict(type='str', required=False, default='legacy'),
            image=dict(type='str', required=False),
            docker=dict(type='bool', required=False, default=False),
            pull=dict(type='bool', required=False, default=True),
            firewalld=dict(type='bool', required=False, default=True),
        ),
        supports_check_mode=True,
    )

    name = module.params.get('name')
    cluster = module.params.get('cluster')
    style = module.params.get('style')
    docker = module.params.get('docker')
    image = module.params.get('image')
    pull = module.params.get('pull')
    firewalld = module.params.get('firewalld')

    startd = datetime.datetime.now()

    cmd = ['cephadm', 'ls', '--no-detail']

    if module.check_mode:
        exit_module(
            module=module,
            out='',
            rc=0,
            cmd=cmd,
            err='',
            startd=startd,
            changed=False
        )
    else:
        rc, out, err = module.run_command(cmd)

    if rc == 0:
        if name in [x["name"] for x in json.loads(out) if x["style"] == "cephadm:v1"]:
            exit_module(
                module=module,
                out='{} is already adopted'.format(name),
                rc=0,
                cmd=cmd,
                err='',
                startd=startd,
                changed=False
            )
    else:
        module.fail_json(msg=err, rc=rc)

    cmd = ['cephadm']

    if docker:
        cmd.append('--docker')

    if image:
        cmd.extend(['--image', image])

    cmd.extend(['adopt', '--cluster', cluster, '--name', name, '--style', style])

    if not pull:
        cmd.append('--skip-pull')

    if not firewalld:
        cmd.append('--skip-firewalld')

    rc, out, err = module.run_command(cmd)
    exit_module(
        module=module,
        out=out,
        rc=rc,
        cmd=cmd,
        err=err,
        startd=startd,
        changed=True
    )
示例#10
0
def main():
    module = AnsibleModule(argument_spec=dict(
        name=dict(type='str', required=True),
        cluster=dict(type='str', required=False, default='ceph'),
        state=dict(type='str',
                   required=False,
                   choices=['present', 'absent', 'info'],
                   default='present'),
        rule_type=dict(type='str',
                       required=False,
                       choices=['replicated', 'erasure']),
        bucket_root=dict(type='str', required=False),
        bucket_type=dict(type='str',
                         required=False,
                         choices=[
                             'osd', 'host', 'chassis', 'rack', 'row', 'pdu',
                             'pod', 'room', 'datacenter', 'zone', 'region',
                             'root'
                         ]),
        device_class=dict(type='str', required=False),
        profile=dict(type='str', required=False)),
                           supports_check_mode=True,
                           required_if=[('state', 'present', ['rule_type']),
                                        ('rule_type', 'replicated',
                                         ['bucket_root', 'bucket_type']),
                                        ('rule_type', 'erasure', ['profile'])])

    # Gather module parameters in variables
    name = module.params.get('name')
    state = module.params.get('state')
    rule_type = module.params.get('rule_type')

    if module.check_mode:
        module.exit_json(
            changed=False,
            stdout='',
            stderr='',
            rc=0,
            start='',
            end='',
            delta='',
        )

    startd = datetime.datetime.now()
    changed = False

    # will return either the image name or None
    container_image = is_containerized()

    if state == "present":
        rc, cmd, out, err = exec_command(
            module, get_rule(module, container_image=container_image))
        if rc != 0:
            rc, cmd, out, err = exec_command(
                module, create_rule(module, container_image=container_image))
            changed = True
        else:
            rule = json.loads(out)
            if (rule['type'] == 1 and rule_type == 'erasure') or (
                    rule['type'] == 3 and rule_type == 'replicated'):
                module.fail_json(
                    msg="Can not convert crush rule {} to {}".format(
                        name, rule_type),
                    changed=False,
                    rc=1)

    elif state == "absent":
        rc, cmd, out, err = exec_command(
            module, get_rule(module, container_image=container_image))
        if rc == 0:
            rc, cmd, out, err = exec_command(
                module, remove_rule(module, container_image=container_image))
            changed = True
        else:
            rc = 0
            out = "Crush Rule {} doesn't exist".format(name)

    elif state == "info":
        rc, cmd, out, err = exec_command(
            module, get_rule(module, container_image=container_image))

    exit_module(module=module,
                out=out,
                rc=rc,
                cmd=cmd,
                err=err,
                startd=startd,
                changed=changed)
示例#11
0
def run_module():
    module_args = dict(
        cluster=dict(type='str', required=False, default='ceph'),
        name=dict(type='str', required=True),
        state=dict(type='str',
                   required=False,
                   choices=['present', 'absent'],
                   default='present'),
        stripe_unit=dict(type='str', required=False),
        k=dict(type='str', required=False),
        m=dict(type='str', required=False),
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True,
        required_if=[['state', 'present', ['k', 'm']]],
    )

    # Gather module parameters in variables
    name = module.params.get('name')
    cluster = module.params.get('cluster')
    state = module.params.get('state')
    stripe_unit = module.params.get('stripe_unit')
    k = module.params.get('k')
    m = module.params.get('m')

    if module.check_mode:
        module.exit_json(
            changed=False,
            stdout='',
            stderr='',
            rc=0,
            start='',
            end='',
            delta='',
        )

    startd = datetime.datetime.now()
    changed = False

    # will return either the image name or None
    container_image = is_containerized()

    if state == "present":
        rc, cmd, out, err = exec_command(
            module,
            get_profile(module, name, cluster,
                        container_image=container_image))  # noqa: E501
        if rc == 0:
            # the profile already exists, let's check whether we have to
            # update it
            current_profile = json.loads(out)
            if current_profile['k'] != k or \
               current_profile['m'] != m or \
               current_profile.get('stripe_unit', stripe_unit) != stripe_unit:
                rc, cmd, out, err = exec_command(
                    module,
                    create_profile(
                        module,
                        name,
                        k,
                        m,
                        stripe_unit,
                        cluster,
                        force=True,
                        container_image=container_image))  # noqa: E501
                changed = True
        else:
            # the profile doesn't exist, it has to be created
            rc, cmd, out, err = exec_command(
                module,
                create_profile(
                    module,
                    name,
                    k,
                    m,
                    stripe_unit,  # noqa: E501
                    cluster,
                    container_image=container_image))  # noqa: E501
            if rc == 0:
                changed = True

    elif state == "absent":
        rc, cmd, out, err = exec_command(
            module,
            delete_profile(module,
                           name,
                           cluster,
                           container_image=container_image))  # noqa: E501
        if not err:
            out = 'Profile {} removed.'.format(name)
            changed = True
        else:
            rc = 0
            out = "Skipping, the profile {} doesn't exist".format(name)

    exit_module(module=module,
                out=out,
                rc=rc,
                cmd=cmd,
                err=err,
                startd=startd,
                changed=changed)  # noqa: E501
示例#12
0
def run_module():
    module = AnsibleModule(
        argument_spec=yaml.safe_load(DOCUMENTATION)['options'],
        supports_check_mode=True,
        required_if=[['state', 'present', ['password']]])

    # Gather module parameters in variables
    name = module.params.get('name')
    state = module.params.get('state')
    roles = module.params.get('roles')
    password = module.params.get('password')

    if module.check_mode:
        module.exit_json(
            changed=False,
            stdout='',
            stderr='',
            rc=0,
            start='',
            end='',
            delta='',
        )

    startd = datetime.datetime.now()
    changed = False

    # will return either the image name or None
    container_image = is_containerized()

    if state == "present":
        rc, cmd, out, err = exec_command(
            module, get_user(module, container_image=container_image))
        if rc == 0:
            user = json.loads(out)
            user['roles'].sort()
            roles.sort()
            if user['roles'] != roles:
                rc, cmd, out, err = exec_command(
                    module, set_roles(module, container_image=container_image))
                changed = True
            rc, cmd, out, err = exec_command(
                module,
                set_password(module, container_image=container_image),
                stdin=password)
        else:
            rc, cmd, out, err = exec_command(
                module,
                create_user(module, container_image=container_image),
                stdin=password)
            rc, cmd, out, err = exec_command(
                module, set_roles(module, container_image=container_image))
            changed = True

    elif state == "absent":
        rc, cmd, out, err = exec_command(
            module, get_user(module, container_image=container_image))
        if rc == 0:
            rc, cmd, out, err = exec_command(
                module, remove_user(module, container_image=container_image))
            changed = True
        else:
            rc = 0
            out = "Dashboard User {} doesn't exist".format(name)

    elif state == "info":
        rc, cmd, out, err = exec_command(
            module, get_user(module, container_image=container_image))

    exit_module(module=module,
                out=out,
                rc=rc,
                cmd=cmd,
                err=err,
                startd=startd,
                changed=changed)
示例#13
0
def run_module():
    module_args = dict(
        cluster=dict(type="str", required=False, default="ceph"),
        name=dict(type="str", required=True),
        state=dict(type="str",
                   required=False,
                   choices=["present", "absent"],
                   default="present"),
        caps=dict(type="list", required=True),
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True,
    )

    # Gather module parameters in variables
    name = module.params.get("name")
    state = module.params.get("state")
    caps = module.params.get("caps")

    startd = datetime.datetime.now()
    changed = False

    # will return either the image name or None
    container_image = is_containerized()

    diff = dict(before="", after="")

    # get user infos for diff
    rc, cmd, out, err = exec_command(
        module, get_user(module, container_image=container_image))

    if rc == 0:
        before_user = json.loads(out)
        before_caps = sorted(before_user["caps"], key=lambda d: d["type"])
        diff["before"] = json.dumps(before_caps, indent=4)

        out = ""
        err = ""

        if state == "present":
            cmd = add_caps(module, container_image=container_image)
        elif state == "absent":
            cmd = remove_caps(module, container_image=container_image)

        if not module.check_mode:
            rc, cmd, out, err = exec_command(module, cmd)
        else:
            out_caps = params_to_caps_output(before_user["caps"],
                                             caps,
                                             deletion=(state == "absent"))
            out = json.dumps(dict(caps=out_caps))

        if rc == 0:
            after_user = json.loads(out)["caps"]
            after_user = sorted(after_user, key=lambda d: d["type"])
            diff["after"] = json.dumps(after_user, indent=4)
            changed = diff["before"] != diff["after"]
    else:
        out = "User {} doesn't exist".format(name)

    exit_module(
        module=module,
        out=out,
        rc=rc,
        cmd=cmd,
        err=err,
        startd=startd,
        changed=changed,
        diff=diff,
    )
def main():
    module = AnsibleModule(
        argument_spec=dict(
            cluster=dict(type='str', required=False, default='ceph'),
            path=dict(type='path', required=False),
            systemd=dict(type='bool', required=False, default=True),
            osd_id=dict(type='str', required=False),
            osd_fsid=dict(type='str', required=False),
            osd_all=dict(type='bool', required=False),
        ),
        supports_check_mode=True,
        mutually_exclusive=[
            ('osd_all', 'osd_id'),
            ('osd_all', 'osd_fsid'),
            ('path', 'osd_id'),
            ('path', 'osd_fsid'),
        ],
        required_together=[('osd_id', 'osd_fsid')],
        required_one_of=[
            ('path', 'osd_id', 'osd_all'),
            ('path', 'osd_fsid', 'osd_all'),
        ],
    )

    path = module.params.get('path')
    cluster = module.params.get('cluster')
    systemd = module.params.get('systemd')
    osd_id = module.params.get('osd_id')
    osd_fsid = module.params.get('osd_fsid')
    osd_all = module.params.get('osd_all')

    if path and not os.path.exists(path):
        module.fail_json(msg='{} does not exist'.format(path), rc=1)

    startd = datetime.datetime.now()

    container_image = os.getenv('CEPH_CONTAINER_IMAGE')
    container_binary = os.getenv('CEPH_CONTAINER_BINARY')
    if container_binary and container_image:
        cmd = [
            container_binary, 'run', '--rm', '--privileged', '--ipc=host',
            '--net=host', '-v', '/etc/ceph:/etc/ceph:z', '-v',
            '/var/lib/ceph/:/var/lib/ceph/:z', '-v',
            '/var/log/ceph/:/var/log/ceph/:z', '-v', '/run/lvm/:/run/lvm/',
            '-v', '/run/lock/lvm/:/run/lock/lvm/', '--entrypoint=ceph-volume',
            container_image
        ]
    else:
        cmd = ['ceph-volume']

    cmd.extend(['--cluster', cluster, 'simple', 'activate'])

    if osd_all:
        cmd.append('--all')
    else:
        if path:
            cmd.extend(['--file', path])
        else:
            cmd.extend([osd_id, osd_fsid])

    if not systemd:
        cmd.append('--no-systemd')

    if module.check_mode:
        exit_module(module=module,
                    out='',
                    rc=0,
                    cmd=cmd,
                    err='',
                    startd=startd,
                    changed=False)
    else:
        rc, out, err = module.run_command(cmd)
        exit_module(module=module,
                    out=out,
                    rc=rc,
                    cmd=cmd,
                    err=err,
                    startd=startd,
                    changed=True)
示例#15
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            cluster=dict(type='str', required=False, default='ceph'),
            path=dict(type='path', required=False),
            force=dict(type='bool', required=False, default=False),
            stdout=dict(type='bool', required=False, default=False),
        ),
        supports_check_mode=True,
    )

    path = module.params.get('path')
    cluster = module.params.get('cluster')
    force = module.params.get('force')
    stdout = module.params.get('stdout')

    if path and not os.path.exists(path):
        module.fail_json(msg='{} does not exist'.format(path), rc=1)

    startd = datetime.datetime.now()

    container_image = os.getenv('CEPH_CONTAINER_IMAGE')
    container_binary = os.getenv('CEPH_CONTAINER_BINARY')
    if container_binary and container_image:
        cmd = [
            container_binary, 'run', '--rm', '--privileged', '--ipc=host',
            '--net=host', '-v', '/etc/ceph:/etc/ceph:z', '-v',
            '/var/lib/ceph/:/var/lib/ceph/:z', '-v',
            '/var/log/ceph/:/var/log/ceph/:z', '-v', '/run/lvm/:/run/lvm/',
            '-v', '/run/lock/lvm/:/run/lock/lvm/', '--entrypoint=ceph-volume',
            container_image
        ]
    else:
        cmd = ['ceph-volume']

    cmd.extend(['--cluster', cluster, 'simple', 'scan'])

    if force:
        cmd.append('--force')

    if stdout:
        cmd.append('--stdout')

    if path:
        cmd.append(path)

    if module.check_mode:
        exit_module(module=module,
                    out='',
                    rc=0,
                    cmd=cmd,
                    err='',
                    startd=startd,
                    changed=False)
    else:
        rc, out, err = module.run_command(cmd)
        exit_module(module=module,
                    out=out,
                    rc=rc,
                    cmd=cmd,
                    err=err,
                    startd=startd,
                    changed=True)
示例#16
0
def run_module():
    module_args = dict(
        cluster=dict(type='str', required=False, default='ceph'),
        name=dict(type='str', required=True),
        state=dict(type='str',
                   required=False,
                   default='present',
                   choices=['present', 'absent', 'list']),
        details=dict(type='bool', required=False, default=False),
        size=dict(type='str', required=False),
        min_size=dict(type='str', required=False),
        pg_num=dict(type='str', required=False),
        pgp_num=dict(type='str', required=False),
        pg_autoscale_mode=dict(type='str', required=False, default='on'),
        target_size_ratio=dict(type='str', required=False, default=None),
        pool_type=dict(type='str',
                       required=False,
                       default='replicated',
                       choices=['replicated', 'erasure', '1', '3']),
        erasure_profile=dict(type='str', required=False, default='default'),
        rule_name=dict(type='str', required=False, default=None),
        expected_num_objects=dict(type='str', required=False, default="0"),
        application=dict(type='str', required=False, default=None),
    )

    module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

    # Gather module parameters in variables
    cluster = module.params.get('cluster')
    name = module.params.get('name')
    state = module.params.get('state')
    details = module.params.get('details')
    size = module.params.get('size')
    min_size = module.params.get('min_size')
    pg_num = module.params.get('pg_num')
    pgp_num = module.params.get('pgp_num')
    pg_autoscale_mode = module.params.get('pg_autoscale_mode')
    target_size_ratio = module.params.get('target_size_ratio')
    application = module.params.get('application')

    if (module.params.get('pg_autoscale_mode').lower()
            in ['true', 'on', 'yes']):
        pg_autoscale_mode = 'on'
    elif (module.params.get('pg_autoscale_mode').lower()
          in ['false', 'off', 'no']):
        pg_autoscale_mode = 'off'
    else:
        pg_autoscale_mode = 'warn'

    if module.params.get('pool_type') == '1':
        pool_type = 'replicated'
    elif module.params.get('pool_type') == '3':
        pool_type = 'erasure'
    else:
        pool_type = module.params.get('pool_type')

    if not module.params.get('rule_name'):
        rule_name = 'replicated_rule' if pool_type == 'replicated' else None
    else:
        rule_name = module.params.get('rule_name')

    erasure_profile = module.params.get('erasure_profile')
    expected_num_objects = module.params.get('expected_num_objects')
    user_pool_config = {
        'pool_name': {
            'value': name
        },
        'pg_num': {
            'value': pg_num,
            'cli_set_opt': 'pg_num'
        },
        'pgp_num': {
            'value': pgp_num,
            'cli_set_opt': 'pgp_num'
        },
        'pg_autoscale_mode': {
            'value': pg_autoscale_mode,
            'cli_set_opt': 'pg_autoscale_mode'
        },
        'target_size_ratio': {
            'value': target_size_ratio,
            'cli_set_opt': 'target_size_ratio'
        },
        'application': {
            'value': application
        },
        'type': {
            'value': pool_type
        },
        'erasure_profile': {
            'value': erasure_profile
        },
        'crush_rule': {
            'value': rule_name,
            'cli_set_opt': 'crush_rule'
        },
        'expected_num_objects': {
            'value': expected_num_objects
        },
        'size': {
            'value': size,
            'cli_set_opt': 'size'
        },
        'min_size': {
            'value': min_size
        }
    }

    if module.check_mode:
        module.exit_json(
            changed=False,
            stdout='',
            stderr='',
            rc=0,
            start='',
            end='',
            delta='',
        )

    startd = datetime.datetime.now()
    changed = False

    # will return either the image name or None
    container_image = is_containerized()

    user = "******"
    keyring_filename = cluster + '.' + user + '.keyring'
    user_key = os.path.join("/etc/ceph/", keyring_filename)

    if state == "present":
        rc, cmd, out, err = exec_command(
            module,
            check_pool_exist(cluster,
                             name,
                             user,
                             user_key,
                             container_image=container_image))  # noqa: E501
        if rc == 0:
            running_pool_details = get_pool_details(
                module,
                cluster,
                name,
                user,
                user_key,
                container_image=container_image)  # noqa: E501
            user_pool_config['pg_placement_num'] = {
                'value': str(running_pool_details[2]['pg_placement_num']),
                'cli_set_opt': 'pgp_num'
            }  # noqa: E501
            delta = compare_pool_config(user_pool_config,
                                        running_pool_details[2])
            if len(delta) > 0:
                keys = list(delta.keys())
                details = running_pool_details[2]
                if details['erasure_code_profile'] and 'size' in keys:
                    del delta['size']
                if details['pg_autoscale_mode'] == 'on':
                    delta.pop('pg_num', None)
                    delta.pop('pgp_num', None)

                if len(delta) == 0:
                    out = "Skipping pool {}.\nUpdating either 'size' on an erasure-coded pool or 'pg_num'/'pgp_num' on a pg autoscaled pool is incompatible".format(
                        name)  # noqa: E501
                else:
                    rc, cmd, out, err = update_pool(
                        module,
                        cluster,
                        name,
                        user,
                        user_key,
                        delta,
                        container_image=container_image)  # noqa: E501
                    if rc == 0:
                        changed = True
            else:
                out = "Pool {} already exists and there is nothing to update.".format(
                    name)  # noqa: E501
        else:
            rc, cmd, out, err = exec_command(
                module,
                create_pool(
                    cluster,
                    name,
                    user,
                    user_key,
                    user_pool_config=user_pool_config,  # noqa: E501
                    container_image=container_image))  # noqa: E501
            if user_pool_config['application']['value']:
                rc, _, _, _ = exec_command(
                    module,
                    enable_application_pool(
                        cluster,
                        name,
                        user_pool_config['application']['value'],  # noqa: E501
                        user,
                        user_key,
                        container_image=container_image))  # noqa: E501
            if user_pool_config['min_size']['value']:
                # not implemented yet
                pass
            changed = True

    elif state == "list":
        rc, cmd, out, err = exec_command(
            module,
            list_pools(cluster,
                       name,
                       user,
                       user_key,
                       details,
                       container_image=container_image))  # noqa: E501
        if rc != 0:
            out = "Couldn't list pool(s) present on the cluster"

    elif state == "absent":
        rc, cmd, out, err = exec_command(
            module,
            check_pool_exist(cluster,
                             name,
                             user,
                             user_key,
                             container_image=container_image))  # noqa: E501
        if rc == 0:
            rc, cmd, out, err = exec_command(
                module,
                remove_pool(cluster,
                            name,
                            user,
                            user_key,
                            container_image=container_image))  # noqa: E501
            changed = True
        else:
            rc = 0
            out = "Skipped, since pool {} doesn't exist".format(name)

    exit_module(module=module,
                out=out,
                rc=rc,
                cmd=cmd,
                err=err,
                startd=startd,
                changed=changed)
示例#17
0
def main():
    module = AnsibleModule(
        argument_spec=yaml.safe_load(DOCUMENTATION)['options'],
        supports_check_mode=True,
        required_if=[('state', 'present', ['rule_type']),
                     ('rule_type', 'replicated',
                      ['bucket_root', 'bucket_type']),
                     ('rule_type', 'erasure', ['profile'])])

    # Gather module parameters in variables
    name = module.params.get('name')
    state = module.params.get('state')
    rule_type = module.params.get('rule_type')

    if module.check_mode:
        module.exit_json(
            changed=False,
            stdout='',
            stderr='',
            rc=0,
            start='',
            end='',
            delta='',
        )

    startd = datetime.datetime.now()
    changed = False

    # will return either the image name or None
    container_image = is_containerized()

    if state == "present":
        rc, cmd, out, err = exec_command(
            module, get_rule(module, container_image=container_image))
        if rc != 0:
            rc, cmd, out, err = exec_command(
                module, create_rule(module, container_image=container_image))
            changed = True
        else:
            rule = json.loads(out)
            if (rule['type'] == 1 and rule_type == 'erasure') or (
                    rule['type'] == 3 and rule_type == 'replicated'):
                module.fail_json(
                    msg="Can not convert crush rule {} to {}".format(
                        name, rule_type),
                    changed=False,
                    rc=1)

    elif state == "absent":
        rc, cmd, out, err = exec_command(
            module, get_rule(module, container_image=container_image))
        if rc == 0:
            rc, cmd, out, err = exec_command(
                module, remove_rule(module, container_image=container_image))
            changed = True
        else:
            rc = 0
            out = "Crush Rule {} doesn't exist".format(name)

    elif state == "info":
        rc, cmd, out, err = exec_command(
            module, get_rule(module, container_image=container_image))

    exit_module(module=module,
                out=out,
                rc=rc,
                cmd=cmd,
                err=err,
                startd=startd,
                changed=changed)