Chef- Roles

A role is a way to define certain patterns and processes that exist across nodes in an organization as belonging to a single job function. Each role consists of zero (or more) attributes and a run-list. Each node can have zero (or more) roles assigned to it.

When a role is run against a node, the configuration details of that node are compared against the attributes of the role, and then the contents of that role’s run-list are applied to the node’s configuration details. When a Chef Infra Client runs, it merges its own attributes and run-lists with those contained within each assigned role

Step to create Role

Inside the chef-repo directory, we have roles directory which contains the default.rb ruby file.

  • Create One more file with roleName.rb and edit with name, description, and run-list details.

    vi roleName.rb

    Edit this file with,

    Name “Newfile”

    Description “webserver role”

    run_list “recipe[apache-cookbook::apache-recipe]”

    You can add multiple run-list, In these case Apache recipe will reflect on the node after bootstrapping.

  • Upload the role to chef server

    Back to the chef-repo directory,

    knife role from file roles/RoleName.rb

  • If you want to see the created role

    knife role list

  • Create No. of nodes as you wish, Bootstrap them

    knife bootstrap <privateIP(node)> -–ssh-user ec2-user -–sudo -i nodeNokey.pem -N nodeNo

  • Now connect these nodes to roles one by one.

    knife node run-list set nodeNo “role[RoleName]”

  • UPLOAD cookbook to server

    knife cookbook upload apache-cookbook

    Now we can check the public IP of any node on the webserver, every node will behave like a server.

How to see Delete everything from inside chef-server

Run the below command in the Chef-repo directory,

  • To see cookbook list

    knife cookbook list

  • To delete cookbook

    knife cookbook delete cookbookName -y

  • To see Node list

    knife node list

  • To delete Node

    knife cookbook delete nodeName -y

  • To see list of client present inside chef-server

    knife client list

  • To delete clients

    knife client delete clientname -y