Esempio n. 1
0
    def execute_module(self):
        gather_subset = self.params['gather_subset']
        gather_timeout = self.params['gather_timeout']
        filter_spec = self.params['filter']

        minimal_gather_subset = \
            JenkinsSSHCLIFactCollector.minimal_gather_subset
        all_collector_classes = \
            JenkinsSSHCLIFactCollector.all_collector_classes

        namespace = PrefixFactNamespace(
            namespace_name='jenkins', prefix='jenkins_'
        )

        fact_collector = ansible_collector.get_ansible_collector(
            all_collector_classes=all_collector_classes,
            namespace=namespace,
            filter_spec=filter_spec,
            gather_subset=gather_subset,
            gather_timeout=gather_timeout,
            minimal_gather_subset=minimal_gather_subset
        )

        facts_dict = fact_collector.collect(module=self)

        self.exit_json(jenkins_facts=facts_dict)
Esempio n. 2
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            gather_subset=dict(default=["all"],
                               required=False,
                               type='list',
                               elements='str'),
            gather_timeout=dict(default=10, required=False, type='int'),
            filter=dict(default=[],
                        required=False,
                        type='list',
                        elements='str'),
            fact_path=dict(default='/etc/ansible/facts.d',
                           required=False,
                           type='path'),
        ),
        supports_check_mode=True,
    )

    gather_subset = module.params['gather_subset']
    gather_timeout = module.params['gather_timeout']
    filter_spec = module.params['filter']

    # TODO: this mimics existing behavior where gather_subset=["!all"] actually means
    #       to collect nothing except for the below list
    # TODO: decide what '!all' means, I lean towards making it mean none, but likely needs
    #       some tweaking on how gather_subset operations are performed
    minimal_gather_subset = frozenset([
        'apparmor', 'caps', 'cmdline', 'date_time', 'distribution', 'dns',
        'env', 'fips', 'local', 'lsb', 'pkg_mgr', 'platform', 'python',
        'selinux', 'service_mgr', 'ssh_pub_keys', 'user'
    ])

    all_collector_classes = default_collectors.collectors

    # rename namespace_name to root_key?
    namespace = PrefixFactNamespace(namespace_name='ansible',
                                    prefix='ansible_')

    try:
        fact_collector = ansible_collector.get_ansible_collector(
            all_collector_classes=all_collector_classes,
            namespace=namespace,
            filter_spec=filter_spec,
            gather_subset=gather_subset,
            gather_timeout=gather_timeout,
            minimal_gather_subset=minimal_gather_subset)
    except (TypeError, CollectorNotFoundError, CycleFoundInFactDeps,
            UnresolvedFactDep) as e:
        # bad subset given, collector, idk, deps declared but not found
        module.fail_json(msg=to_text(e))

    facts_dict = fact_collector.collect(module=module)

    module.exit_json(ansible_facts=facts_dict)
Esempio n. 3
0
    def __init__(self):
        collector = ansible_collector.get_ansible_collector(all_collector_classes=default_collectors.collectors,
                                                            filter_spec='default_ipv4',
                                                            gather_subset=['!all', 'network'],
                                                            gather_timeout=10)
        self.facts = collector.collect(module)

        self.api_ip = None
        self.fact_paths = {
            'cloudstack_service_offering': 'service-offering',
            'cloudstack_availability_zone': 'availability-zone',
            'cloudstack_public_hostname': 'public-hostname',
            'cloudstack_public_ipv4': 'public-ipv4',
            'cloudstack_local_hostname': 'local-hostname',
            'cloudstack_local_ipv4': 'local-ipv4',
            'cloudstack_instance_id': 'instance-id'
        }
