示例#1
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(required=True),
            selection=dict(choices=['install', 'hold', 'deinstall', 'purge'])
        ),
        supports_check_mode=True,
    )

    dpkg = module.get_bin_path('dpkg', True)

    name = module.params['name']
    selection = module.params['selection']

    # Get current settings.
    rc, out, err = module.run_command([dpkg, '--get-selections', name], check_rc=True)
    if not out:
        current = 'not present'
    else:
        current = out.split()[1]

    changed = current != selection

    if module.check_mode or not changed:
        module.exit_json(changed=changed, before=current, after=selection)

    module.run_command([dpkg, '--set-selections'], data="%s %s" % (name, selection), check_rc=True)
    module.exit_json(changed=changed, before=current, after=selection)
示例#2
0
def main():
    module = AnsibleModule(argument_spec=dict(
        path=dict(required=True),
        state=dict(default="present",
                   choices=["present", "followed", "absent", "unfollowed"]),
        name=dict(required=False, default=None, type='str'),
        logtype=dict(required=False,
                     default=None,
                     type='str',
                     aliases=['type'])),
                           supports_check_mode=True)

    le_path = module.get_bin_path('le', True, ['/usr/local/bin'])

    p = module.params

    # Handle multiple log files
    logs = p["path"].split(",")
    logs = filter(None, logs)

    if p["state"] in ["present", "followed"]:
        follow_log(module, le_path, logs, name=p['name'], logtype=p['logtype'])

    elif p["state"] in ["absent", "unfollowed"]:
        unfollow_log(module, le_path, logs)
示例#3
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(type='str',
                       default='enabled',
                       choices=['disabled', 'enabled']),
            name=dict(type='list'),
            activate=dict(type='bool', default=False),
        ),
        required_one_of=[['name', 'activate']],
        supports_check_mode=True,
    )

    global AWALL_PATH
    AWALL_PATH = module.get_bin_path('awall', required=True)

    p = module.params

    if p['name']:
        if p['state'] == 'enabled':
            enable_policy(module, p['name'], p['activate'])
        elif p['state'] == 'disabled':
            disable_policy(module, p['name'], p['activate'])

    if p['activate']:
        if not module.check_mode:
            activate(module)
        module.exit_json(changed=True, msg="activated awall rules")

    module.fail_json(msg="no action defined")
示例#4
0
def main():
    """ Entry point. """
    argument_spec = {
        'bridge': {'required': True},
        'port': {'required': True},
        'state': {'default': 'present', 'choices': ['present', 'absent']},
        'timeout': {'default': 5, 'type': 'int'},
        'external_ids': {'default': None, 'type': 'dict'},
        'tag': {'default': None},
        'set': {'required': False, 'default': None}
    }

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

    result = {'changed': False}

    # We add ovs-vsctl to module_params to later build up templatized commands
    module.params["ovs-vsctl"] = module.get_bin_path("ovs-vsctl", True)

    want = map_params_to_obj(module)
    have = map_config_to_obj(module)

    commands = map_obj_to_commands(want, have, module)
    result['commands'] = commands

    if commands:
        if not module.check_mode:
            for c in commands:
                module.run_command(c, check_rc=True)
        result['changed'] = True

    module.exit_json(**result)
示例#5
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            executable=dict(type='str', default='podman'),
            name=dict(type='list')
        ),
        supports_check_mode=True,
    )

    executable = module.params['executable']
    name = module.params.get('name')
    executable = module.get_bin_path(executable, required=True)

    if name:
        valid_names = filter_invalid_names(module, executable, name)
        results = json.loads(get_image_info(module, executable, valid_names))
    else:
        results = json.loads(get_all_image_info(module, executable))

    results = dict(
        changed=False,
        images=results
    )

    module.exit_json(**results)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(default="installed", choices=['installed', 'removed', 'absent', 'present', 'latest']),
            name=dict(aliases=["pkg"], required=True, type='list'),
            update_cache=dict(default=False, aliases=["update-cache"],
                              type='bool'),
        ),
        supports_check_mode=True)

    slackpkg_path = module.get_bin_path('slackpkg', True)

    p = module.params

    pkgs = p['name']

    if p["update_cache"]:
        update_cache(module, slackpkg_path)

    if p['state'] == 'latest':
        upgrade_packages(module, slackpkg_path, pkgs)

    elif p['state'] in ['present', 'installed']:
        install_packages(module, slackpkg_path, pkgs)

    elif p["state"] in ['removed', 'absent']:
        remove_packages(module, slackpkg_path, pkgs)
示例#7
0
def main():
    module = AnsibleModule(argument_spec=dict(
        name=dict(aliases=["pkg"], required=True),
        state=dict(default="present",
                   choices=["present", "installed", "absent", "removed"]),
        force=dict(default="",
                   choices=[
                       "", "depends", "maintainer", "reinstall", "overwrite",
                       "downgrade", "space", "postinstall", "remove",
                       "checksum", "removal-of-dependent-packages"
                   ]),
        update_cache=dict(default="no", aliases=["update-cache"],
                          type='bool')))

    opkg_path = module.get_bin_path('opkg', True, ['/bin'])

    p = module.params

    if p["update_cache"]:
        update_package_db(module, opkg_path)

    pkgs = p["name"].split(",")

    if p["state"] in ["present", "installed"]:
        install_packages(module, opkg_path, pkgs)

    elif p["state"] in ["absent", "removed"]:
        remove_packages(module, opkg_path, pkgs)
示例#8
0
def main():

    module = AnsibleModule(argument_spec=dict(
        msg=dict(required=True),
        voice=dict(required=False),
    ),
                           supports_check_mode=True)

    msg = module.params['msg']
    voice = module.params['voice']
    possibles = ('say', 'espeak', 'espeak-ng')

    if platform.system() != 'Darwin':
        # 'say' binary available, it might be GNUstep tool which doesn't support 'voice' parameter
        voice = None

    for possible in possibles:
        executable = module.get_bin_path(possible)
        if executable:
            break
    else:
        module.fail_json(msg='Unable to find either %s' % ', '.join(possibles))

    if module.check_mode:
        module.exit_json(msg=msg, changed=False)

    say(module, executable, msg, voice)

    module.exit_json(msg=msg, changed=True)
示例#9
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            database=dict(type='str', required=True),
            key=dict(type='str'),
            service=dict(type='str'),
            split=dict(type='str'),
            fail_key=dict(type='bool', default=True),
        ),
        supports_check_mode=True,
    )

    colon = ['passwd', 'shadow', 'group', 'gshadow']

    database = module.params['database']
    key = module.params.get('key')
    split = module.params.get('split')
    service = module.params.get('service')
    fail_key = module.params.get('fail_key')

    getent_bin = module.get_bin_path('getent', True)

    if key is not None:
        cmd = [getent_bin, database, key]
    else:
        cmd = [getent_bin, database]

    if service is not None:
        cmd.extend(['-s', service])

    if split is None and database in colon:
        split = ':'

    try:
        rc, out, err = module.run_command(cmd)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=traceback.format_exc())

    msg = "Unexpected failure!"
    dbtree = 'getent_%s' % database
    results = {dbtree: {}}

    if rc == 0:
        for line in out.splitlines():
            record = line.split(split)
            results[dbtree][record[0]] = record[1:]

        module.exit_json(ansible_facts=results)

    elif rc == 1:
        msg = "Missing arguments, or database unknown."
    elif rc == 2:
        msg = "One or more supplied key could not be found in the database."
        if not fail_key:
            results[dbtree][key] = None
            module.exit_json(ansible_facts=results, msg=msg)
    elif rc == 3:
        msg = "Enumeration not supported on this database."

    module.fail_json(msg=msg)
