A device role is aptly named, the role of the device. This is likely to be something that is meaningful to your organization and could change. For example you may have the 3 tier system of Core, Distribution, and Access layer environments. These are just fine. So you would want to have the roles there to reflect this reality. You may have leaf-spine environments, there are two more roles. And in my past I have also had roles that would indicate that there are dedicated DMZ, WAN edge, Internet edge devices. So this is the place to set this.
Running the playbook on the roles are going to be straight to the point. Before the execution Nautobot's UI shows no device roles:
----name:"ADDDEVICEROLES"hosts:localhostconnection:localgather_facts:false# No gathering facts about the container execution envtasks:-name:"05-ADDDEVICEROLES"# Already present, showing idempotencynetworktocode.nautobot.device_role:url:"{{lookup('env','NAUTOBOT_URL')}}"token:"{{lookup('env','NAUTOBOT_TOKEN')}}"data:name:"{{item['name']}}"color:"{{item['color']}}"vm_role:"{{item['vm_role']}}"loop:"{{device_roles}}"
josh-v@a6339c74e30d:~$ ansible-playbook add_device_role.yml -vvansible-playbook 2.10.6config file = /local/ansible.cfgconfigured module search path = ['/local/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/local/lib/python3.7/site-packages/ansibleexecutable location = /usr/local/bin/ansible-playbookpython version = 3.7.10 (default, Feb 16 2021, 19:28:34) [GCC 8.3.0]Using /local/ansible.cfg as config file[WARNING]:No inventory was parsed, only implicit localhost is available[WARNING]:provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'redirecting (type:callback) ansible.builtin.yaml to community.general.yamlredirecting (type:callback) ansible.builtin.yaml to community.general.yamlSkipping callback 'default', as we already have a stdout callback.Skipping callback 'minimal', as we already have a stdout callback.Skipping callback 'oneline', as we already have a stdout callback.PLAYBOOK:add_device_role.yml **********************************************************************************************1 plays in add_device_role.ymlPLAY [ADD DEVICE ROLES] ****************************************************************************************************META:ran handlersTASK [05 - ADD DEVICE ROLES] ***********************************************************************************************task path:/local/add_device_role.yml:7changed:[localhost]=> (item={'name':'Firewall','color':'FF0000','vm_role':True}) => changed=trueansible_loop_var:itemdevice_role:color:ff0000created:'2021-03-14'custom_fields:{}description:''id:252bd7ba-3b15-4651-a7e3-e43cdd85227clast_updated:'2021-03-14T18:50:53.994175Z'name:Firewallslug:firewallurl:http://nautobot-demo.josh-v.com/api/dcim/device-roles/252bd7ba-3b15-4651-a7e3-e43cdd85227c/vm_role:trueitem:color:FF0000name:Firewallvm_role:truemsg:device_role Firewall createdchanged:[localhost]=> (item={'name':'Leaf','color':'008000','vm_role':False}) => changed=trueansible_loop_var:itemdevice_role:color:008000created:'2021-03-14'custom_fields:{}description:''id:35f176c4-9b20-4e3c-b961-47c624afaa56last_updated:'2021-03-14T18:50:54.942372Z'name:Leafslug:leafurl:http://nautobot-demo.josh-v.com/api/dcim/device-roles/35f176c4-9b20-4e3c-b961-47c624afaa56/vm_role:falseitem:color:008000name:Leafvm_role:falsemsg:device_role Leaf createdchanged:[localhost]=> (item={'name':'Router','color':'000080','vm_role':True}) => changed=trueansible_loop_var:itemdevice_role:color:000080created:'2021-03-14'custom_fields:{}description:''id:c6909cfd-0fd9-4ab1-b0e7-58493fff84b7last_updated:'2021-03-14T18:50:55.901588Z'name:Routerslug:routerurl:http://nautobot-demo.josh-v.com/api/dcim/device-roles/c6909cfd-0fd9-4ab1-b0e7-58493fff84b7/vm_role:trueitem:color:000080name:Routervm_role:truemsg:device_role Router createdchanged:[localhost]=> (item={'name':'Server','color':'000000','vm_role':False}) => changed=trueansible_loop_var:itemdevice_role:color:'000000'created:'2021-03-14'custom_fields:{}description:''id:f9acf678-7b71-4cf9-88f8-4e3ad3f499cblast_updated:'2021-03-14T18:50:57.004599Z'name:Serverslug:serverurl:http://nautobot-demo.josh-v.com/api/dcim/device-roles/f9acf678-7b71-4cf9-88f8-4e3ad3f499cb/vm_role:falseitem:color:'000000'name:Servervm_role:falsemsg:device_role Server createdchanged:[localhost]=> (item={'name':'Spine','color':'0000FF','vm_role':False}) => changed=trueansible_loop_var:itemdevice_role:color:0000ffcreated:'2021-03-14'custom_fields:{}description:''id:4121b0bf-8085-424e-bf0d-b11855cd9c04last_updated:'2021-03-14T18:50:57.912158Z'name:Spineslug:spineurl:http://nautobot-demo.josh-v.com/api/dcim/device-roles/4121b0bf-8085-424e-bf0d-b11855cd9c04/vm_role:falseitem:color:0000FFname:Spinevm_role:falsemsg:device_role Spine createdchanged:[localhost]=> (item={'name':'Switch','color':'008000','vm_role':True}) => changed=trueansible_loop_var:itemdevice_role:color:008000created:'2021-03-14'custom_fields:{}description:''id:c99fe14b-5172-446f-8bd5-78c1e84aaa1clast_updated:'2021-03-14T18:50:58.743836Z'name:Switchslug:switchurl:http://nautobot-demo.josh-v.com/api/dcim/device-roles/c99fe14b-5172-446f-8bd5-78c1e84aaa1c/vm_role:trueitem:color:008000name:Switchvm_role:truemsg:device_role Switch createdchanged:[localhost]=> (item={'name':'VM','color':'00FFFF','vm_role':True}) => changed=trueansible_loop_var:itemdevice_role:color:00ffffcreated:'2021-03-14'custom_fields:{}description:''id:00a2da7f-fe44-4332-bc58-391b429c97eblast_updated:'2021-03-14T18:50:59.548621Z'name:VMslug:vmurl:http://nautobot-demo.josh-v.com/api/dcim/device-roles/00a2da7f-fe44-4332-bc58-391b429c97eb/vm_role:trueitem:color:00FFFFname:VMvm_role:truemsg:device_role VM createdMETA:ran handlersMETA:ran handlersPLAY RECAP *****************************************************************************************************************localhost :ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
After completion of this you will have the device roles are now available to be assigned to devices. Taking a look the UI now has the data:
Device roles are a required item to add devices to Nautobot. This can be as generic as "Device" or "Network Device". However, I strongly encourage you to look at putting some thought into the roles that you will assign to devices. This will become very helpful in the future as you look at building out the automation platform. You can see in the inventory build, you can assign devices based on roles to an inventory group. This becomes particularly helpful when you want to run a playbook against a single group, such as all Leaf switches, or all Spine switches that must have a particular configuration set.
Hope this has helped. If so, let me know with a comment below or give a thumbs up on the post. Connect with me on Twitter @vanderaaj.