How can I let anonymous users view user profiles by role? I have 2 different roles like A and B. 

I want anonymous users (everyone) can view user profiles from A role but can't view user profiles from B role. 

In summary, A role profiles is open for everyone but B is not.

Access user profiles permission or View profiles permissions module is not enough for that.

Thanks in advance.
You can use Views to achieve this. Under Page Settings change Access to Role &amp; select Anonymous users from the list. Check exported view shown below:

$view = new view();
$view-&gt;name = 'user_profiles';
$view-&gt;description = '';
$view-&gt;tag = 'default';
$view-&gt;base_table = 'users';
$view-&gt;human_name = 'User Profiles';
$view-&gt;core = 7;
$view-&gt;api_version = '3.0';
$view-&gt;disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view-&gt;new_display('default', 'Master', 'default');
$handler-&gt;display-&gt;display_options['title'] = 'User Profiles';
$handler-&gt;display-&gt;display_options['use_more_always'] = FALSE;
$handler-&gt;display-&gt;display_options['access']['type'] = 'role';
$handler-&gt;display-&gt;display_options['access']['role'] = array(
  1 =&gt; '1',
);
$handler-&gt;display-&gt;display_options['cache']['type'] = 'none';
$handler-&gt;display-&gt;display_options['query']['type'] = 'views_query';
$handler-&gt;display-&gt;display_options['exposed_form']['type'] = 'basic';
$handler-&gt;display-&gt;display_options['pager']['type'] = 'full';
$handler-&gt;display-&gt;display_options['pager']['options']['items_per_page'] = '10';
$handler-&gt;display-&gt;display_options['style_plugin'] = 'table';
/* Field: User: Uid */
$handler-&gt;display-&gt;display_options['fields']['uid']['id'] = 'uid';
$handler-&gt;display-&gt;display_options['fields']['uid']['table'] = 'users';
$handler-&gt;display-&gt;display_options['fields']['uid']['field'] = 'uid';
$handler-&gt;display-&gt;display_options['fields']['uid']['label'] = '';
$handler-&gt;display-&gt;display_options['fields']['uid']['exclude'] = TRUE;
$handler-&gt;display-&gt;display_options['fields']['uid']['element_label_colon'] = FALSE;
$handler-&gt;display-&gt;display_options['fields']['uid']['link_to_user'] = FALSE;
/* Field: User: Name */
$handler-&gt;display-&gt;display_options['fields']['name']['id'] = 'name';
$handler-&gt;display-&gt;display_options['fields']['name']['table'] = 'users';
$handler-&gt;display-&gt;display_options['fields']['name']['field'] = 'name';
$handler-&gt;display-&gt;display_options['fields']['name']['alter']['make_link'] = TRUE;
$handler-&gt;display-&gt;display_options['fields']['name']['alter']['path'] = 'user-profile/[uid]';
$handler-&gt;display-&gt;display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler-&gt;display-&gt;display_options['fields']['name']['alter']['ellipsis'] = FALSE;
$handler-&gt;display-&gt;display_options['fields']['name']['link_to_user'] = FALSE;
/* Sort criterion: User: Created date */
$handler-&gt;display-&gt;display_options['sorts']['created']['id'] = 'created';
$handler-&gt;display-&gt;display_options['sorts']['created']['table'] = 'users';
$handler-&gt;display-&gt;display_options['sorts']['created']['field'] = 'created';
$handler-&gt;display-&gt;display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: User: Active */
$handler-&gt;display-&gt;display_options['filters']['status']['id'] = 'status';
$handler-&gt;display-&gt;display_options['filters']['status']['table'] = 'users';
$handler-&gt;display-&gt;display_options['filters']['status']['field'] = 'status';
$handler-&gt;display-&gt;display_options['filters']['status']['value'] = '1';
$handler-&gt;display-&gt;display_options['filters']['status']['group'] = 1;
$handler-&gt;display-&gt;display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: User: Roles */
$handler-&gt;display-&gt;display_options['filters']['rid']['id'] = 'rid';
$handler-&gt;display-&gt;display_options['filters']['rid']['table'] = 'users_roles';
$handler-&gt;display-&gt;display_options['filters']['rid']['field'] = 'rid';
$handler-&gt;display-&gt;display_options['filters']['rid']['value'] = array(
  5 =&gt; '5',
);

/* Display: Page */
$handler = $view-&gt;new_display('page', 'Page', 'page');
$handler-&gt;display-&gt;display_options['path'] = 'user-profiles';

/* Display: Page */
$handler = $view-&gt;new_display('page', 'Page', 'page_1');
$handler-&gt;display-&gt;display_options['defaults']['pager'] = FALSE;
$handler-&gt;display-&gt;display_options['pager']['type'] = 'some';
$handler-&gt;display-&gt;display_options['pager']['options']['items_per_page'] = '1';
$handler-&gt;display-&gt;display_options['pager']['options']['offset'] = '0';
$handler-&gt;display-&gt;display_options['defaults']['fields'] = FALSE;
/* Field: User: Rendered User */
$handler-&gt;display-&gt;display_options['fields']['rendered_entity']['id'] = 'rendered_entity';
$handler-&gt;display-&gt;display_options['fields']['rendered_entity']['table'] = 'views_entity_user';
$handler-&gt;display-&gt;display_options['fields']['rendered_entity']['field'] = 'rendered_entity';
$handler-&gt;display-&gt;display_options['fields']['rendered_entity']['label'] = '';
$handler-&gt;display-&gt;display_options['fields']['rendered_entity']['element_label_colon'] = FALSE;
$handler-&gt;display-&gt;display_options['fields']['rendered_entity']['link_to_entity'] = 1;
$handler-&gt;display-&gt;display_options['fields']['rendered_entity']['display'] = 'view';
$handler-&gt;display-&gt;display_options['fields']['rendered_entity']['view_mode'] = 'full';
$handler-&gt;display-&gt;display_options['fields']['rendered_entity']['bypass_access'] = 1;
$handler-&gt;display-&gt;display_options['defaults']['arguments'] = FALSE;
/* Contextual filter: User: Uid */
$handler-&gt;display-&gt;display_options['arguments']['uid']['id'] = 'uid';
$handler-&gt;display-&gt;display_options['arguments']['uid']['table'] = 'users';
$handler-&gt;display-&gt;display_options['arguments']['uid']['field'] = 'uid';
$handler-&gt;display-&gt;display_options['arguments']['uid']['default_action'] = 'default';
$handler-&gt;display-&gt;display_options['arguments']['uid']['default_argument_type'] = 'user';
$handler-&gt;display-&gt;display_options['arguments']['uid']['default_argument_options']['user'] = FALSE;
$handler-&gt;display-&gt;display_options['arguments']['uid']['summary']['number_of_records'] = '0';
$handler-&gt;display-&gt;display_options['arguments']['uid']['summary']['format'] = 'default_summary';
$handler-&gt;display-&gt;display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';
$handler-&gt;display-&gt;display_options['path'] = 'user-profile/%';


In my case, I wanted to list user of role field executive.