示例#10
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='list', required=True, aliases=['pkg']),
            repository_path=dict(type='path'),
            accept_license=dict(type='bool', default=False),
            state=dict(type='str',
                       default='present',
                       choices=['absent', 'present']),
        ),
        supports_check_mode=True,
    )

    name = module.params['name']
    repository_path = module.params['repository_path']
    accept_license = module.params['accept_license']
    state = module.params['state']

    installp_cmd = module.get_bin_path('installp', True)

    if state == 'present':
        if repository_path is None:
            module.fail_json(
                msg="repository_path is required to install package")

        changed, msg = install(module, installp_cmd, name, repository_path,
                               accept_license)

    elif state == 'absent':
        changed, msg = remove(module, installp_cmd, name)

    else:
        module.fail_json(changed=False, msg="Unexpected state.")

    module.exit_json(changed=changed, msg=msg)
示例#11
0
def main():

    module = AnsibleModule(
        argument_spec={},
        supports_check_mode=True,
    )

    if platform.system() != 'Linux':
        module.fail_json(msg='This module requires Linux.')

    def getPidSTime(pid):
        ps_cmd = module.get_bin_path('ps', True)
        rc, ps_output, stderr = module.run_command(
            [ps_cmd, '-o', 'lstart', '-p',
             str(pid)])
        stime = ''
        if rc == 0:
            for line in ps_output.splitlines():
                if 'started' not in line:
                    stime = line
        return stime

    def getPidUser(pid):
        ps_cmd = module.get_bin_path('ps', True)
        rc, ps_output, stderr = module.run_command(
            [ps_cmd, '-o', 'user', '-p', str(pid)])
        user = ''
        if rc == 0:
            for line in ps_output.splitlines():
                if line != 'USER':
                    user = line
        return user

    result = {
        'changed': False,
        'ansible_facts': {
            'tcp_listen': [],
            'udp_listen': [],
        },
    }

    try:
        netstat_cmd = module.get_bin_path('netstat', True)

        # which ports are listening for connections?
        rc, stdout, stderr = module.run_command([netstat_cmd, '-plunt'])
        if rc == 0:
            netstatOut = netStatParse(stdout)
            for p in netstatOut:
                p['stime'] = getPidSTime(p['pid'])
                p['user'] = getPidUser(p['pid'])
                if p['protocol'] == 'tcp':
                    result['ansible_facts']['tcp_listen'].append(p)
                elif p['protocol'] == 'udp':
                    result['ansible_facts']['udp_listen'].append(p)
    except (KeyError, EnvironmentError) as e:
        module.fail_json(msg=to_native(e))

    module.exit_json(**result)
 def test_sanity_check(self):
     basic._load_params = lambda: {}
     # Module used internally to execute ssh-keygen system executable
     module = AnsibleModule(argument_spec={})
     host = '10.0.0.1'
     key = '%s ssh-rsa ASDF foo@bar' % (host, )
     keygen = module.get_bin_path('ssh-keygen')
     sanity_check(module, host, key, keygen)
示例#13
0
def main():

    module = AnsibleModule(
        argument_spec=dict(
            command=dict(type='str'),
            script_file=dict(type='str'),
            count=dict(type='int'),
            units=dict(type='str',
                       choices=['minutes', 'hours', 'days', 'weeks']),
            state=dict(type='str',
                       default='present',
                       choices=['absent', 'present']),
            unique=dict(type='bool', default=False),
        ),
        mutually_exclusive=[['command', 'script_file']],
        required_one_of=[['command', 'script_file']],
        supports_check_mode=False,
    )

    at_cmd = module.get_bin_path('at', True)

    command = module.params['command']
    script_file = module.params['script_file']
    count = module.params['count']
    units = module.params['units']
    state = module.params['state']
    unique = module.params['unique']

    if (state == 'present') and (not count or not units):
        module.fail_json(msg="present state requires count and units")

    result = dict(
        changed=False,
        state=state,
    )

    # If command transform it into a script_file
    if command:
        script_file = create_tempfile(command)

    # if absent remove existing and return
    if state == 'absent':
        delete_job(module, result, at_cmd, command, script_file)

    # if unique if existing return unchanged
    if unique:
        if len(get_matching_jobs(module, at_cmd, script_file)) != 0:
            if command:
                os.unlink(script_file)
            module.exit_json(**result)

    result['script_file'] = script_file
    result['count'] = count
    result['units'] = units

    add_job(module, result, at_cmd, count, units, command, script_file)

    module.exit_json(**result)
示例#14
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            dest=dict(type='path', required=True),
            name=dict(type='str', required=True, aliases=['parent']),
            version=dict(type='str', default='head'),
            force=dict(type='bool', default='no'),
            executable=dict(type='str'),
        )
    )

    dest = module.params['dest']
    parent = module.params['name']
    version = module.params['version']
    force = module.params['force']
    bzr_path = module.params['executable'] or module.get_bin_path('bzr', True)

    bzrconfig = os.path.join(dest, '.bzr', 'branch', 'branch.conf')

    rc, out, err = (0, None, None)

    bzr = Bzr(module, parent, dest, version, bzr_path)

    # if there is no bzr configuration, do a branch operation
    # else pull and switch the version
    before = None
    local_mods = False
    if not os.path.exists(bzrconfig):
        (rc, out, err) = bzr.clone()

    else:
        # else do a pull
        local_mods = bzr.has_local_mods()
        before = bzr.get_version()
        (rc, out, err) = bzr.reset(force)
        if rc != 0:
            module.fail_json(msg=err)
        (rc, out, err) = bzr.fetch()
        if rc != 0:
            module.fail_json(msg=err)

    # switch to version specified regardless of whether
    # we cloned or pulled
    (rc, out, err) = bzr.switch_version()

    # determine if we changed anything
    after = bzr.get_version()
    changed = False

    if before != after or local_mods:
        changed = True

    module.exit_json(changed=changed, before=before, after=after)
