Wednesday, September 13, 2017

chef-Welcome to the Index

Introduction to Search in Chef

What is a Search Index?

Searching the Index
Subtitle
§ Search indexes allow queries to be made (aka search) for any type of
data that is indexed by the Chef server, including:
clients
environments
nodes
roles
data bags and data bag items – (We cover this in the next module)
§ The search engine is based on Apache Solr and is run from the Chef
server. Check https://docs.chef.io/chef_search.html#query-syntax
for syntax
§ The search query is run on the Chef Server and is invoked from knife,

within a recipe or from the WebUI.

Use knife search

$ knife search node '*:*'!


1 items found!
!
Node Name: node1
Environment: _default!
FQDN: node1.novalocal!
IP: 172.16.232.5!
Run List: recipe[mysite], recipe[motd]!
Roles:!
Recipes: mysite::default, motd::default!
Platform: centos 6.6!
Tags:!


Search Query Syntax

https://docs.chef.io/chef_search.html#query-syntax
§ Chef uses the Solr 'key:query" syntax
!
$ knife search node 'ipaddress:1.2.3.4'!
§ Use an asterisk '*' for wildcard searches when you want anything.
$ knife search node 'ipaddress:1.*'!
$ knife search node 'platfo*:rhel'!
!
§ Use a question mark '?' to replace a single character
!
$ knife search node 'platform_version:6.?'!


The results of the Search is an Array of
Node Objects

§ A successful search query will result in an array of (zero or more)
node objects that match your request.
§ This is important! We now have access to all of the node attributes
for every node that met our query...not just a list of node names.
!
$ knife search node 'platform:redhat'!
!
§ This query will return and array of every node object that is running
Redhat in your Org

Use knife search

knife search node 'ipaddress:1*'!

1 items found!
!
Node Name: node1
Environment: _default!
FQDN: node1.novalocal!
IP: 172.16.232.5!
Run List: recipe[mysite], recipe[motd]!
Roles:!
Recipes: mysite::default, motd::default!
Platform: centos 6.6!
Tags:!

Use knife to search for the ipaddress of every
node in our Org and filter the results

$ knife search node '*:*' –a ipaddress!

1 items found!
!
node1:!
ipaddress: 172.16.232.5!

Use knife to search for the ipaddress of every node in
our Org that starts with 1 and filter the results

knife search node 'ipaddress:1*' –a ipaddress

1 items found!
!
node1:!
ipaddress: 172.16.232.5

Use knife search with Boolean matching

$ knife search node 'ipaddress:1* AND platform_family:rhel'!

1 items found!
!
Node Name: node1
Environment: _default!
FQDN: node1.novalocal!
IP: 172.16.232.5!
Run List: recipe[mysite], recipe[motd]!
Roles:!
Recipes: mysite::default, motd::default!
Platform: centos 6.6!
Tags:!

Searching inside a Recipe

§ More important that using knife for searches, you can query the Chef
Server within your recipes to make dynamic cookbooks.
§ You can invoke a search with a recipe, and use the results to apply
further Chef resources. They follow a pattern like this:
!
search(INDEX, SEARCH_QUERY).each do |result|!
< Do things>!
end!
!
§ For example:
!
search('node', 'role:webserver') do |ws|!
< Add ws['ipaddress'] to haproxy config >!
end

A quick word on Search and Index Delay



eventual consistency for your infrastructure
§ Search is based on an index, so if you just defined an attribute in a
recipe...it will NOT be immediately available.
!
node.default['admin'] = 'Doris'!
search('node', admin:*').each do |adm|!
user adm['admin'] !
end!
!
§ Here, since the attribute value has just been defined the search will
NOT pick it up until Solr rebuilds its index.
§ The value is already in memory, no need to search!
§ REMEMBER, node attributes set or changed during a Chef run aren't
saved back to the server until the node successfully converges.!

Knowledge Check
§ What search engine is Chef search built on?
§ What is searchable in Chef?
§ What character can you use for a wildcard search?
§ Why should you not set an attribute and then immmediately search
for it?

















No comments:

Post a Comment