Saturday, September 2, 2017

Chef-attribute precedence

One thing ,there is a facter,that gets involved.

The purpose of this tool is to give the facts(means the data)
Basically,the tool which collects (these facts)  is Ohia.

Facter is a tool,which collect facts is in windows operating system.

We want facts of our machine.

When you install the chef,a tool called ohai is installed.

Ohai has its own way of its,looking at the facts.
This is the tool,which takes final call on the precendence and other things.

When we do this
>>  ohia | more

we get the all facts related to that system in JSON format

The format we are seeing is not the ruby has,the format we are seeing the is JSON FORMAT

Difference between ruby hash and JSON format

Ruby hash looks like this:
{
     “action”   =>  “something”
}

samething in the JSON hash looks like
{
     “action”  : “something”
}

ohai is the tool,basically wraps up everything and gives you the facts.
And ohai is used for the every convergence.

We have seen,some of the details of it in manage.chef.io,those are facts and made something of it is the node objects.

Ohai is basically responsible for that.

How it is built,it is depend up on how you built the attributes.

What is the responsibility of ohai tool for?
Ohai tool is responsible for getting all the facts of your system and which is responsible for giving you all the values.

(these give values in the JSON Format and
whenever you want to access this,we use the node[] and a hash processing structure)

for example:if I want something like network and interfaces:
    >>  node [“networks”][“interfaces”]
     (this is specially used in my recipe).
    >>  node [“networks”][“recipes”]
This is the tool,which basically builds something called as node object.
Node object is built between every convergence.

Ohai tools gives us,basically the facts,these facts used to construct a node object.

Note:***
When will be node object will be constructed?
It is constructed during every convergence,whenever you execute a simple command called the chef-client,on your node,node object is built.

Node object is a collection of all the details (that means your details,the variables,which you define in your files).

This is one of the major tool,present in the agent.

Ohai is the tool,which serves this:

Platform details
Network usage
Memory usage
CPU data
Kernel data
Host names
Fully qualified domain names
Other configuration details.

Whatever,we write in the attributes folder,we mention it as a default attribute(customized attibutes)
Similarly,there is another kind of attribute is called the automatic attribute (collected by ohai)


Attributes that are collected by Ohai are automatic attributes in that  these attributes  are used by the chef-client  to ensure that these attributes  remain unchanged  after the chef-client is done,that the configuring node.

Actual meaning of attribute = it is a detail about the node.

Attribute is a specific detail about  a node.Attributes are used  by the chef-client to understand:

The current state of the node
What the state of the node was at the end of the previous chef-client run.
What the state of the node should be at the end of the current chef-client run.

Attribute are defined by:
The state of the node itself
Cookbooks (in attribute files and/or recipes).
Roles
Environments.




                                          (What is attribute  =  specific detail of node)
    Types  of attributes  <<<<<<       Attributes   >>>>>>>     who uses it (Chef-client)
           (default)                                                ^            ^
           (force_default)                                    ^            ^
           (normal)                                                ^            ^  >>>>>>>>>  (Why are attributes)
           (override)                                             ^                                        To determine
           (force_override)                                  ^                                       (current state of node)
           (Automatic)                                          ^                                      1)State of the Node before
                                                                           ^                                     previous chef-client run
                                                                           ^                                     2)State of the node that  should                                                                                                                  
                                                           Who defines attributes                 be after current chef-client.
                                                             The state of node itself
                                                              Cookbooks
                                                              Roles

                                                              Environments




During every chef-client  run,the chef client builds the attributes list using:

*Data about the node collected by Ohai.

How to identify the attribute type?

Usually,we write:
default[“mynetwork”] [“myrecipes”]
Force_default[“mynetwork”] [“myrecipes”]
normal[“mynetwork”] [“myrecipes”]
override[“mynetwork”] [“myrecipes”]
force_override[“mynetwork”] [“myrecipes”]

                                                 Chef-client          
                         Run starts (that means convergence started)                                                        
 [Description about chef-client:
 For chef,there is a tool called the chef-client,whenever the chef-client starts,there will be a sequence of steps/actions that happen (takes place),that determine basically,how your recipe is applied to your node]
                        All the Attributes are reset