示例#15
0
def main():
    module = AnsibleModule(
        argument_spec=dict(state=dict(default="present",
                                      choices=["present", "absent"]),
                           name=dict(aliases=["pkg"], type='list'),
                           update_cache=dict(default='no', type='bool'),
                           upgrade=dict(default='no', type='bool'),
                           full_upgrade=dict(default='no', type='bool'),
                           clean=dict(default='no', type='bool'),
                           force=dict(default='no', type='bool')),
        required_one_of=[[
            'name', 'update_cache', 'upgrade', 'full_upgrade', 'clean'
        ]],
        supports_check_mode=True)

    global PKGIN_PATH
    PKGIN_PATH = module.get_bin_path('pkgin', True, ['/opt/local/bin'])

    module.run_command_environ_update = dict(LANG='C',
                                             LC_ALL='C',
                                             LC_MESSAGES='C',
                                             LC_CTYPE='C')

    p = module.params

    if p["update_cache"]:
        c, msg = update_package_db(module)
        if not (p['name'] or p["upgrade"] or p["full_upgrade"]):
            module.exit_json(changed=c, msg=msg)

    if p["upgrade"]:
        upgrade_packages(module)
        if not p['name']:
            module.exit_json(changed=True, msg='upgraded packages')

    if p["full_upgrade"]:
        full_upgrade_packages(module)
        if not p['name']:
            module.exit_json(changed=True, msg='upgraded all packages')

    if p["clean"]:
        clean_cache(module)
        if not p['name']:
            module.exit_json(changed=True, msg='cleaned caches')

    pkgs = p["name"]

    if p["state"] == "present":
        install_packages(module, pkgs)

    elif p["state"] == "absent":
        remove_packages(module, pkgs)
示例#16
0
def main():
    module = AnsibleModule(argument_spec=dict(
        state=dict(
            default='present',
            choices=['present', 'installed', 'absent', 'removed', 'latest']),
        name=dict(type='list', elements='str'),
        repository=dict(type='list'),
        update_cache=dict(default='no', type='bool'),
        upgrade=dict(default='no', type='bool'),
        available=dict(default='no', type='bool'),
    ),
                           required_one_of=[[
                               'name', 'update_cache', 'upgrade'
                           ]],
                           mutually_exclusive=[['name', 'upgrade']],
                           supports_check_mode=True)

    # Set LANG env since we parse stdout
    module.run_command_environ_update = dict(LANG='C',
                                             LC_ALL='C',
                                             LC_MESSAGES='C',
                                             LC_CTYPE='C')

    global APK_PATH
    APK_PATH = module.get_bin_path('apk', required=True)

    p = module.params

    # add repositories to the APK_PATH
    if p['repository']:
        for r in p['repository']:
            APK_PATH = "%s --repository %s --repositories-file /dev/null" % (
                APK_PATH, r)

    # normalize the state parameter
    if p['state'] in ['present', 'installed']:
        p['state'] = 'present'
    if p['state'] in ['absent', 'removed']:
        p['state'] = 'absent'

    if p['update_cache']:
        update_package_db(module, not p['name'] and not p['upgrade'])

    if p['upgrade']:
        upgrade_packages(module, p['available'])

    if p['state'] in ['present', 'latest']:
        install_packages(module, p['name'], p['state'])
    elif p['state'] == 'absent':
        remove_packages(module, p['name'])
def main():
    module = AnsibleModule(
        argument_spec=dict(executable=dict(type='str', default='podman'),
                           name=dict(type='str')),
        supports_check_mode=True,
    )

    name = module.params['name']
    executable = module.get_bin_path(module.params['executable'],
                                     required=True)

    inspect_results, out, err = get_volume_info(module, executable, name)

    results = {"changed": False, "volumes": inspect_results, "stderr": err}

    module.exit_json(**results)
示例#18
0
def main():
    module = AnsibleModule(
        argument_spec=dict(name=dict(default=None,
                                     aliases=['spell'],
                                     type='list'),
                           state=dict(default='present',
                                      choices=[
                                          'present', 'latest', 'absent',
                                          'cast', 'dispelled', 'rebuild'
                                      ]),
                           depends=dict(default=None),
                           update=dict(default=False, type='bool'),
                           update_cache=dict(default=False,
                                             aliases=['update_codex'],
                                             type='bool'),
                           cache_valid_time=dict(default=0, type='int')),
        required_one_of=[['name', 'update', 'update_cache']],
        supports_check_mode=True)

    if os.geteuid() != 0:
        module.fail_json(msg="root privileges are required for this operation")

    for c in SORCERY:
        SORCERY[c] = module.get_bin_path(c, True)

    # prepare environment: run sorcery commands without asking questions
    module.run_command_environ_update = dict(PROMPT_DELAY='0', VOYEUR='0')

    params = module.params

    # normalize 'state' parameter
    if params['state'] in ('present', 'cast'):
        params['state'] = 'present'
    elif params['state'] in ('absent', 'dispelled'):
        params['state'] = 'absent'

    if params['update']:
        update_sorcery(module)

    if params['update_cache'] or params['state'] == 'latest':
        update_codex(module)

    if params['name']:
        manage_spells(module)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(aliases=['tap'], type='list', required=True),
            url=dict(default=None, required=False),
            state=dict(default='present', choices=['present', 'absent']),
        ),
        supports_check_mode=True,
    )

    brew_path = module.get_bin_path('brew',
                                    required=True,
                                    opt_dirs=['/usr/local/bin'])

    taps = module.params['name']
    url = module.params['url']

    if module.params['state'] == 'present':
        if url is None:
            # No tap URL provided explicitly, continue with bulk addition
            # of all the taps.
            failed, changed, msg = add_taps(module, brew_path, taps)
        else:
            # When an tap URL is provided explicitly, we allow adding
            # *single* tap only. Validate and proceed to add single tap.
            if len(taps) > 1:
                msg = "List of multiple taps may not be provided with 'url' option."
                module.fail_json(msg=msg)
            else:
                failed, changed, msg = add_tap(module, brew_path, taps[0], url)

        if failed:
            module.fail_json(msg=msg)
        else:
            module.exit_json(changed=changed, msg=msg)

    elif module.params['state'] == 'absent':
        failed, changed, msg = remove_taps(module, brew_path, taps)

        if failed:
            module.fail_json(msg=msg)
        else:
            module.exit_json(changed=changed, msg=msg)
示例#20
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str', required=True),
            flatpakrepo_url=dict(type='str'),
            method=dict(type='str', default='system',
                        choices=['user', 'system']),
            state=dict(type='str', default="present",
                       choices=['absent', 'present']),
            executable=dict(type='str', default="flatpak")
        ),
        # This module supports check mode
        supports_check_mode=True,
    )

    name = module.params['name']
    flatpakrepo_url = module.params['flatpakrepo_url']
    method = module.params['method']
    state = module.params['state']
    executable = module.params['executable']
    binary = module.get_bin_path(executable, None)

    if flatpakrepo_url is None:
        flatpakrepo_url = ''

    global result
    result = dict(
        changed=False
    )

    # If the binary was not found, fail the operation
    if not binary:
        module.fail_json(msg="Executable '%s' was not found on the system." % executable, **result)

    remote_already_exists = remote_exists(module, binary, to_bytes(name), method)

    if state == 'present' and not remote_already_exists:
        add_remote(module, binary, name, flatpakrepo_url, method)
    elif state == 'absent' and remote_already_exists:
        remove_remote(module, binary, name, method)

    module.exit_json(**result)
