def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), locations=dict(type='list'), organizations=dict(type='list'), operatingsystems=dict(type='list'), os_family=dict(), path=dict(), ), supports_check_mode=True, ) (server_params, medium_dict, state) = module.parse_params() try: (server_url, username, password, verify_ssl) = server_params create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg="Failed to connect to Foreman server: %s " % e) ping_server(module) entity = find_installation_medium(module, name=medium_dict['name'], failsafe=True) if 'operatingsystems' in medium_dict: medium_dict['operatingsystems'] = find_operating_systems_by_title( module, medium_dict['operatingsystems']) if len(medium_dict['operatingsystems'] ) == 1 and 'os_family' not in medium_dict and entity is None: medium_dict['os_family'] = medium_dict['operatingsystems'][ 0].family if 'locations' in medium_dict: medium_dict['locations'] = find_locations(module, medium_dict['locations']) if 'organizations' in medium_dict: medium_dict['organizations'] = find_organizations( module, medium_dict['organizations']) medium_dict = sanitize_entity_dict(medium_dict, name_map) changed = naildown_entity_state(Media, medium_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), locations=dict(type='list'), organizations=dict(type='list'), operatingsystems=dict(type='list'), os_family=dict(), path=dict(), ), supports_check_mode=True, ) (medium_dict, state) = module.parse_params() module.connect() entity = find_installation_medium(module, name=medium_dict['name'], failsafe=True) if 'operatingsystems' in medium_dict: medium_dict['operatingsystems'] = find_operating_systems_by_title( module, medium_dict['operatingsystems']) if len(medium_dict['operatingsystems'] ) == 1 and 'os_family' not in medium_dict and entity is None: medium_dict['os_family'] = medium_dict['operatingsystems'][ 0].family if 'locations' in medium_dict: medium_dict['locations'] = find_locations(module, medium_dict['locations']) if 'organizations' in medium_dict: medium_dict['organizations'] = find_organizations( module, medium_dict['organizations']) medium_dict = sanitize_entity_dict(medium_dict, name_map) changed = naildown_entity_state(Media, medium_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = AnsibleModule( argument_spec=dict( server_url=dict(required=True), username=dict(required=True), password=dict(required=True, no_log=True), verify_ssl=dict(type='bool', default=True), name=dict(required=True), locations=dict(type='list'), organizations=dict(type='list'), operatingsystems=dict(type='list'), os_family=dict(), path=dict(), state=dict(choices=['present', 'absent'], default='present'), ), supports_check_mode=True, ) if has_import_error: module.fail_json(msg=import_error_msg) medium_dict = dict([(k, v) for (k, v) in module.params.items() if v is not None]) server_url = medium_dict.pop('server_url') username = medium_dict.pop('username') password = medium_dict.pop('password') verify_ssl = medium_dict.pop('verify_ssl') state = medium_dict.pop('state') try: create_server(server_url, (username, password), verify_ssl) except Exception as e: module.fail_json(msg="Failed to connect to Foreman server: %s " % e) ping_server(module) entity = find_installation_medium(module, name=medium_dict['name'], failsafe=True) if 'operatingsystems' in medium_dict: medium_dict['operatingsystems'] = find_operating_systems_by_title( module, medium_dict['operatingsystems']) if len(medium_dict['operatingsystems'] ) == 1 and 'os_family' not in medium_dict and entity is None: medium_dict['os_family'] = medium_dict['operatingsystems'][ 0].family if 'locations' in medium_dict: medium_dict['locations'] = find_locations(module, medium_dict['locations']) if 'organizations' in medium_dict: medium_dict['organizations'] = find_organizations( module, medium_dict['organizations']) medium_dict = sanitize_entity_dict(medium_dict, name_map) changed = naildown_entity_state(Media, medium_dict, entity, state, module) module.exit_json(changed=changed)
def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( name=dict(required=True), locations=dict(type='list'), organizations=dict(type='list'), operatingsystems=dict(type='list'), os_family=dict(), path=dict(), ), supports_check_mode=True, ) (medium_dict, state) = module.parse_params() module.connect() name = medium_dict['name'] affects_multiple = name == '*' # sanitize user input, filter unuseful configuration combinations with 'name: *' if affects_multiple: if state == 'present_with_defaults': module.fail_json( msg= "'state: present_with_defaults' and 'name: *' cannot be used together" ) if state == 'absent': if list(medium_dict.keys()) != ['name']: module.fail_json( msg= 'When deleting all installation media, there is no need to specify further parameters %s ' % medium_dict.keys()) try: if affects_multiple: entities = find_entities(Media) else: entities = find_installation_medium(module, name=medium_dict['name'], failsafe=True) except Exception as e: module.fail_json(msg='Failed to find entity: %s ' % e) if 'operatingsystems' in medium_dict: medium_dict['operatingsystems'] = find_operating_systems_by_title( module, medium_dict['operatingsystems']) if len(medium_dict['operatingsystems'] ) == 1 and 'os_family' not in medium_dict and entities is None: medium_dict['os_family'] = medium_dict['operatingsystems'][ 0].family if 'locations' in medium_dict: medium_dict['locations'] = find_locations(module, medium_dict['locations']) if 'organizations' in medium_dict: medium_dict['organizations'] = find_organizations( module, medium_dict['organizations']) medium_dict = sanitize_entity_dict(medium_dict, name_map) changed = False if not affects_multiple: changed = naildown_entity_state(Media, medium_dict, entities, state, module) else: medium_dict.pop('name') for entity in entities: changed |= naildown_entity_state(Media, medium_dict, entity, state, module) module.exit_json(changed=changed)