def main(): module = ForemanEntityAnsibleModule( argument_spec=dict( host=dict(required=True), state=dict(default='present', choices=['present', 'absent', 'reverted']), ), entity_spec=dict( name=dict(required=True), description=dict(), ), ) snapshot_dict = module.clean_params() with module.api_connection(): host = module.find_resource_by_name('hosts', name=snapshot_dict['host'], failsafe=False, thin=True) scope = {'host_id': host['id']} entity = module.find_resource_by_name('snapshots', name=snapshot_dict['name'], params=scope, failsafe=True) module.ensure_entity('snapshots', snapshot_dict, entity, params=scope)
def main(): module = ForemanEntityAnsibleModule( foreman_spec=dict( name=dict(aliases=['hostname'], required=True), ), argument_spec=dict( state=dict(default='state', choices=['on', 'start', 'off', 'stop', 'soft', 'reboot', 'cycle', 'reset', 'state', 'status']), ) ) module_params = module.foreman_params with module.api_connection(): # power_status endpoint was only added in foreman 1.22.0 per https://projects.theforeman.org/issues/25436 # Delete this piece when versions below 1.22 are off common use # begin delete if 'power_status' not in module.foremanapi.resource('hosts').actions: params = {'id': module_params['name'], 'power_action': 'status'} power_state = module.resource_action('hosts', 'power', params=params, ignore_check_mode=True) power_state['state'] = 'on' if power_state['power'] == 'running' else 'off' else: # end delete (on delete un-indent the below two lines) params = {'id': module_params['name']} power_state = module.resource_action('hosts', 'power_status', params=params, ignore_check_mode=True) if module.state in ['state', 'status']: module.exit_json(power_state=power_state['state']) elif ((module.state in ['on', 'start'] and power_state['state'] == 'on') or (module.state in ['off', 'stop'] and power_state['state'] == 'off')): module.exit_json() else: params['power_action'] = module.state module.resource_action('hosts', 'power', params=params)