def main():
    global module
    global glusterbin
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='str', required=True, aliases=['volume']),
            status_filter=dict(type='str', default='self-heal', choices=['self-heal', 'rebalance']),
        ),
    )
    is_old_facts = module._name == 'gluster_heal_facts'
    if is_old_facts:
        module.deprecate("The 'gluster_heal_facts' module has been renamed to 'gluster_heal_info', "
                         "and the renamed one no longer returns ansible_facts", version='2.13')

    glusterbin = module.get_bin_path('gluster', True)
    required_version = "3.2"
    status_filter = module.params['status_filter']
    volume_name = module.params['name']
    heal_info = ''
    rebalance_status = ''

    # Verify if required GlusterFS version is installed
    if is_invalid_gluster_version(module, required_version):
        module.fail_json(msg="GlusterFS version > %s is required" %
                         required_version)

    try:
        if status_filter == "self-heal":
            heal_info = get_self_heal_status(volume_name)
        elif status_filter == "rebalance":
            rebalance_status = get_rebalance_status(volume_name)
    except Exception as e:
        module.fail_json(msg='Error retrieving status: %s' % e, exception=traceback.format_exc())

    facts = {}
    facts['glusterfs'] = {'volume': volume_name, 'status_filter': status_filter, 'heal_info': heal_info, 'rebalance': rebalance_status}

    if is_old_facts:
        module.exit_json(ansible_facts=facts)
    else:
        module.exit_json(**facts)
示例#22
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            name=dict(type='list', elements='str', aliases=["port"]),
            selfupdate=dict(aliases=["update_cache", "update_ports"], default=False, type='bool'),
            state=dict(default="present", choices=["present", "installed", "absent", "removed", "active", "inactive"]),
            upgrade=dict(default=False, type='bool'),
            variant=dict(aliases=["variants"], default=None, type='str')
        )
    )

    port_path = module.get_bin_path('port', True, ['/opt/local/bin'])

    p = module.params

    if p["selfupdate"]:
        (changed, msg) = selfupdate(module, port_path)
        if not (p["name"] or p["upgrade"]):
            module.exit_json(changed=changed, msg=msg)

    if p["upgrade"]:
        (changed, msg) = upgrade(module, port_path)
        if not p["name"]:
            module.exit_json(changed=changed, msg=msg)

    pkgs = p["name"]

    variant = p["variant"]

    if p["state"] in ["present", "installed"]:
        install_ports(module, port_path, pkgs, variant)

    elif p["state"] in ["absent", "removed"]:
        remove_ports(module, port_path, pkgs)

    elif p["state"] == "active":
        activate_ports(module, port_path, pkgs)

    elif p["state"] == "inactive":
        deactivate_ports(module, port_path, pkgs)
示例#23
0
def main():
    # This module supports check mode
    module = AnsibleModule(
        argument_spec=dict(name=dict(type='str', required=True),
                           remote=dict(type='str', default='flathub'),
                           method=dict(type='str',
                                       default='system',
                                       choices=['user', 'system']),
                           state=dict(type='str',
                                      default='present',
                                      choices=['absent', 'present']),
                           executable=dict(type='path', default='flatpak')),
        supports_check_mode=True,
    )

    name = module.params['name']
    state = module.params['state']
    remote = module.params['remote']
    method = module.params['method']
    executable = module.params['executable']
    binary = module.get_bin_path(executable, None)

    global result
    result = dict(changed=False)

    # If the binary was not found, fail the operation
    if not binary:
        module.fail_json(msg="Executable '%s' was not found on the system." %
                         executable,
                         **result)

    if state == 'present' and not flatpak_exists(module, binary, name, method):
        install_flat(module, binary, remote, name, method)
    elif state == 'absent' and flatpak_exists(module, binary, name, method):
        uninstall_flat(module, binary, name, method)

    module.exit_json(**result)
示例#24
0
def main():
    module = AnsibleModule(
        argument_spec={
            'executable': {
                'type': 'str',
                'default': 'podman'
            },
            'name': {
                'type': 'list',
                'elements': 'str'
            },
        },
        supports_check_mode=True,
    )

    name = module.params['name']
    executable = module.get_bin_path(module.params['executable'],
                                     required=True)

    inspect_results, out, err = get_containers_facts(module, executable, name)

    results = {"changed": False, "containers": inspect_results, "stderr": err}

    module.exit_json(**results)
示例#25
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            package=dict(type='list',
                         elements='str',
                         default=None,
                         aliases=['name']),
            state=dict(
                default=portage_present_states[0],
                choices=portage_present_states + portage_absent_states,
            ),
            update=dict(default=False, type='bool'),
            deep=dict(default=False, type='bool'),
            newuse=dict(default=False, type='bool'),
            changed_use=dict(default=False, type='bool'),
            oneshot=dict(default=False, type='bool'),
            noreplace=dict(default=True, type='bool'),
            nodeps=dict(default=False, type='bool'),
            onlydeps=dict(default=False, type='bool'),
            depclean=dict(default=False, type='bool'),
            quiet=dict(default=False, type='bool'),
            verbose=dict(default=False, type='bool'),
            sync=dict(default=None, choices=['yes', 'web', 'no']),
            getbinpkg=dict(default=False, type='bool'),
            usepkgonly=dict(default=False, type='bool'),
            usepkg=dict(default=False, type='bool'),
            keepgoing=dict(default=False, type='bool'),
            jobs=dict(default=None, type='int'),
            loadavg=dict(default=None, type='float'),
            quietbuild=dict(default=False, type='bool'),
            quietfail=dict(default=False, type='bool'),
        ),
        required_one_of=[['package', 'sync', 'depclean']],
        mutually_exclusive=[
            ['nodeps', 'onlydeps'],
            ['quiet', 'verbose'],
            ['quietbuild', 'verbose'],
            ['quietfail', 'verbose'],
        ],
        supports_check_mode=True,
    )

    module.emerge_path = module.get_bin_path('emerge', required=True)
    module.equery_path = module.get_bin_path('equery', required=True)

    p = module.params

    if p['sync'] and p['sync'].strip() != 'no':
        sync_repositories(module, webrsync=(p['sync'] == 'web'))
        if not p['package']:
            module.exit_json(msg='Sync successfully finished.')

    packages = []
    if p['package']:
        packages.extend(p['package'])

    if p['depclean']:
        if packages and p['state'] not in portage_absent_states:
            module.fail_json(
                msg='Depclean can only be used with package when the state is '
                'one of: %s' % portage_absent_states, )

        cleanup_packages(module, packages)

    elif p['state'] in portage_present_states:
        emerge_packages(module, packages)

    elif p['state'] in portage_absent_states:
        unmerge_packages(module, packages)