[Description:
 All the attributes,that are written are reset(reset means,everything goes into the default values ,whether you have written or Ohai is collected it,everything is reset back,reset is not happened to one type of attribute,which is normal,all the attributes are reset,except the attributes,that are defined as normal(by saying normal,your saying these variable,does not have impact  and will not change lot of times].
                       Chef-client  rebuilds attributes  by using attributes collected by ohai
(it is a tool,which collects all the attributes)
     Chef-client uses default and override attributes specified in cookbooks  roles or environment
[Description:normal or reset,it will not take care about much,basically,if you have attribute one place noproblem,we can define attribute at three places,one is attribute or recipe file,roles and environments.attributes at all the three places,with same variable name,then it is a problematic situation right,we cannot have three values,we can have only have one value to it,so,during this time,the attributes are merged and applied to node,based on the rules,which is called the attribute precedence]
      Attributes are merged,applied to node according to attribute precedence.
[if I have a same variable in the attribute,same variable in role and same variable in environment,what gets applied?,for that we are using the attribute precedence.once this is been applied,all the attributes are saved and acopy is sent to your chef-server].
      Attributes that were applied to node are saved to the chef-server as part of the node object.
                                                 Chef-client run finish
                             (that’s how,every convergence happends).


Flow chart steps are written inorder:
1)Chef-client starts
2)All the attributes values are reset to the default values.
(here are the exceptions,attributes which are type:normal).
3)Basically,ohai will be run to collect the facts and these will be filtered.
4)Basically,it will look in to your cookbook files,to identify,are they any attributes which are user defined,then it will try to pull,all of the variables and then you have situation,where variable is defined at,more than one place.same variable,one in attribute,another one in role and another in environment.(you might have combination of these things).
5)so,what it will do is,it will merge those values,using the attribute precedence.
6)All of these values,form something called the node object.
7)these values again,sent back to the chef server.
(that is why,chef-server will have the update attribute-list everytime).

Note
When this attributes are populated in to this server?
Or when is the last time,these are attributes are populated in to the server?
Last convergence happened.(when was the last convergence happends of that node).
(That basically,determines the recentness,when the node object was built).
(whenever convergence happends,you will get this object).

Based out of the datatype,whatever it is storing,it will have its own default:
The default for integer is zero.
The default for Boolean is false.
(all of values,default set to its values).
Only exception,if the type of the attribute is normal.
If it is the type:normal,reset process will not impact.

Notes:
This explain,how to write variable?
When to write a variable.
This is important,because,when your doing continuous delivery in chef.
If you want to impact only for the production environment,how you take care.

Note:
For example of run_list:
You have written ten cookbooks,but for your node run_list,you applied only the one cookbook.your server is responsible of taking only one cookbook.
Run_list+cookbook combination determines = what goes into your node.

Note:
Hosted chef server will be trying to get the node details,the agent does all of this work and gives it back.

Note:
In puppet,the files get compiled,then transferred.that’s what the manifest is.(compiled catlogies).
In chef,whatever you sent,might not be executed,in whatever the format you sent.
(but,you can still tweak it,there is a chance,you can still tweak chef-client some more).

Incase of anyother tool,that is not possible.
Because,you getting a complied version of it.

Is there any signing mechanism that happends?
There is no other signing mechanism,that happends.
(while bootstrap,we are giving all our .pem files,this is the communication we use for chef).
(this is agent-level communication)
In ansible,it is machine-level communication.(there is two python’s,trying to communicate with each other).