Esempio n. 4
0
    def __init__(self):
        collector = ansible_collector.get_ansible_collector(all_collector_classes=default_collectors.collectors,
                                                            filter_spec='default_ipv4',
                                                            gather_subset=['!all', 'network'],
                                                            gather_timeout=10)
        self.facts = collector.collect(module)

        self.api_ip = None
        self.fact_paths = {
            'cloudstack_service_offering': 'service-offering',
            'cloudstack_availability_zone': 'availability-zone',
            'cloudstack_public_hostname': 'public-hostname',
            'cloudstack_public_ipv4': 'public-ipv4',
            'cloudstack_local_hostname': 'local-hostname',
            'cloudstack_local_ipv4': 'local-ipv4',
            'cloudstack_instance_id': 'instance-id'
        }
Esempio n. 5
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            gather_subset=dict(default=["all"], required=False, type='list'),
            gather_timeout=dict(default=10, required=False, type='int'),
            filter=dict(default="*", required=False),
            fact_path=dict(default='/etc/ansible/facts.d', required=False, type='path'),
        ),
        supports_check_mode=True,
    )

    gather_subset = module.params['gather_subset']
    gather_timeout = module.params['gather_timeout']
    filter_spec = module.params['filter']

    # TODO: this mimics existing behavior where gather_subset=["!all"] actually means
    #       to collect nothing except for the below list
    # TODO: decide what '!all' means, I lean towards making it mean none, but likely needs
    #       some tweaking on how gather_subset operations are performed
    minimal_gather_subset = frozenset(['apparmor', 'caps', 'cmdline', 'date_time',
                                       'distribution', 'dns', 'env', 'fips', 'local', 'lsb',
                                       'pkg_mgr', 'platform', 'python', 'selinux',
                                       'service_mgr', 'ssh_pub_keys', 'user'])

    all_collector_classes = default_collectors.collectors

    # rename namespace_name to root_key?
    namespace = PrefixFactNamespace(namespace_name='ansible',
                                    prefix='ansible_')

    fact_collector = \
        ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes,
                                                namespace=namespace,
                                                filter_spec=filter_spec,
                                                gather_subset=gather_subset,
                                                gather_timeout=gather_timeout,
                                                minimal_gather_subset=minimal_gather_subset)

    facts_dict = fact_collector.collect(module=module)

    module.exit_json(ansible_facts=facts_dict)
def ansible_facts(module, gather_subset=None):
    '''Compat api for ansible 2.0/2.2/2.3 module_utils.facts.ansible_facts method

    2.3/2.3 expects a gather_subset arg.
    2.0/2.1 does not except a gather_subset arg

    So make gather_subsets an optional arg, defaulting to configured DEFAULT_GATHER_TIMEOUT

    'module' should be an instance of an AnsibleModule.

    returns a dict mapping the bare fact name ('default_ipv4' with no 'ansible_' namespace) to
    the fact value.
    '''

    gather_subset = gather_subset or module.params.get('gather_subset',
                                                       ['all'])
    gather_timeout = module.params.get('gather_timeout', 10)
    filter_spec = module.params.get('filter', '*')

    minimal_gather_subset = frozenset([
        'apparmor', 'caps', 'cmdline', 'date_time', 'distribution', 'dns',
        'env', 'fips', 'local', 'lsb', 'pkg_mgr', 'platform', 'python',
        'selinux', 'service_mgr', 'ssh_pub_keys', 'user'
    ])

    all_collector_classes = default_collectors.collectors

    # don't add a prefix
    namespace = PrefixFactNamespace(namespace_name='ansible', prefix='')

    fact_collector = \
        ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes,
                                                namespace=namespace,
                                                filter_spec=filter_spec,
                                                gather_subset=gather_subset,
                                                gather_timeout=gather_timeout,
                                                minimal_gather_subset=minimal_gather_subset)

    facts_dict = fact_collector.collect(module=module)

    return facts_dict