示例#26
0
def main():
    # MAIN

    global module
    module = AnsibleModule(argument_spec=dict(
        name=dict(type='str', required=True, aliases=['volume']),
        state=dict(type='str',
                   required=True,
                   choices=['absent', 'started', 'stopped', 'present']),
        cluster=dict(type='list'),
        host=dict(type='str'),
        stripes=dict(type='int'),
        replicas=dict(type='int'),
        arbiters=dict(type='int'),
        disperses=dict(type='int'),
        redundancies=dict(type='int'),
        transport=dict(type='str',
                       default='tcp',
                       choices=['tcp', 'rdma', 'tcp,rdma']),
        bricks=dict(type='str', aliases=['brick']),
        start_on_create=dict(type='bool', default=True),
        rebalance=dict(type='bool', default=False),
        options=dict(type='dict', default={}),
        quota=dict(type='str'),
        directory=dict(type='str'),
        force=dict(type='bool', default=False),
    ), )

    global glusterbin
    glusterbin = module.get_bin_path('gluster', True)

    changed = False

    action = module.params['state']
    volume_name = module.params['name']
    cluster = module.params['cluster']
    brick_paths = module.params['bricks']
    stripes = module.params['stripes']
    replicas = module.params['replicas']
    arbiters = module.params['arbiters']
    disperses = module.params['disperses']
    redundancies = module.params['redundancies']
    transport = module.params['transport']
    myhostname = module.params['host']
    start_on_create = module.boolean(module.params['start_on_create'])
    rebalance = module.boolean(module.params['rebalance'])
    force = module.boolean(module.params['force'])

    if not myhostname:
        myhostname = socket.gethostname()

    # Clean up if last element is empty. Consider that yml can look like this:
    #   cluster="{% for host in groups['glusterfs'] %}{{ hostvars[host]['private_ip'] }},{% endfor %}"
    if cluster is not None and len(cluster) > 1 and cluster[-1] == '':
        cluster = cluster[0:-1]

    if cluster is None:
        cluster = []

    if brick_paths is not None and "," in brick_paths:
        brick_paths = brick_paths.split(",")
    else:
        brick_paths = [brick_paths]

    options = module.params['options']
    quota = module.params['quota']
    directory = module.params['directory']

    # get current state info
    peers = get_peers()
    volumes = get_volumes()
    quotas = {}
    if volume_name in volumes and volumes[volume_name]['quota'] and volumes[
            volume_name]['status'].lower() == 'started':
        quotas = get_quotas(volume_name, True)

    # do the work!
    if action == 'absent':
        if volume_name in volumes:
            if volumes[volume_name]['status'].lower() != 'stopped':
                stop_volume(volume_name)
            run_gluster(['volume', 'delete', volume_name])
            changed = True

    if action == 'present':
        probe_all_peers(cluster, peers, myhostname)

        # create if it doesn't exist
        if volume_name not in volumes:
            create_volume(volume_name, stripes, replicas, arbiters, disperses,
                          redundancies, transport, cluster, brick_paths, force)
            volumes = get_volumes()
            changed = True

        if volume_name in volumes:
            if volumes[volume_name]['status'].lower(
            ) != 'started' and start_on_create:
                start_volume(volume_name)
                changed = True

            # switch bricks
            new_bricks = []
            removed_bricks = []
            all_bricks = []
            bricks_in_volume = volumes[volume_name]['bricks']

            for node in cluster:
                for brick_path in brick_paths:
                    brick = '%s:%s' % (node, brick_path)
                    all_bricks.append(brick)
                    if brick not in bricks_in_volume:
                        new_bricks.append(brick)

            if not new_bricks and len(all_bricks) > 0 and \
               len(all_bricks) < len(bricks_in_volume):
                for brick in bricks_in_volume:
                    if brick not in all_bricks:
                        removed_bricks.append(brick)

            if new_bricks:
                add_bricks(volume_name, new_bricks, stripes, replicas, force)
                changed = True

            if removed_bricks:
                if replicas and int(replicas) < int(
                        volumes[volume_name]['replicas']):
                    reduce_config(volume_name, removed_bricks, str(replicas),
                                  force)
                else:
                    remove_bricks(volume_name, removed_bricks, force)
                changed = True

            # handle quotas
            if quota:
                if not volumes[volume_name]['quota']:
                    enable_quota(volume_name)
                quotas = get_quotas(volume_name, False)
                if directory not in quotas or quotas[directory] != quota:
                    set_quota(volume_name, directory, quota)
                    changed = True

            # set options
            for option in options.keys():
                if option not in volumes[volume_name]['options'] or volumes[
                        volume_name]['options'][option] != options[option]:
                    set_volume_option(volume_name, option, options[option])
                    changed = True

        else:
            module.fail_json(msg='failed to create volume %s' % volume_name)

    if action != 'absent' and volume_name not in volumes:
        module.fail_json(msg='volume not found %s' % volume_name)

    if action == 'started':
        if volumes[volume_name]['status'].lower() != 'started':
            start_volume(volume_name)
            changed = True

    if action == 'stopped':
        if volumes[volume_name]['status'].lower() != 'stopped':
            stop_volume(volume_name)
            changed = True

    if changed:
        volumes = get_volumes()
        if rebalance:
            do_rebalance(volume_name)

    facts = {}
    facts['glusterfs'] = {'peers': peers, 'volumes': volumes, 'quotas': quotas}

    module.exit_json(changed=changed, ansible_facts=facts)
