Is it possible to create/manage Roles using a .yaml file? I'm trying to secure some REST URLs that are present in my ApiController.php so that users with certain privileges can access these REST APIs.
I created some users and assigned them with some Roles using the below link: 

https://www.drupal.org/docs/user_guide/en/user-concept.html

As you can see, I'm creating roles in Drupal Admin following below steps: 

Drupal UI → People → Role → Add New Role     // line 4

After that, I added a few permissions in permission.yml:

xyz.permissions.yml

edit permission:
  title: 'edit permission'


And updated routing file to secure APIs:

xyz.routing.yml

xyz.update:
  path: '/update'
    _controller: 'ApiController::update'
    _permission: 'edit permission'


This is working fine. i.e. users that have "edit permission" can access URL "/update". 
But, I don't want to do step on line 4 rather I would like to use some .yaml file to create Roles.
User roles are defined in YML files called user.role.ROLENAME.yml.

Do a config export (drush cex) to get an idea how these files should look like (they will be written to your config/sync directory).

Then create your custom role YML files in my_module/config/install and they will be created when you enable your module (if you took a YML file from config/sync as blueprint, remove the uuid and default_config_hash keys)

The file should look something like this:

langcode: en
status: true
dependencies: {  }
id: my_custom_role
label: My custom role
weight: 1234
is_admin: null
permissions:
  - 'do fancy stuff'