Esempio n. 7
0
def ansible_facts(module, gather_subset=None):
    '''Compat api for ansible 2.0/2.2/2.3 module_utils.facts.ansible_facts method

    2.3/2.3 expects a gather_subset arg.
    2.0/2.1 does not except a gather_subset arg

    So make gather_subsets an optional arg, defaulting to configured DEFAULT_GATHER_TIMEOUT

    'module' should be an instance of an AnsibleModule.

    returns a dict mapping the bare fact name ('default_ipv4' with no 'ansible_' namespace) to
    the fact value.
    '''

    gather_subset = gather_subset or module.params.get('gather_subset', ['all'])
    gather_timeout = module.params.get('gather_timeout', 10)
    filter_spec = module.params.get('filter', '*')

    minimal_gather_subset = frozenset(['apparmor', 'caps', 'cmdline', 'date_time',
                                       'distribution', 'dns', 'env', 'fips', 'local', 'lsb',
                                       'pkg_mgr', 'platform', 'python', 'selinux',
                                       'service_mgr', 'ssh_pub_keys', 'user'])

    all_collector_classes = default_collectors.collectors

    # don't add a prefix
    namespace = PrefixFactNamespace(namespace_name='ansible',
                                    prefix='')

    fact_collector = \
        ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes,
                                                namespace=namespace,
                                                filter_spec=filter_spec,
                                                gather_subset=gather_subset,
                                                gather_timeout=gather_timeout,
                                                minimal_gather_subset=minimal_gather_subset)

    facts_dict = fact_collector.collect(module=module)

    return facts_dict
Esempio n. 8
0
def run_module():

    valid_roles = set(['mons', 'mdss', 'osds', 'rgws', 'mgrs', 'iscsigws'])

    fields = dict(role=dict(type='str', required=True),
                  mode=dict(type='str',
                            choices=['prod', 'dev'],
                            default='prod',
                            required=False),
                  deployment=dict(type='str',
                                  choices=['container', 'rpm'],
                                  default='container',
                                  required=False),
                  osdtype=dict(type='str',
                               choices=['bluestore', 'filestore'],
                               default='bluestore',
                               required=False),
                  flashusage=dict(type='str',
                                  choices=['journal', 'data'],
                                  default='journal',
                                  required=False))

    module = AnsibleModule(argument_spec=fields, supports_check_mode=False)

    flash_usage = module.params.get('flashusage')
    osd_type = module.params.get('osdtype')
    mode = module.params.get('mode')
    deployment_type = module.params.get('deployment')
    role = module.params.get('role')

    role_list = role.split(',')
    # If the host has an undefined role, it's in the ansible inventory, but not in the
    # inventory we've received. This typically happens for the metrics host (ceph-grafana)
    if role_list == ['undefined']:
        module.exit_json(changed=False,
                         msg="host doesn't have a ceph role, nothing to do")

    # abort if the roles provided don't match with the defaults
    if not all(role in valid_roles for role in role_list):
        module.fail_json(msg="Invalid roles specfified. Must be {}".format(
            ','.join(valid_roles)))

    # Define the ansible collector logic, as used by the ansible "setup" module
    all_collector_classes = default_collectors.collectors
    minimal_gather_subset = frozenset([
        'apparmor', 'caps', 'cmdline', 'date_time', 'distribution', 'dns',
        'env', 'fips', 'local', 'lsb', 'pkg_mgr', 'platform', 'python',
        'selinux', 'service_mgr', 'ssh_pub_keys', 'user'
    ])

    namespace = PrefixFactNamespace(namespace_name='ansible',
                                    prefix='ansible_')

    fact_collector = \
        ansible_collector.get_ansible_collector(all_collector_classes=all_collector_classes,
                                                namespace=namespace,
                                                filter_spec="*",
                                                gather_subset=['all'],
                                                gather_timeout=10,
                                                minimal_gather_subset=minimal_gather_subset)

    # Get the facts from the host
    ansible_facts = fact_collector.collect(module=module)

    summary = summarize(ansible_facts)
    checker = Checker(host_details=summary,
                      roles=role,
                      deployment_type=deployment_type,
                      mode=mode,
                      flash_usage=flash_usage,
                      osd_type=osd_type)
    checker.analyse()

    module.exit_json(changed=False,
                     data={
                         "role": role,
                         "mode": mode,
                         "osdtype": osd_type,
                         "flashusage": flash_usage,
                         "deployment_type": deployment_type,
                         'summary_facts': summary,
                         'status_msgs': sorted(checker.status_msgs),
                         'status': checker.state
                     })
