martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 1 | ============================================================= |
| 2 | reclass — recursive external node classification |
| 3 | ============================================================= |
| 4 | reclass is © 2007–2013 martin f. krafft <madduck@madduck.net> |
| 5 | and available under the terms of the Artistic Licence 2.0 |
martin f. krafft | e39e890 | 2013-06-14 22:12:17 +0200 | [diff] [blame] | 6 | ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' |
martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 7 | |
| 8 | reclass is an "external node classifier" (ENC) as can be used with automation |
| 9 | tools, such as Puppet, Salt, and Ansible. |
| 10 | |
| 11 | The purpose of an ENC is to allow a system administrator to maintain an |
| 12 | inventory of nodes to be managed, completely separately from the configuration |
| 13 | of the automation tool. Usually, the external node classifier completely |
| 14 | replaces the tool-specific inventory (such as site.pp for Puppet, or |
| 15 | /etc/ansible/hosts). |
| 16 | |
martin f. krafft | 6223989 | 2013-06-14 20:03:59 +0200 | [diff] [blame] | 17 | reclass allows you to define your nodes through class inheritance, while |
| 18 | always able to override details of classes further up the tree. Think of |
| 19 | classes as feature sets, as commonalities between nodes, or as tags. Add to |
| 20 | that the ability to nest classes (multiple inheritance is allowed, |
| 21 | well-defined, and encouraged), and piece together your infrastructure from |
| 22 | smaller bits, eliminating redundancy and exposing all important parameters to |
| 23 | a single location, logically organised. |
| 24 | |
martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 25 | In general, the ENC fulfills two jobs: |
| 26 | |
| 27 | - it provides information about groups of nodes and group memberships |
| 28 | - it gives access to node-specific information, such as variables |
| 29 | |
| 30 | While reclass was born into a Puppet environment and has also been used with |
| 31 | Salt, the version you have in front of you is a rewrite from scratch, which |
| 32 | was targetted at Ansible. However, care was taken to make the code flexible |
| 33 | enough to allow it to be used from Salt, Puppet, and maybe even other tools as |
| 34 | well. |
| 35 | |
| 36 | In this document, you will find an overview of the concepts of reclass, the |
| 37 | way it works, and how it can be tied in with Ansible. |
| 38 | |
| 39 | Quick start — Ansible |
| 40 | ~~~~~~~~~~~~~~~~~~~~~ |
| 41 | The following steps should get you up and running quickly. Generally, we will |
| 42 | be working in /etc/ansible. However, if you are using a source-code checkout |
| 43 | of Ansible, you might also want to work inside the ./hacking directory |
| 44 | instead. |
| 45 | |
| 46 | Or you can also just look into ./examples/ansible of your reclass checkout, |
| 47 | where the following steps have already been prepared. |
| 48 | |
| 49 | /…/reclass refers to the location of your reclass checkout. |
| 50 | |
| 51 | 1. Symlink /…/reclass/adapters/ansible to /etc/ansible/hosts (or |
| 52 | ./hacking/hosts) |
| 53 | |
| 54 | 2. Copy the two directories 'nodes' and 'classes' from the example |
| 55 | subdirectory in the reclass checkout to /etc/ansible |
| 56 | |
| 57 | If you prefer to put those directories elsewhere, you can create |
| 58 | /etc/ansible/reclass-config.yml with contents such as |
| 59 | |
| 60 | storage_type: yaml_fs |
| 61 | nodes_uri: /srv/reclass/nodes |
| 62 | classes_uri: /srv/reclass/classes |
| 63 | |
| 64 | Note that yaml_fs is currently the only supported storage_type, and it's |
| 65 | the default if you don't set it. |
| 66 | |
| 67 | 3. Check out your inventory by invoking |
| 68 | |
| 69 | ./hosts --list |
| 70 | |
| 71 | which should return 5 groups in JSON-format, and each group has exactly |
| 72 | one member 'localhost'. |
| 73 | |
| 74 | 4. See the node information for 'localhost': |
| 75 | |
| 76 | ./hosts --host localhost |
| 77 | |
| 78 | This should print a set of keys and values, including a greeting, |
| 79 | a colour, and a sub-class called 'RECLASS'. |
| 80 | |
| 81 | 5. Execute some ansible commands, e.g. |
| 82 | |
| 83 | ansible -i hosts \* --list-hosts |
| 84 | ansible -i hosts \* -m ping |
| 85 | ansible -i hosts \* -m debug -a 'msg="${greeting}"' |
| 86 | ansible -i hosts \* -m setup |
| 87 | ansible-playbook -i hosts test.yml |
| 88 | |
| 89 | 6. You can also invoke reclass directly, which gives a slightly different |
| 90 | view onto the same data, i.e. before it has been adapted for Ansible: |
| 91 | |
| 92 | /…/reclass.py --pretty-print --inventory |
| 93 | /…/reclass.py --pretty-print --nodeinfo localhost |
| 94 | |
| 95 | reclass concepts |
| 96 | ~~~~~~~~~~~~~~~~ |
| 97 | reclass assumes a node-centric perspective into your inventory. This is |
| 98 | obvious when you query reclass for node-specific information, but it might not |
| 99 | be clear when you ask reclass to provide you with a list of groups. In that |
| 100 | case, reclass loops over all nodes it can find in its database, reads all |
| 101 | information it can find about the nodes, and finally reorders the result to |
| 102 | provide a list of groups with the nodes they contain. |
| 103 | |
| 104 | Since the term 'groups' is somewhat ambiguous, it helps to start off with |
| 105 | a short glossary of reclass-specific terminology: |
| 106 | |
| 107 | node: A node, usually a computer in your infrastructure |
| 108 | class: A category, tag, feature, or role that applies to a node |
| 109 | Classes may be nested, i.e. there can be a class hierarchy |
| 110 | application: A specific set of behaviour to apply to members of a class |
| 111 | parameter: Node-specific variables, with inheritance throughout the class |
| 112 | hierarchy. |
| 113 | |
| 114 | A class consists of zero or more parent classes, zero or more applications, |
| 115 | and any number of parameters. |
| 116 | |
| 117 | A node is almost equivalent to a class, except that it usually does not (but |
| 118 | can) specify applications. |
| 119 | |
| 120 | When reclass parses a node (or class) definition and encounters a parent |
| 121 | class, it recurses to this parent class first before reading any data of the |
| 122 | node (or class). When reclass returns from the recursive, depth first walk, it |
| 123 | then merges all information of the current node (or class) into the |
| 124 | information it obtained during the recursion. |
| 125 | |
martin f. krafft | ff1cb06 | 2013-06-20 17:23:00 +0200 | [diff] [blame] | 126 | Furthermore, a node (or class) may define a list of classes it derives from, |
| 127 | in which case classes defined further down the list will be able to override |
| 128 | classes further up the list. |
| 129 | |
martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 130 | Information in this context is essentially one of a list of applications or |
| 131 | a list of parameters. |
| 132 | |
| 133 | The interaction between the depth-first walk and the delayed merging of data |
| 134 | means that the node (and any class) may override any of the data defined by |
| 135 | any of the parent classes (ancestors). This is in line with the assumption |
| 136 | that more specific definitions ("this specific host") should have a higher |
| 137 | precedence than more general definitions ("all webservers", which includes all |
| 138 | webservers in Munich, which includes "this specific host", for example). |
| 139 | |
| 140 | Here's a quick example, showing how parameters accumulate and can get |
| 141 | replaced. |
| 142 | |
| 143 | All unixnodes (i.e. nodes who have the 'unixnodes' class in their ancestry) |
| 144 | have /etc/motd centrally-managed (through the 'motd' application), and the |
| 145 | unixnodes class definition provides a generic message-of-the-day to be put |
| 146 | into this file. |
| 147 | |
| 148 | All debiannodes, which are descendants of unixnodes, should include the |
| 149 | Debian codename in this message, so the message-of-the-day is overwritten in |
| 150 | the debiannodes class. |
| 151 | |
| 152 | The node 'quantum.example.org' will have a scheduled downtime this weekend, |
| 153 | so until Monday, an appropriate message-of-the-day is added to the node |
| 154 | definition. |
| 155 | |
martin f. krafft | ff1cb06 | 2013-06-20 17:23:00 +0200 | [diff] [blame] | 156 | When the 'motd' application runs, it receives the appropriate |
martin f. krafft | a0db070 | 2013-06-20 17:25:01 +0200 | [diff] [blame^] | 157 | message-of-the-day (from 'quantum.example.org' when run on that node) and |
martin f. krafft | ff1cb06 | 2013-06-20 17:23:00 +0200 | [diff] [blame] | 158 | writes it into /etc/motd. |
martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 159 | |
| 160 | At this point it should be noted that parameters whose values are lists or |
| 161 | key-value pairs don't get overwritten by children classes or node definitions, |
| 162 | but the information gets merged (recursively) instead. |
| 163 | |
| 164 | Similarly to parameters, applications also accumulate during the recursive |
| 165 | walk through the class ancestry. It is possible for a node or child class to |
| 166 | _remove_ an application added by a parent class, by prefixing the application |
| 167 | with '~'. |
| 168 | |
| 169 | Finally, reclass happily lets you use multiple inheritance, and ensures that |
| 170 | the resolution of parameters is still well-defined. Here's another example |
| 171 | building upon the one about /etc/motd above: |
| 172 | |
| 173 | 'quantum.example.org' (which is back up and therefore its node definition no |
| 174 | longer contains a message-of-the-day) is at a site in Munich. Therefore, it |
| 175 | is a child of the class 'hosted@munich'. This class is independent of the |
| 176 | 'unixnode' hierarchy, 'quantum.example.org' derives from both. |
| 177 | |
| 178 | In this example infrastructure, 'hosted@munich' is more specific than |
| 179 | 'debiannodes' because there are plenty of Debian nodes at other sites (and |
| 180 | some non-Debian nodes in Munich). Therefore, 'quantum.example.org' derives |
| 181 | from 'hosted@munich' _after_ 'debiannodes'. |
| 182 | |
| 183 | When an electricity outage is expected over the weekend in Munich, the admin |
| 184 | can change the message-of-the-day in the 'hosted@munich' class, and it will |
| 185 | apply to all hosts in Munich. |
| 186 | |
| 187 | However, not all hosts in Munich have /etc/motd, because some of them are |
| 188 | 'windowsnodes'. Since the 'windowsnodes' ancestry does not specify the |
| 189 | 'motd' application, those hosts have access to the message-of-the-day in the |
| 190 | node variables, but the message won't get used… |
| 191 | |
| 192 | … unless, of course, 'windowsnodes' specified a Windows-specific application |
| 193 | to bring such notices to the attention of the user. |
| 194 | |
martin f. krafft | ff1cb06 | 2013-06-20 17:23:00 +0200 | [diff] [blame] | 195 | It's also trivial to ensure a certain order of class evaluation. Here's |
| 196 | another example: |
| 197 | |
| 198 | The 'ssh.server' class defines the 'permit_root_login' parameter to 'no'. |
| 199 | |
| 200 | The 'backuppc.client' class defines the parameter to 'without-password', |
| 201 | because the BackupPC server might need to log in to the host as root. |
| 202 | |
| 203 | Now, what happens if the admin accidentally provides the following two |
| 204 | classes? |
| 205 | |
| 206 | - backuppc.client |
| 207 | - ssh.server |
| 208 | |
| 209 | Theoretically, this would mean 'permit_root_login' gets set to 'no'. |
| 210 | |
martin f. krafft | a0db070 | 2013-06-20 17:25:01 +0200 | [diff] [blame^] | 211 | However, since all 'backuppc.client' nodes need 'ssh.server' (at least in |
| 212 | most setups), the class 'backuppc.client' itself derives from 'ssh.server', |
martin f. krafft | ff1cb06 | 2013-06-20 17:23:00 +0200 | [diff] [blame] | 213 | ensuring that it gets parsed before 'backuppc.client'. |
| 214 | |
| 215 | When reclass returns to the node and encounters the 'ssh.server' class |
martin f. krafft | a0db070 | 2013-06-20 17:25:01 +0200 | [diff] [blame^] | 216 | defined there, it simply skips it, as it's already been processed. |
martin f. krafft | ff1cb06 | 2013-06-20 17:23:00 +0200 | [diff] [blame] | 217 | |
martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 218 | reclass operations |
| 219 | ~~~~~~~~~~~~~~~~~~ |
| 220 | While reclass has been built to support different storage backends through |
| 221 | plugins, currently only the 'yaml_fs' storage backend exists. This is a very |
| 222 | simple, yet powerful, YAML-based backend, using flat files on the filesystem |
| 223 | (as suggested by the _fs postfix). |
| 224 | |
| 225 | yaml_fs works with two directories, one for node definitions, and another for |
| 226 | class definitions. It is possible to use a single directory for both, but that |
| 227 | could get messy and is therefore not recommended. |
| 228 | |
| 229 | Files in those directories are YAML-files, specifying key-value pairs. The |
| 230 | following three keys are read by reclass: |
| 231 | |
| 232 | classes: a list of parent classes |
| 233 | appliations: a list of applications to append to the applications defined by |
| 234 | ancestors. If an application name starts with '~', it would |
| 235 | remove this application from the list, if it had already been |
| 236 | added — but it does not prevent a future addition. |
| 237 | E.g. '~firewalled' |
| 238 | parameters: key-value pairs to set defaults in class definitions, override |
| 239 | existing data, or provide node-specific information in node |
| 240 | specifications. |
| 241 | By convention, parameters corresponding to an application |
| 242 | should be provided as subkey-value pairs, keyed by the name of |
| 243 | the application, e.g. |
| 244 | |
| 245 | applications: |
| 246 | - ssh.server |
| 247 | parameters: |
| 248 | ssh.server: |
| 249 | permit_root_login: no |
| 250 | |
| 251 | reclass starts out reading a node definition file, obtains the list of |
| 252 | classes, then reads the files corresponding to these classes, recursively |
| 253 | reading parent classes, and finally merges the applications list (append |
| 254 | unless |
| 255 | |
martin f. krafft | 9b2049e | 2013-06-14 20:05:08 +0200 | [diff] [blame] | 256 | Version control |
| 257 | ~~~~~~~~~~~~~~~ |
| 258 | I recommend you maintain your reclass inventory database in Git, right from |
| 259 | the start. |
| 260 | |
martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 261 | Usage |
| 262 | ~~~~~ |
| 263 | For information on how to use reclass directly, invoke reclass.py with --help |
| 264 | and study the output. |
| 265 | |
| 266 | More commonly, however, use of reclass will happen indirectly, and through |
| 267 | so-called adapters, e.g. /…/reclass/adapters/ansible. The job of an adapter is |
| 268 | to translate between different invocation paradigms, provide a sane set of |
| 269 | default options, and massage the data from reclass into the format expected by |
| 270 | the automation tool in use. |
| 271 | |
| 272 | Configuration file |
| 273 | ~~~~~~~~~~~~~~~~~~ |
| 274 | reclass can read some of its configuration from a file. The file is |
| 275 | a YAML-file and simply defines key-value pairs. |
| 276 | |
| 277 | The configuration file can be used to set defaults for all the options that |
| 278 | are otherwise configurable via the command-line interface, so please use the |
| 279 | --help output of reclass for reference. The command-line option '--nodes-uri' |
| 280 | corresponds to the key 'nodes_uri' in the configuration file. For example: |
| 281 | |
| 282 | storage_type: yaml_fs |
| 283 | pretty_print: True |
| 284 | output: json |
| 285 | nodes_uri: ../nodes |
| 286 | |
| 287 | reclass first looks in the current directory for the file called |
| 288 | 'reclass-config.yml' and if no such file is found, it looks "next to" the |
| 289 | reclass script itself. Adapters implement their own lookup logic. |
| 290 | |
| 291 | Integration with Ansible |
| 292 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 293 | The integration between reclass and Ansible is performed through an adapter, |
| 294 | and needs not be of our concern too much. |
| 295 | |
| 296 | However, Ansible has no concept of "nodes", "applications", "parameters", and |
| 297 | "classes". Therefore it is necessary to explain how those correspond to |
| 298 | Ansible. Crudely, the following mapping exists: |
| 299 | |
| 300 | nodes hosts |
| 301 | classes groups |
| 302 | applications playbooks |
| 303 | parameters host_vars |
| 304 | |
| 305 | reclass does not provide any group_vars because of its node-centric |
| 306 | perspective. While class definitions include parameters, those are inherited |
| 307 | by the node definitions and hence become node_vars. |
| 308 | |
| 309 | reclass also does not provide playbooks, nor does it deal with any of the |
| 310 | related Ansible concepts, i.e. vars_files, vars, tasks, handlers, roles, etc.. |
| 311 | |
| 312 | Let it be said at this point that you'll probably want to stop using |
| 313 | host_vars, group_vars and vars_files altogether, and if only because you |
| 314 | should no longer need them, but also because the variable precedence rules |
| 315 | of Ansible are full of surprises, at least to me. |
| 316 | |
| 317 | reclass' Ansible adapter massage the reclass output into Ansible-usable data, |
| 318 | namely: |
| 319 | |
| 320 | - Every class in the ancestry of a node becomes a group to Ansible. This is |
| 321 | mainly useful to be able to target nodes during interactive use of |
| 322 | Ansible, e.g. |
| 323 | |
| 324 | ansible debiannode@wheezy -m command -a 'apt-get upgrade' |
| 325 | → upgrade all Debian nodes running wheezy |
| 326 | |
| 327 | ansible ssh.server -m command -a 'invoke-rc.d ssh restart' |
| 328 | → restart all SSH server processes |
| 329 | |
| 330 | ansible mailserver -m command -a 'tail -n1000 /var/log/mail.err' |
| 331 | → obtain the last 1,000 lines of all mailserver error log files |
| 332 | |
| 333 | The attentive reader might stumble over the use of singular words, whereas |
| 334 | it might make more sense to address all 'mailserver*s*' with this tool. |
| 335 | This is convention and up to you. I prefer to think of my node as |
| 336 | a (singular) mailserver when I add 'mailserver' to its parent classes. |
| 337 | |
| 338 | - Every entry in the list of a host's applications might well correspond to |
| 339 | an Ansible playbook. Therefore, reclass creates a (Ansible-)group for |
| 340 | every application, and adds '_hosts' to the name. |
| 341 | |
| 342 | For instance, the ssh.server class adds the ssh.server application to |
| 343 | a node's application list. Now the admin might create an Ansible playbook |
| 344 | like so: |
| 345 | |
| 346 | - name: SSH server management |
| 347 | hosts: ssh.server_hosts ← SEE HERE |
| 348 | tasks: |
| 349 | - name: install SSH package |
| 350 | action: … |
| 351 | … |
| 352 | |
| 353 | There's a bit of redundancy in this, but unfortunately Ansible playbooks |
| 354 | hardcode the nodes to which a playbook applies. |
| 355 | |
martin f. krafft | b608e6d | 2013-06-14 22:10:43 +0200 | [diff] [blame] | 356 | It's now trivial to apply this playbook across your infrastructure: |
| 357 | |
| 358 | ansible-playbook ssh.server.yml |
| 359 | |
| 360 | My suggested way to use Ansible site-wide is then to create a 'site' |
martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 361 | playbook that includes all the other playbooks (which shall hopefully be |
| 362 | based on Ansible roles), and then to invoke Ansible like this: |
| 363 | |
| 364 | ansible-playbook site.yml |
| 365 | |
| 366 | or, if you prefer only to reconfigure a subset of nodes, e.g. all |
| 367 | webservers: |
| 368 | |
| 369 | ansible-playbook site.yml --limit webserver |
| 370 | |
| 371 | Again, if the singular word 'webserver' puts you off, change the |
| 372 | convention as you wish. |
| 373 | |
martin f. krafft | b608e6d | 2013-06-14 22:10:43 +0200 | [diff] [blame] | 374 | And if anyone comes up with a way to directly connect groups in the |
| 375 | inventory with roles, thereby making it unnecessary to write playbook |
| 376 | files (containing redundant information), please tell me! |
| 377 | |
martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 378 | - Parameters corresponding to a node become host_vars for that host. |
| 379 | |
martin f. krafft | 6e9dcba | 2013-06-16 15:21:09 +0200 | [diff] [blame] | 380 | It is possible to include Jinja2-style variables like you would in Ansible, |
| 381 | in parameter values. This is especially powerful in combination with the |
| 382 | recursive merging, e.g. |
| 383 | |
| 384 | parameters: |
| 385 | motd: |
| 386 | greeting: Welcome to {{ ansible_fqdn }}! |
| 387 | closing: This system is part of {{ realm }} |
| 388 | |
| 389 | Now you just need to specify realm somewhere. The reference can reside in |
| 390 | a parent class, while the variable is defined e.g. in the node. |
| 391 | |
martin f. krafft | 3c33322 | 2013-06-14 19:27:57 +0200 | [diff] [blame] | 392 | Contributing to reclass |
| 393 | ~~~~~~~~~~~~~~~~~~~~~~~ |
| 394 | Conttributions to reclass are very welcome. Since I prefer to keep a somewhat |
| 395 | clean history, I will not merge pull requests. Please send your patches using |
| 396 | git-format-patch and git-send-e-mail to reclass@pobox.madduck.net. |
| 397 | |
| 398 | I have added rudimentary unit tests, and it would be nice if you could submit |
| 399 | your changes with appropriate changes to the tests. To run tests, invoke |
| 400 | ./run_tests.py in the top-level checkout directory. |
| 401 | |
| 402 | If you have larger ideas, I'll be looking forward to discuss them with you. |
| 403 | |
martin f. krafft | e39e890 | 2013-06-14 22:12:17 +0200 | [diff] [blame] | 404 | -- martin f. krafft <madduck@madduck.net> Fri, 14 Jun 2013 22:12:05 +0200 |