def get_conn(opts, profile):

    '''
    Return a client object for accessing consul
    '''
    opts_pillar = opts.get('pillar', {})
    opts_master = opts_pillar.get('master', {})

    opts_merged = {}
    opts_merged.update(opts_master)
    opts_merged.update(opts_pillar)
    opts_merged.update(opts)

    if profile:
        conf = opts_merged.get(profile, {})
    else:
        conf = opts_merged

    params = {}
    for key in conf:
        if key.startswith('consul.'):
            params[key.split('.')[1]] = conf[key]

    if 'dc' in params:
        pillarenv = opts_merged.get('pillarenv') or 'base'
        params['dc'] = _resolve_datacenter(params['dc'], pillarenv)

    if consul:
        # Sanity check. ACL Tokens are supported on python-consul 0.4.7 onwards only.
        if consul.__version__ < '0.4.7' and params.get('target'):
            params.pop('target')
        return consul.Consul(**params)
    else:
        raise CommandExecutionError(
            '(unable to import consul, '
            'module most likely not installed. Download python-consul '
            'module and be sure to import consul)'
        )