Linux – chef common recipe multiple roles

chefchef-serverlinuxmongodb

i have created a mongodb chef recipe, and i am overriding the default attributes on the recipe from the role.

name 'mongo_datanode_jackey'
description 'this module is currently under testing...'

override_attributes(
    susemongodb:  {
        node_type:  'datanode',
        node_nickname: 'jackey',
        port:  '27018',
        is_replicaset_node: true,
        is_cluster_node: true,
        replicaset_name: 'myreplica',
        clusterRole: 'configsvr',
     }

)

run_list(
  "recipe[susemongodb::setupmachine]",
  "recipe[susemongodb::datanode]"
)

now, I would like to spin up multiple datanodes within the same VM, so i have created another role similar to above but with different attribute values.

name 'mongo_datanode_meerkat'
description 'this module is currently under testing...'

override_attributes(
    susemongodb:  {
        node_type:  'datanode',
        node_nickname: 'meerkat',
        port:  '27019',
        is_replicaset_node: true,
        is_cluster_node: true,
        replicaset_name: 'myreplica',
        clusterRole: 'configsvr',
     }

)

run_list(
  "recipe[susemongodb::setupmachine]",
  "recipe[susemongodb::datanode]"
)

If you notice they are using the same recipe, which gets passed the attributes,

I am calling the roles in the client,

{
  "name": "mongo1",
  "chef_environment": "development",
  "normal": {
    "tags": [

    ]
  },
  "run_list": [
  "role[mongo_datanode_jackey]",
  "role[mongo_datanode_meerkat]",
  "role[mongo_datanode_zebra]"
]

}

however, when chef runs the it runs on the last role on the run_list….any idea why?

[2016-02-11T16:51:35+00:00] INFO: Forking chef instance to converge...
[2016-02-11T16:51:35+00:00] INFO: *** Chef 12.6.0 ***
[2016-02-11T16:51:35+00:00] INFO: Chef-client pid: 14010
[2016-02-11T16:51:40+00:00] INFO: Run List is [role[mongo_datanode_jackey], role[mongo_datanode_meerkat], role[mongo_datanode_zebra]]
[2016-02-11T16:51:40+00:00] INFO: Run List expands to [susemongodb::setupmachine, susemongodb::datanode]
[2016-02-11T16:51:40+00:00] INFO: Starting Chef Run for mongo1

I would like each role to be applied on the server and use the common recipe…..?

Best Answer

A node can only have one value value for a given attribute (such as node["susemongodb"]["node_nickname"]) and the values are determined during the compile phase (see "Stages" here).

So in your case, and assuming there are no overrides with higher precedence, the values from the last role in the run-list will be used.

In addition, before starting the converge phase, the initial run-list is expanded to individual recipes. One recipe can only appear once in the expanded run-list, which is why you get:

INFO: Run List expands to [susemongodb::setupmachine, susemongodb::datanode]

I don't know how your cookbook is setup but to achieve what you want to could have the node["susemongodb"]["node_nickname"] and node["susemongodb"]["port"] attributes be arrays and in your recipe iterate over those arrays and execute your resources in the loop.