Tuesday, 8 November 2016

Exploring "Infrastructure as code" with Opscode Chef Part 5 (using local mode & more on OHAI)

Using local mode:

In case we want to bypass the chef server & workstation all together for some reason then we can do that as well. Chef client's local mode allows us to apply a locally created recipe without communicating with the chef server or workstation.
Here's a demo:

On the client, create chef-repo directory & inside that directory create the recipe file:

 mkdir chef-repo
[sahil@cclient1 ~]$ cd chef-repo/
[sahil@cclient1 chef-repo]$ vi hello.rb
hello world[sahil@cclient1 chef-repo]$ cat hello.rb
file '/tmp/motd' do
  content 'hello world'
end
[sahil@cclient1 chef-repo]$

Now to apply the recipe, run chef client in local mode:

[sahil@cclient1 chef-repo]$ chef-client --local-mode hello.rb
[2016-11-06T06:04:48-05:00] WARN: No config file found or specified on command line, using command line options.
[2016-11-06T06:04:48-05:00] WARN: No cookbooks directory found at or above current directory.  Assuming /home/sahil/chef-repo.
Starting Chef Client, version 12.15.19
resolving cookbooks for run list: []
Synchronizing Cookbooks:
Installing Cookbook Gems:
Compiling Cookbooks...
[2016-11-06T06:04:50-05:00] WARN: Node cclient1 has an empty run list.
Converging 1 resources
Recipe: @recipe_files::/home/sahil/chef-repo/hello.rb
  * file[/tmp/motd] action create
    - create new file /tmp/motd
    - update content in file /tmp/motd from none to b94d27
    --- /tmp/motd       2016-11-06 06:04:50.389511251 -0500
    +++ /tmp/.chef-motd20161106-2861-1d6cw4j    2016-11-06 06:04:50.389511251 -0500
    @@ -1 +1,2 @@
    +hello world
    - restore selinux security context

Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 01 seconds


Let's verify that the file has been created:

[sahil@cclient1 chef-repo]$ cat /tmp/motd
hello world[sahil@cclient1 chef-repo]$ 


Viewing information collected by OHAI:

As mentioned earlier, OHAI collects a lot of information in JSON format. You can check this information from the chef server GUI by selecting the node & then clicking on the attributes section. You can also check this information locally by just typing ohai command on the client node.

Here is a snippet of the output:

[root@cclient1 ~]# ohai | more
{
  "cpu": {
    "0": {
      "vendor_id": "GenuineIntel",
      "family": "6",
      "model": "60",
      "model_name": "Intel(R) Core(TM) i5-4440 CPU @ 3.10GHz",
      "stepping": "3",
      "mhz": "3092.833",
      "cache_size": "6144 KB",
      "physical_id": "0",
      "core_id": "0",
      "cores": "1",

      "flags": [
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------

We can also view the information about a node gathered by OHAI from the chef workstation:

[sahil@cwork recipes]$ sudo knife node show mychefnode -l | more
Node Name:   mychefnode
Environment: _default
FQDN:        cclient1
IP:          192.168.44.102
Run List:    recipe[apache::addscreen], recipe[apache]
Roles:
Recipes:     apache, apache::default, apache::addscreen
Platform:    centos 7.0.1406
Tags:
Attributes:
tags:

Default Attributes:

Override Attributes:

Automatic Attributes (Ohai Data):
block_device:
  dm-0:
    logical_block_size:  512
    physical_block_size: 512
    removable:           0
    rotational:          1
    size:                2097152
  dm-1:
    logical_block_size:  512
    physical_block_size: 512
    removable:           0
    rotational:          1
    size:                17842176
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------

From the chef workstation we can also query to a particular attribute belonging to a node:

[sahil@cwork recipes]$ sudo knife node show mychefnode -a name
mychefnode:
  name: mychefnode

We can also run a key value search on chef solr using the knife utility since all information gathered by OHAI is available for searching & indexing at the chef server.

[sahil@cwork recipes]$ sudo knife search node "platform:centos" -a name
1 items found

mychefnode:
  name: mychefnode

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...