How to automatically create relationship between Drupal role user and a group of civi contacts?
Use the Silent User Registration extension https://lab.civicrm.org/extensions/silentuserreg This will create the user in Drupal
Before you install the extension, replace the Createcmsuser.php file in CRM &gt; Silentuserreg &gt; CivirulesAction with the code below. This will create the UF Match that you are looking for to link it to the user in Civi
Follow the extension instructions to set it up. I would have your CiviRule trigger off When contact is added to group or something like that.
&lt;?php

/**

Carls Notes
This file is to create a CRMUser and then a UFMatch record. File Location=secure/sites/all/files/civicrm/extensionsions/silentuserreg-master/CRM/Silentuserreg/Silentuserreg
/
/*
Class for CiviRules Create User Action

@author Jon Goldberg (jon@megaphonetech.com)
@license AGPL-3.0
*/
class CRM_Silentuserreg_CivirulesAction_Createcmsuser extends CRM_Civirules_Action {

/**

Method processAction to execute the action

@param CRM_Civirules_TriggerData_TriggerData $triggerData
@access public


/
public function processAction(CRM_Civirules_TriggerData_TriggerData $triggerData) {
$cid = $triggerData-&gt;getContactId();
$userInfo = \Civi\Api4\Email::get()
-&gt;addSelect('id', 'email', 'contact.display_name', 'is_primary')
-&gt;addWhere('is_primary', '=', TRUE)
-&gt;addWhere('contact_id', '=', $cid)
-&gt;setCheckPermissions(FALSE)
-&gt;execute()
-&gt;first();
$userExists = (bool) count(\Civi\Api4\UFMatch::get()
-&gt;addWhere('contact_id', '=', $cid)
-&gt;setCheckPermissions(FALSE)
-&gt;execute());
if (!$userExists) {
$userParams['cms_name'] = $userParams['nickname'] = $userInfo['contact.display_name'];
$userParams['email'] = $userInfo['email'];
$userParams['cms_pass'] = CRM_Silentuserreg_Util::randomPassword();
$nid=CRM_Core_BAO_CMSUser::create($userParams, 'email');
// create the UF Match record
$ufmatch['uf_id'] = $nid;
$ufmatch['contact_id'] = $cid;
$ufmatch['uf_name'] = $userInfo['email'];
CRM_Core_BAO_UFMatch::create($ufmatch);
}
}
/*

Method to return the url for additional form processing for action
and return false if none is needed

@param int $ruleActionId
@return bool
@access public
*/
public function getExtraDataInputUrl($ruleActionId) {
return FALSE;
}

}