Until now, we have created cookbooks based on a particular technology. We created a cookbook for MariaDB, one for Apache HTTPd, and one for our app (including all the dependencies). What about the role of each of those infrastructure elements? A database role can include what is now running our database (MariaDB), but maybe tomorrow it can run something else (migrate back to MySQL, or switch to PostgreSQL). As roles in Chef have a dedicated run list, it's common to see a role include the product recipes, and everything related to it, think monitoring for example. Roles can do a lot more, like overriding attributes or have different run lists for each environment. Here, we'll create two generic database and webserver roles that might be simply reused later for another project that just need those services and a mysite role, that will include the two other roles. A role can include other roles as well as recipes. This way, the role for mysite will be enough to run our infrastructure, from a functional point of view.
To work through this recipe, you will need the following:
Follow these steps for creating functional roles:
webserver
role in roles/webserver.rb
. It requires a name, a description, and a run list. That's the bare minimum:name "webserver" description "An HTTP server for our application" run_list "recipe[apache]"
database
role; we currently want to use our mariadb
cookbook. So let's write it in roles/database.rb
:name "database" description "A database server for our application" run_list "recipe[mariadb]"
mysite
role, that will include a webserver, a database, as well as its own cookbook, in roles/mysite.rb
:name "mysite" description "MySite role" run_list( "role[webserver]", "role[database]", "recipe[mysite]" )
knife
command:$ knife role from file database.rb Updated Role database $ knife role from file webserver.rb Updated Role webserver $ knife role from file mysite.rb Updated Role mysite
role[mysite]
), or if you're about to bootstrap the server; adding the -r "role[mysite]"
option will bootstrap Chef on the node as well as execute Chef with this run list:$ knife bootstrap 192.168.146.129 -N vagrant -x vagrant --sudo -r "role[mysite]"
We'll now be free to add more complex features to our role in the future!
Puppet does not provide a role feature. However, this can be done by adding a level of abstraction using the role and profile design pattern. In this pattern:
Using this pattern, it is easier to refactor only profiles classes when the technology needs to be changed.
3.145.179.35