Skip to main content

Add Chef Infra node attributes with a cookbook

You can use the chef-cookbook-attr-discovery cookbook to add Chef Infra attributes—such as Ohai data, roles, and environments—to Chef 360 Platform. This cookbook maps Chef Infra attributes to a specified namespace and sends them to Chef 360 Platform using either the PATCH or PUT HTTP method.

The chef-cookbook-attr-discovery cookbook has the following features:

  • It maps specified Chef attributes to custom names and namespaces.
  • You can use PATCH and PUT methods to send data to Chef 360 Platform.
  • It prevents the use of protected namespaces to avoid conflicts.
  • It can fetch and parse configuration details from a YAML file.
  • You can obtain and manage access tokens for secure API communication.
  • It logs important information and errors for troubleshooting and audit purposes.

Configure and deploy the cookbook

  1. Upload the chef-cookbook-attr-discovery cookbook to your Chef Infra Server:

    knife cookbook upload chef-cookbook-attr-discovery --cookbook-path <COOKBOOK_DIR_PATH>
    
  2. Create a wrapper cookbook and declare a dependency on the custom resource by adding the following line to the metadata.rb file of your wrapper cookbook:

    depends 'chef-cookbook-attr-discovery', '~> 1.0.0'
    
  3. In the wrapper cookbook recipe (for example recipes/default.rb), define the Chef node attributes that you want to send to Chef 360 Platform.

    Use the chef_attrs array to define the node attributes that you want to send to Chef 360 Platform and the names for those attributes in Chef 360 Platform.

    For example:

    chef_attrs = [
      { chef_attr_name: '<CHEF_INFRA_ATTRIBUTE>', nm_attr_name: '<NODE_MANAGEMENT_ATTRIBUTE>' },
      { chef_attr_name: '<CHEF_INFRA_ATTRIBUTE>', nm_attr_name: '<NODE_MANAGEMENT_ATTRIBUTE>' }
    ]
    

    Replace the following:

    • <CHEF_INFRA_ATTRIBUTE> with a Chef Infra node attribute reported by Ohai. For example, platform, hostname, or more complex paths like memory/swap/total.
    • <NODE_MANAGEMENT_ATTRIBUTE> with the name for that attribute in Chef Node Management.
  4. In the wrapper cookbook’s recipe (for example recipes/default.rb), define how those attributes are reported to Chef 360 Platform.

    Use the mapping custom resource to define the connection settings:

    mapping '<RESOURCE_NAME>' do
      nm_namespace '<NODE_MANAGEMENT_NAMESPACE>'
      chef_attrs chef_attrs
      api_url <API_ENDPOINT>
      access_key <ACCESS_KEY>
      secret_key <SECRET_KEY>
      http_method <METHOD>
      action :map
    end
    

    Replace the following:

    • <RESOURCE_NAME> with a resource name.

    • <NODE_MANAGEMENT_NAMESPACE> with a namespace in Node Management.

      There are several reserved namespaces that can’t be used. See the node attributes documentation for more information.

    • <API_ENDPOINT> with the API endpoint to which data is sent.

      If set to node['api_url'], define the API endpoint value in the cookbook attributes in the next step.

      Default value: node['api_url'].

    • <ACCESS_KEY> with the access key used for authentication.

      If set to node['access_key'], define the access key value in the cookbook attributes in the next step.

      Default value: node['access_key'].

    • <SECRET_KEY> with the secret key used for authentication.

      If set to node['secret_key'], define the secret key value in the cookbook attributes in the next step.

      Default value: node['secret_key'].

    • <METHOD> with an HTTP method to use for the request.

      Possible values: :patch or :put.

      Default value: :patch.

      Use the PUT method when you want to completely overwrite data on Chef 360 Platform with new data and when you have a complete representation of the node.

      Use the PATCH method for partial data updates of a node. PATCH is suitable when you only want to modify specific data fields of the resource without affecting other node data.

  5. If you include attributes for the access key, secret key, and API URL in your cookbook’s recipe using the node method, define those attributes in an attributes file—for example attributes/default.rb.

    Use the following attributes to connect to Chef 360 Platform:

    default['access_key'] = '<ACCESS_KEY>'
    default['secret_key'] = '<SECRET_KEY>'
    default['api_url'] = '<API_ENDPOINT_AND_PORT>'
    

    Replace:

    • <ACCESS_KEY> with the Chef 360 Platform access key
    • <SECRET_KEY> with the Chef 360 Platform secret key
    • API_ENDPOINT_AND_PORT> with the Chef 360 Platform API endpoint
  6. Deploy the wrapper cookbook.

    1. If using roles, upload the wrapper cookbook to your Chef Infra Server:

      knife cookbook upload YOUR_WRAPPER_COOKBOOK_NAME --cookbook-path <WRAPPER_COOKBOOK_DIR_PATH>
      
    2. If using Policyfiles, install and push the policy:

      chef install
      chef push <POLICY_GROUP> <POLICYFILE>
      

    Once the wrapper cookbook is uploaded, include it in your roles or policies as needed.

Upon successful execution, the wrapper custom resource performs the following actions:

  • It fetches the specified node attributes and maps them to defined attribute names.
  • It logs the mapped attributes.
  • It sends the mapped attributes as JSON to the specified API endpoint using the chosen HTTP method (PATCH or PUT).
  • It logs the response from Chef 360 Platform and raises an error if the request fails.

Examples

The following examples define attributes and connection settings that can be used in a recipe.

chef_attrs = [
  { chef_attr_name: 'platform', nm_attr_name: 'platform_name' },
  { chef_attr_name: 'hostname', nm_attr_name: 'host_name' },
  { chef_attr_name: "node['policy_name']", nm_attr_name: 'policy_name' },
  { chef_attr_name: "node['policy_group']", nm_attr_name: 'policy_group' },
  { chef_attr_name: 'memory/swap/total', nm_attr_name: 'memoryfree' },
]
mapping 'progress' do
  nm_namespace 'Progress_chef'
  chef_attrs chef_attrs
  api_url node['api_url']
  access_key node['access_key']
  secret_key node['secret_key']
  http_method :put
  action :map
end

Thank you for your feedback!

×











Search Results