What are the attribute values,we have,when we have to use that?

 Attribute Types
         >>>>   Default (your giving the lowest priority to a variable)
                       (Automatically reset at every chef-client run (convergence)
                       (lowest precedence)
                       (recommended attribute type).
         >>>>    force_default (this is one-level more priority than default).
                        (Use the force_default  to ensure that attribute defined in coobook
                         takes precedence over default defined in roles or environment).
         >>>>    normal (normal is for,we don’t want variable value be reset,during the chef-covergence,one-level more priority than force_default)
                         (Normal attributes are never reset)
                         (Normal attributes persists in node object).
                         (Higher precedence than default attribute).
         >>>>     override
                         (An override attribute is automatically reset  at the start  of every chef-client)
                         (higher attribute precedence  than default,force_default and normal).
                         (An override attribute Is most often specified in a recipe,but can be specified in       an attribute file,for a role and/or for an environment).
                         (A cookbook should be authored so that it uses override attributes only when required).
         >>>>      force_override
                          (use the force_override attributes to ensure that an attribute defined in a cookbook  (by an attribute file or by a recipe) takes precedence over an override  attribute set by a role  or an environment.
         >>>>      automatic
                             An automatic  attribute contains data that is identified by  Ohai at the beginning  of every chef-client run.
                              An automatic attribute cannot be modified and always  has the highest attribute  precedence.



The table which determines:

Attribute precedence
                                       Attribute files     Node/Recipe           Environment            Role
Default                                    1                          2                               3                               4

Force_default                         5                          6

Normal                                     7                          8

Override                                   9                         10                            12                            11

Force_overide                        13                        14

Automatic                                                                             15



FOR EXAMPLE:
If you have written an attribute in a attribute file,then the value of it  is “1”,if you define an attribute in recipe or in a node object,this is where you edit on the hosted chef,(these two have same priority,which is “2”.if I define “a” in an attribute file,the same variable “a” in recipe,the value of “a” in attribute is “1” and the value of “a” in recipe is “5”.

“1” is more or “2”  is more?

“2”  is greater in number ,so that attribute,which you define in node/recipe,takes the priority.
And the attribute type is default,at both the places.

For example:

Attribute,which I want to write is:

Syntax:
[cookbookname] [package]

the value of this ‘tree’,if I given in attribute file.
(this I write in separate attribute file).
default[‘helloworld’] [‘package’] = tree
(where is this define?
 this is defined at attribute file).


And now,in node or recipe,I put its value as git
default[‘helloworld’] [‘package’] = git
(where is this define?
 this is defined at node/recipe file).


And now,when convergence happends,it will check both the attributes are saying about the samething,let me find out is priority.then it will check with the above precedence table,the value of the attribute file is “1” in default attribute type and the value of  node/recipe is “2”in default attribute type.since “2” is greater,I am going to apply  default[‘helloworld’] [‘package’] = git  to chef-client.

Example2:

Changes in the attribute file:
Default to normal the attribute type is changed.

normal[‘helloworld’] [‘package’] = tree 
(this is defined at attribute file and the attribute type is “normal attribute”)

default[‘helloworld’] [‘package’] = git   
(this is defined at node/recipe file and the attribute type is “default attribute”)


And now,when convergence happends,it will check both the attributes are saying about the samething,let me find out is priority.then it will check with the above precedence table,the value of the attribute file is “7” in normal attribute type and the value of  node/recipe is “2”in default attribute type.since “7” is greater,I am going to apply normal[‘helloworld’] [‘package’] = tree  to chef-client.


For example:
In production environment,(this is in our environment file,let us assume).

Env default[‘helloworld’] [‘package’] =  wget 
(this is defined in my environments).


Attribute==default[‘helloworld’] [‘package’] = tree
(this is defined in my attributefile).


RECIPE==default[‘helloworld’] [‘package’] = git   
(this is defined in my RECIPE).

All of these three,saying about same variable right,this defined at three different places,what should be applied,if you observe the attributes precedence table,obviously environments and roles have highest priority.

Only for testing environments,I want to install “tree”:

The best approach for this is:

Attribute==force_default[‘helloworld’] [‘package’] = tree
(the value becomes to “5”,that means it is greater than the environments and roles in the default attribute type).

Note:I am not changing the behavior,the behavior is still default.
(the table tells,where you can define,different values and where we can apply this).

Note:
Out of all of this,we cannot apply automatic.

Note:
It is nothing but the attribute type and place where you define the variable.

What are the two things,that impact the variable?
One is the attribute type and other is the,where you define the attribute.

What is meant by apply?
Its nothing but the execution

Note:
Solo and zero are for provisioning.
(All of  these are for provisioning and applying on one machine).


 Chef-zero we are applying daily.

No comments:

Post a Comment