def main():
    module = AnsibleModule(
        argument_spec=dict(
            vg=dict(type='str', required=True),
            lv=dict(type='str', required=True),
            lv_type=dict(type='str', default='jfs2'),
            size=dict(type='str'),
            opts=dict(type='str', default=''),
            copies=dict(type='int', default=1),
            state=dict(type='str', default='present', choices=['absent', 'present']),
            policy=dict(type='str', default='maximum', choices=['maximum', 'minimum']),
            pvs=dict(type='list', default=list())
        ),
        supports_check_mode=True,
    )

    vg = module.params['vg']
    lv = module.params['lv']
    lv_type = module.params['lv_type']
    size = module.params['size']
    opts = module.params['opts']
    copies = module.params['copies']
    policy = module.params['policy']
    state = module.params['state']
    pvs = module.params['pvs']

    pv_list = ' '.join(pvs)

    if policy == 'maximum':
        lv_policy = 'x'
    else:
        lv_policy = 'm'

    # Add echo command when running in check-mode
    if module.check_mode:
        test_opt = 'echo '
    else:
        test_opt = ''

    # check if system commands are available
    lsvg_cmd = module.get_bin_path("lsvg", required=True)
    lslv_cmd = module.get_bin_path("lslv", required=True)

    # Get information on volume group requested
    rc, vg_info, err = module.run_command("%s %s" % (lsvg_cmd, vg))

    if rc != 0:
        if state == 'absent':
            module.exit_json(changed=False, msg="Volume group %s does not exist." % vg)
        else:
            module.fail_json(msg="Volume group %s does not exist." % vg, rc=rc, out=vg_info, err=err)

    this_vg = parse_vg(vg_info)

    if size is not None:
        # Calculate pp size and round it up based on pp size.
        lv_size = round_ppsize(convert_size(module, size), base=this_vg['pp_size'])

    # Get information on logical volume requested
    rc, lv_info, err = module.run_command(
        "%s %s" % (lslv_cmd, lv))

    if rc != 0:
        if state == 'absent':
            module.exit_json(changed=False, msg="Logical Volume %s does not exist." % lv)

    changed = False

    this_lv = parse_lv(lv_info)

    if state == 'present' and not size:
        if this_lv is None:
            module.fail_json(msg="No size given.")

    if this_lv is None:
        if state == 'present':
            if lv_size > this_vg['free']:
                module.fail_json(msg="Not enough free space in volume group %s: %s MB free." % (this_vg['name'], this_vg['free']))

            # create LV
            mklv_cmd = module.get_bin_path("mklv", required=True)

            cmd = "%s %s -t %s -y %s -c %s  -e %s %s %s %sM %s" % (test_opt, mklv_cmd, lv_type, lv, copies, lv_policy, opts, vg, lv_size, pv_list)
            rc, out, err = module.run_command(cmd)
            if rc == 0:
                module.exit_json(changed=True, msg="Logical volume %s created." % lv)
            else:
                module.fail_json(msg="Creating logical volume %s failed." % lv, rc=rc, out=out, err=err)
    else:
        if state == 'absent':
            # remove LV
            rmlv_cmd = module.get_bin_path("rmlv", required=True)
            rc, out, err = module.run_command("%s %s -f %s" % (test_opt, rmlv_cmd, this_lv['name']))
            if rc == 0:
                module.exit_json(changed=True, msg="Logical volume %s deleted." % lv)
            else:
                module.fail_json(msg="Failed to remove logical volume %s." % lv, rc=rc, out=out, err=err)
        else:
            if this_lv['policy'] != policy:
                # change lv allocation policy
                chlv_cmd = module.get_bin_path("chlv", required=True)
                rc, out, err = module.run_command("%s %s -e %s %s" % (test_opt, chlv_cmd, lv_policy, this_lv['name']))
                if rc == 0:
                    module.exit_json(changed=True, msg="Logical volume %s policy changed: %s." % (lv, policy))
                else:
                    module.fail_json(msg="Failed to change logical volume %s policy." % lv, rc=rc, out=out, err=err)

            if vg != this_lv['vg']:
                module.fail_json(msg="Logical volume %s already exist in volume group %s" % (lv, this_lv['vg']))

            # from here the last remaining action is to resize it, if no size parameter is passed we do nothing.
            if not size:
                module.exit_json(changed=False, msg="Logical volume %s already exist." % (lv))

            # resize LV based on absolute values
            if int(lv_size) > this_lv['size']:
                extendlv_cmd = module.get_bin_path("extendlv", required=True)
                cmd = "%s %s %s %sM" % (test_opt, extendlv_cmd, lv, lv_size - this_lv['size'])
                rc, out, err = module.run_command(cmd)
                if rc == 0:
                    module.exit_json(changed=True, msg="Logical volume %s size extended to %sMB." % (lv, lv_size))
                else:
                    module.fail_json(msg="Unable to resize %s to %sMB." % (lv, lv_size), rc=rc, out=out, err=err)
            elif lv_size < this_lv['size']:
                module.fail_json(msg="No shrinking of Logical Volume %s permitted. Current size: %s MB" % (lv, this_lv['size']))
            else:
                module.exit_json(changed=False, msg="Logical volume %s size is already %sMB." % (lv, lv_size))