Esempio n. 9
0
def core(module):
    architecture = module.params['architecture']
    conflicts = module.params['conflicts']
    depends = module.params['depends']
    description = module.params['description']
    enhances = module.params['enhances']
    maintainer = module.params['maintainer']
    manager = module.params['manager']
    name = module.params['name']
    recommends = module.params['recommends']
    state = module.params['state']
    suggests = module.params['suggests']
    summary = module.params['summary']
    version = module.params['version']

    if not maintainer:
        pwuid = pwd.getpwuid(os.getuid())

        username = pwuid.pw_name

        if pwuid.pw_gecos:
            fullname = pwuid.pw_gecos.split(',')[0]
        else:
            # fallback to username
            fullname = username

        fqdn = socket.getfqdn()

        maintainer = '{fullname} <{username}@{fqdn}>'.format(fullname=fullname, username=username, fqdn=fqdn)

    if manager == 'auto':
        # Inspired by https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/setup.py
        namespace = PrefixFactNamespace(namespace_name='ansible', prefix='ansible_')

        fact_collector = \
            ansible_collector.get_ansible_collector(all_collector_classes=default_collectors.collectors,
                                                    namespace=namespace,
                                                    filter_spec='*',
                                                    gather_subset=['pkg_mgr'],
                                                    minimal_gather_subset=frozenset(['pkg_mgr']))

        facts_dict = fact_collector.collect(module=module)
        manager = facts_dict['ansible_pkg_mgr']

    if manager not in ['apt', 'dnf', 'yum']:
        raise ValueError('manager %s is not supported' % manager)

    if manager == 'apt' and not HAS_APT:
        module.fail_json(msg=missing_required_lib("apt"), exception=APT_IMPORT_ERROR)

    if manager in ['dnf', 'yum'] and not HAS_BABEL:
        module.fail_json(msg=missing_required_lib("babel"), exception=BABEL_IMPORT_ERROR)

    if manager == 'dnf' and not HAS_DNF:
        module.fail_json(msg=missing_required_lib("dnf"), exception=DNF_IMPORT_ERROR)

    if manager == 'yum' and not HAS_YUM:
        module.fail_json(msg=missing_required_lib("yum"), exception=YUM_IMPORT_ERROR)

    if not architecture:
        if manager == 'apt':
            architecture = 'all'
        elif manager in ['yum', 'dnf']:
            architecture = 'noarch'

    if module.check_mode:
        return dict(
            changed=False,
            architecture=architecture,
            conflicts=conflicts,
            depends=depends,
            description=description,
            enhances=enhances,
            maintainer=maintainer,
            manager=manager,
            name=name,
            recommends=recommends,
            state=state,
            suggests=suggests,
            summary=summary,
            version=version)

    if state == 'present':
        changed, conflicts, depends, enhances, recommends, suggests = install(
            architecture,
            conflicts,
            depends,
            description,
            enhances,
            maintainer,
            manager,
            name,
            recommends,
            suggests,
            summary,
            version,
            module)
    elif state == 'absent':
        changed = remove(
            manager,
            name,
            module)

    return dict(
        changed=changed,
        architecture=architecture,
        conflicts=conflicts,
        depends=depends,
        description=description,
        enhances=enhances,
        maintainer=maintainer,
        manager=manager,
        name=name,
        recommends=recommends,
        state=state,
        suggests=suggests,
        summary=summary,
        version=version)