示例#28
0
def main():
    command_keys = ['state', 'default', 'rule', 'logging']

    module = AnsibleModule(
        argument_spec=dict(
            state=dict(type='str',
                       choices=['enabled', 'disabled', 'reloaded', 'reset']),
            default=dict(type='str',
                         aliases=['policy'],
                         choices=['allow', 'deny', 'reject']),
            logging=dict(
                type='str',
                choices=['full', 'high', 'low', 'medium', 'off', 'on']),
            direction=dict(
                type='str',
                choices=['in', 'incoming', 'out', 'outgoing', 'routed']),
            delete=dict(type='bool', default=False),
            route=dict(type='bool', default=False),
            insert=dict(type='int'),
            insert_relative_to=dict(choices=[
                'zero', 'first-ipv4', 'last-ipv4', 'first-ipv6', 'last-ipv6'
            ],
                                    default='zero'),
            rule=dict(type='str', choices=['allow', 'deny', 'limit',
                                           'reject']),
            interface=dict(type='str', aliases=['if']),
            interface_in=dict(type='str', aliases=['if_in']),
            interface_out=dict(type='str', aliases=['if_out']),
            log=dict(type='bool', default=False),
            from_ip=dict(type='str', default='any', aliases=['from', 'src']),
            from_port=dict(type='str'),
            to_ip=dict(type='str', default='any', aliases=['dest', 'to']),
            to_port=dict(type='str', aliases=['port']),
            proto=dict(type='str',
                       aliases=['protocol'],
                       choices=[
                           'ah', 'any', 'esp', 'ipv6', 'tcp', 'udp', 'gre',
                           'igmp'
                       ]),
            name=dict(type='str', aliases=['app']),
            comment=dict(type='str'),
        ),
        supports_check_mode=True,
        mutually_exclusive=[
            ['name', 'proto', 'logging'],
            # Mutual exclusivity with `interface` implied by `required_by`.
            ['direction', 'interface_in'],
            ['direction', 'interface_out'],
        ],
        required_one_of=([command_keys]),
        required_by=dict(interface=('direction', ), ),
    )

    cmds = []

    ipv4_regexp = compile_ipv4_regexp()
    ipv6_regexp = compile_ipv6_regexp()

    def filter_line_that_not_start_with(pattern, content):
        return ''.join([
            line for line in content.splitlines(True)
            if line.startswith(pattern)
        ])

    def filter_line_that_contains(pattern, content):
        return [line for line in content.splitlines(True) if pattern in line]

    def filter_line_that_not_contains(pattern, content):
        return ''.join([
            line for line in content.splitlines(True)
            if not line.contains(pattern)
        ])

    def filter_line_that_match_func(match_func, content):
        return ''.join([
            line for line in content.splitlines(True)
            if match_func(line) is not None
        ])

    def filter_line_that_contains_ipv4(content):
        return filter_line_that_match_func(ipv4_regexp.search, content)

    def filter_line_that_contains_ipv6(content):
        return filter_line_that_match_func(ipv6_regexp.search, content)

    def is_starting_by_ipv4(ip):
        return ipv4_regexp.match(ip) is not None

    def is_starting_by_ipv6(ip):
        return ipv6_regexp.match(ip) is not None

    def execute(cmd, ignore_error=False):
        cmd = ' '.join(map(itemgetter(-1), filter(itemgetter(0), cmd)))

        cmds.append(cmd)
        (rc, out, err) = module.run_command(cmd, environ_update={"LANG": "C"})

        if rc != 0 and not ignore_error:
            module.fail_json(msg=err or out, commands=cmds)

        return out

    def get_current_rules():
        user_rules_files = [
            "/lib/ufw/user.rules", "/lib/ufw/user6.rules",
            "/etc/ufw/user.rules", "/etc/ufw/user6.rules",
            "/var/lib/ufw/user.rules", "/var/lib/ufw/user6.rules"
        ]

        cmd = [[grep_bin], ["-h"], ["'^### tuple'"]]

        cmd.extend([[f] for f in user_rules_files])
        return execute(cmd, ignore_error=True)

    def ufw_version():
        """
        Returns the major and minor version of ufw installed on the system.
        """
        out = execute([[ufw_bin], ["--version"]])

        lines = [x for x in out.split('\n') if x.strip() != '']
        if len(lines) == 0:
            module.fail_json(msg="Failed to get ufw version.", rc=0, out=out)

        matches = re.search(r'^ufw.+(\d+)\.(\d+)(?:\.(\d+))?.*$', lines[0])
        if matches is None:
            module.fail_json(msg="Failed to get ufw version.", rc=0, out=out)

        # Convert version to numbers
        major = int(matches.group(1))
        minor = int(matches.group(2))
        rev = 0
        if matches.group(3) is not None:
            rev = int(matches.group(3))

        return major, minor, rev

    params = module.params

    commands = dict((key, params[key]) for key in command_keys if params[key])

    # Ensure ufw is available
    ufw_bin = module.get_bin_path('ufw', True)
    grep_bin = module.get_bin_path('grep', True)

    # Save the pre state and rules in order to recognize changes
    pre_state = execute([[ufw_bin], ['status verbose']])
    pre_rules = get_current_rules()

    changed = False

    # Execute filter
    for (command, value) in commands.items():

        cmd = [[ufw_bin], [module.check_mode, '--dry-run']]

        if command == 'state':
            states = {
                'enabled': 'enable',
                'disabled': 'disable',
                'reloaded': 'reload',
                'reset': 'reset'
            }

            if value in ['reloaded', 'reset']:
                changed = True

            if module.check_mode:
                # "active" would also match "inactive", hence the space
                ufw_enabled = pre_state.find(" active") != -1
                if (value == 'disabled'
                        and ufw_enabled) or (value == 'enabled'
                                             and not ufw_enabled):
                    changed = True
            else:
                execute(cmd + [['-f'], [states[value]]])

        elif command == 'logging':
            extract = re.search(r'Logging: (on|off)(?: \(([a-z]+)\))?',
                                pre_state)
            if extract:
                current_level = extract.group(2)
                current_on_off_value = extract.group(1)
                if value != "off":
                    if current_on_off_value == "off":
                        changed = True
                    elif value != "on" and value != current_level:
                        changed = True
                elif current_on_off_value != "off":
                    changed = True
            else:
                changed = True

            if not module.check_mode:
                execute(cmd + [[command], [value]])

        elif command == 'default':
            if params['direction'] not in [
                    'outgoing', 'incoming', 'routed', None
            ]:
                module.fail_json(
                    msg=
                    'For default, direction must be one of "outgoing", "incoming" and "routed", or direction must not be specified.'
                )
            if module.check_mode:
                regexp = r'Default: (deny|allow|reject) \(incoming\), (deny|allow|reject) \(outgoing\), (deny|allow|reject|disabled) \(routed\)'
                extract = re.search(regexp, pre_state)
                if extract is not None:
                    current_default_values = {}
                    current_default_values["incoming"] = extract.group(1)
                    current_default_values["outgoing"] = extract.group(2)
                    current_default_values["routed"] = extract.group(3)
                    v = current_default_values[params['direction']
                                               or 'incoming']
                    if v not in (value, 'disabled'):
                        changed = True
                else:
                    changed = True
            else:
                execute(cmd + [[command], [value], [params['direction']]])

        elif command == 'rule':
            if params['direction'] not in ['in', 'out', None]:
                module.fail_json(
                    msg=
                    'For rules, direction must be one of "in" and "out", or direction must not be specified.'
                )
            if not params['route'] and params['interface_in'] and params[
                    'interface_out']:
                module.fail_json(msg='Only route rules can combine '
                                 'interface_in and interface_out')
            # Rules are constructed according to the long format
            #
            # ufw [--dry-run] [route] [delete] [insert NUM] allow|deny|reject|limit [in|out on INTERFACE] [log|log-all] \
            #     [from ADDRESS [port PORT]] [to ADDRESS [port PORT]] \
            #     [proto protocol] [app application] [comment COMMENT]
            cmd.append([module.boolean(params['route']), 'route'])
            cmd.append([module.boolean(params['delete']), 'delete'])
            if params['insert'] is not None:
                relative_to_cmd = params['insert_relative_to']
                if relative_to_cmd == 'zero':
                    insert_to = params['insert']
                else:
                    (dummy, numbered_state, dummy) = module.run_command(
                        [ufw_bin, 'status', 'numbered'])
                    numbered_line_re = re.compile(R'^\[ *([0-9]+)\] ')
                    lines = [(numbered_line_re.match(line), '(v6)' in line)
                             for line in numbered_state.splitlines()]
                    lines = [(int(matcher.group(1)), ipv6)
                             for (matcher, ipv6) in lines if matcher]
                    last_number = max([no for (no,
                                               ipv6) in lines]) if lines else 0
                    has_ipv4 = any([not ipv6 for (no, ipv6) in lines])
                    has_ipv6 = any([ipv6 for (no, ipv6) in lines])
                    if relative_to_cmd == 'first-ipv4':
                        relative_to = 1
                    elif relative_to_cmd == 'last-ipv4':
                        relative_to = max(
                            [no for (no, ipv6) in lines
                             if not ipv6]) if has_ipv4 else 1
                    elif relative_to_cmd == 'first-ipv6':
                        relative_to = max(
                            [no for (no, ipv6) in lines
                             if not ipv6]) + 1 if has_ipv4 else 1
                    elif relative_to_cmd == 'last-ipv6':
                        relative_to = last_number if has_ipv6 else last_number + 1
                    insert_to = params['insert'] + relative_to
                    if insert_to > last_number:
                        # ufw does not like it when the insert number is larger than the
                        # maximal rule number for IPv4/IPv6.
                        insert_to = None
                cmd.append([insert_to is not None, "insert %s" % insert_to])
            cmd.append([value])
            cmd.append([params['direction'], "%s" % params['direction']])
            cmd.append([params['interface'], "on %s" % params['interface']])
            cmd.append(
                [params['interface_in'],
                 "in on %s" % params['interface_in']])
            cmd.append([
                params['interface_out'],
                "out on %s" % params['interface_out']
            ])
            cmd.append([module.boolean(params['log']), 'log'])

            for (key, template) in [('from_ip', "from %s"),
                                    ('from_port', "port %s"),
                                    ('to_ip', "to %s"), ('to_port', "port %s"),
                                    ('proto', "proto %s"),
                                    ('name', "app '%s'")]:
                value = params[key]
                cmd.append([value, template % (value)])

            ufw_major, ufw_minor, dummy = ufw_version()
            # comment is supported only in ufw version after 0.35
            if (ufw_major == 0 and ufw_minor >= 35) or ufw_major > 0:
                cmd.append(
                    [params['comment'],
                     "comment '%s'" % params['comment']])

            rules_dry = execute(cmd)

            if module.check_mode:

                nb_skipping_line = len(
                    filter_line_that_contains("Skipping", rules_dry))

                if not (nb_skipping_line > 0 and nb_skipping_line == len(
                        rules_dry.splitlines(True))):

                    rules_dry = filter_line_that_not_start_with(
                        "### tuple", rules_dry)
                    # ufw dry-run doesn't send all rules so have to compare ipv4 or ipv6 rules
                    if is_starting_by_ipv4(
                            params['from_ip']) or is_starting_by_ipv4(
                                params['to_ip']):
                        if filter_line_that_contains_ipv4(
                                pre_rules) != filter_line_that_contains_ipv4(
                                    rules_dry):
                            changed = True
                    elif is_starting_by_ipv6(
                            params['from_ip']) or is_starting_by_ipv6(
                                params['to_ip']):
                        if filter_line_that_contains_ipv6(
                                pre_rules) != filter_line_that_contains_ipv6(
                                    rules_dry):
                            changed = True
                    elif pre_rules != rules_dry:
                        changed = True

    # Get the new state
    if module.check_mode:
        return module.exit_json(changed=changed, commands=cmds)
    else:
        post_state = execute([[ufw_bin], ['status'], ['verbose']])
        if not changed:
            post_rules = get_current_rules()
            changed = (pre_state != post_state) or (pre_rules != post_rules)
        return module.exit_json(changed=changed,
                                commands=cmds,
                                msg=post_state.rstrip())
示例#29
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            target=dict(type='str'),
            params=dict(type='dict'),
            chdir=dict(type='path', required=True),
            file=dict(type='path'),
        ),
        supports_check_mode=True,
    )
    # Build up the invocation of `make` we are going to use
    # For non-Linux OSes, prefer gmake (GNU make) over make
    make_path = module.get_bin_path('gmake', required=False)
    if not make_path:
        # Fall back to system make
        make_path = module.get_bin_path('make', required=True)
    make_target = module.params['target']
    if module.params['params'] is not None:
        make_parameters = [
            k + '=' + str(v) for k, v in iteritems(module.params['params'])
        ]
    else:
        make_parameters = []

    if module.params['file'] is not None:
        base_command = [make_path, "-f", module.params['file'], make_target]
    else:
        base_command = [make_path, make_target]
    base_command.extend(make_parameters)

    # Check if the target is already up to date
    rc, out, err = run_command(base_command + ['--question'],
                               module,
                               check_rc=False)
    if module.check_mode:
        # If we've been asked to do a dry run, we only need
        # to report whether or not the target is up to date
        changed = (rc != 0)
    else:
        if rc == 0:
            # The target is up to date, so we don't have to
            #  do anything
            changed = False
        else:
            # The target isn't up to date, so we need to run it
            rc, out, err = run_command(base_command, module, check_rc=True)
            changed = True

    # We don't report the return code, as if this module failed
    # we would be calling fail_json from run_command, so even if
    # we had a non-zero return code, we did not fail. However, if
    # we report a non-zero return code here, we will be marked as
    # failed regardless of what we signal using the failed= kwarg.
    module.exit_json(changed=changed,
                     failed=False,
                     stdout=out,
                     stderr=err,
                     target=module.params['target'],
                     params=module.params['params'],
                     chdir=module.params['chdir'],
                     file=module.params['file'])
示例#30
0
def main():

    module = AnsibleModule(
        argument_spec=dict(
            command=dict(required=False, default=None, choices=[
                'ping', 'kv_test', 'join', 'plan', 'commit']),
            config_dir=dict(default='/etc/riak', type='path'),
            http_conn=dict(required=False, default='127.0.0.1:8098'),
            target_node=dict(default='[email protected]', required=False),
            wait_for_handoffs=dict(default=False, type='int'),
            wait_for_ring=dict(default=False, type='int'),
            wait_for_service=dict(
                required=False, default=None, choices=['kv']),
            validate_certs=dict(default='yes', type='bool'))
    )

    command = module.params.get('command')
    http_conn = module.params.get('http_conn')
    target_node = module.params.get('target_node')
    wait_for_handoffs = module.params.get('wait_for_handoffs')
    wait_for_ring = module.params.get('wait_for_ring')
    wait_for_service = module.params.get('wait_for_service')

    # make sure riak commands are on the path
    riak_bin = module.get_bin_path('riak')
    riak_admin_bin = module.get_bin_path('riak-admin')

    timeout = time.time() + 120
    while True:
        if time.time() > timeout:
            module.fail_json(msg='Timeout, could not fetch Riak stats.')
        (response, info) = fetch_url(module, 'http://%s/stats' % (http_conn), force=True, timeout=5)
        if info['status'] == 200:
            stats_raw = response.read()
            break
        time.sleep(5)

    # here we attempt to load those stats,
    try:
        stats = json.loads(stats_raw)
    except Exception:
        module.fail_json(msg='Could not parse Riak stats.')

    node_name = stats['nodename']
    nodes = stats['ring_members']
    ring_size = stats['ring_creation_size']
    rc, out, err = module.run_command([riak_bin, 'version'])
    version = out.strip()

    result = dict(node_name=node_name,
                  nodes=nodes,
                  ring_size=ring_size,
                  version=version)

    if command == 'ping':
        cmd = '%s ping %s' % (riak_bin, target_node)
        rc, out, err = module.run_command(cmd)
        if rc == 0:
            result['ping'] = out
        else:
            module.fail_json(msg=out)

    elif command == 'kv_test':
        cmd = '%s test' % riak_admin_bin
        rc, out, err = module.run_command(cmd)
        if rc == 0:
            result['kv_test'] = out
        else:
            module.fail_json(msg=out)

    elif command == 'join':
        if nodes.count(node_name) == 1 and len(nodes) > 1:
            result['join'] = 'Node is already in cluster or staged to be in cluster.'
        else:
            cmd = '%s cluster join %s' % (riak_admin_bin, target_node)
            rc, out, err = module.run_command(cmd)
            if rc == 0:
                result['join'] = out
                result['changed'] = True
            else:
                module.fail_json(msg=out)

    elif command == 'plan':
        cmd = '%s cluster plan' % riak_admin_bin
        rc, out, err = module.run_command(cmd)
        if rc == 0:
            result['plan'] = out
            if 'Staged Changes' in out:
                result['changed'] = True
        else:
            module.fail_json(msg=out)

    elif command == 'commit':
        cmd = '%s cluster commit' % riak_admin_bin
        rc, out, err = module.run_command(cmd)
        if rc == 0:
            result['commit'] = out
            result['changed'] = True
        else:
            module.fail_json(msg=out)

# this could take a while, recommend to run in async mode
    if wait_for_handoffs:
        timeout = time.time() + wait_for_handoffs
        while True:
            cmd = '%s transfers' % riak_admin_bin
            rc, out, err = module.run_command(cmd)
            if 'No transfers active' in out:
                result['handoffs'] = 'No transfers active.'
                break
            time.sleep(10)
            if time.time() > timeout:
                module.fail_json(msg='Timeout waiting for handoffs.')

    if wait_for_service:
        cmd = [riak_admin_bin, 'wait_for_service', 'riak_%s' % wait_for_service, node_name]
        rc, out, err = module.run_command(cmd)
        result['service'] = out

    if wait_for_ring:
        timeout = time.time() + wait_for_ring
        while True:
            if ring_check(module, riak_admin_bin):
                break
            time.sleep(10)
        if time.time() > timeout:
            module.fail_json(msg='Timeout waiting for nodes to agree on ring.')

    result['ring_ready'] = ring_check(module, riak_admin_bin)

    module.exit_json(**result)