Smart Variables¶
- Smart Variables
- Creating a Smart Variable
- Validators
- String validators
- Regexp / List validators
- Ordering
- Examples
- Example 1 - Simple change to a single host
- Example 2 - Change for a group of hosts (via custom fact) with validation and ordering
Smart variables are a tool to provide data to your ENC, depending on a set of rules. They are intended to be a stepping stone to full parameterized classes.
It's also possible to retrieve these values if you're not using a ENC, via a custom Puppet function. You'll need to retrieve the value from
https://foreman/hosts/<fqdn>/lookup_keys/<key>You can find a ready-made function for your puppet module at https://github.com/theforeman/puppet-foreman/blob/master/lib/puppet/parser/functions/smartvar.rb
Creating a Smart Variable¶
Start by going to Foreman -> Settings -> Puppetclasses and then click one of your classes to edit it.
Click on the Smart Variables tab. If you have any existing Smart Variables, they will be listed at the left side of the tab.
Click New Variable, and you will be presented with a set of input fields:
| Name |
| Description |
| Default Value |
| Type Validator |
| Validator Constraint |
| Order |
These are:
- name is what the parameter will be called in the ENC data
- description is a free form text box for your own information
- default_value is what the ENC will use if no other criteria is matched
- type validator is a combo-box of data types. The type applies to the next field, the validator. See below
- validator is used to enforce certain values for the Smart Variable. See below for examples
- order is a list of variables which Foreman will search (in order) for the validator
Once you've created the defaults for your Smart variable, you then need to add at least one criterion to match against - click the "plus" under your variable, and two more input fields will appear:
| Match |
| Value |
- match_expression should state a name = value relationship that Foreman use to match against the entries in searchlist
- value is what the parameter should be in the ENC, if this rule is matched
Validators¶
The fourth and fifth fields in the Smart Variable combine to produce a validation criteria for the final value of the Smart Variable.
String validators¶
At present, the string type cannot be validated - leave the validator field blank, and all strings in the variable will be considered acceptable
Regexp / List validators¶
By entering a list (comma-separated, no spaces) or a regex (no delimiter required), the value to be assigned to the Smart Variable will be checked against this list. If the value does not match the validator, and error will be raised.
Ordering¶
All the ordering information is stored in the sixth field of the Smart Variable (order). The order matters and is used to find the first match.
Examples¶
Example 1 - Simple change to a single host¶
All our hosts use "server.foo" for something, except bob.domain.com which uses "server2.bar":
| Name | target |
| Description | The target server to talk to |
| Default Value | server.foo |
| Type Validator | string |
| Validator Constraint | |
| Order | fqdn hostgroup os domain |
| Match | fqdn = bob.domain.com |
| Value | server2.bar |
Example 2 - Change for a group of hosts (via custom fact) with validation and ordering¶
Most hosts need to use a port of "80" but all machines with a fact "region" and value "europe" need to use "8080". To do this, you have to add the factname (in this example "region") to the searchlist:
| Name | port |
| Description | The port to use |
| Default Value | 80 |
| Type Validator | list |
| Validator Constraint | 80,443,8080 |
| Order | fqdn region hostgroup os domain |
| Match | region = europe |
| Value | 8080 |
| Match | fqdn = foo.domain |
| Value | 67 |
Note that all machines will get either 80 or 8080 as required, except foo.domain which will generate an error, since 67 is not in the list validator. Note also that foo.domain will match before region, since it is higher in the searchlist. The rule ordering does not matter.
It is also possible to mix conditions, e.g.| Name | port |
| Description | The port to use |
| Default Value | 80 |
| Type Validator | list |
| Validator Constraint | 80,443,8080 |
| Order | fqdn region, hostgroup, environment hostgroup environment domain |
| Match | fqdn = foo.domain |
| Value | 67 |
| Match | region = europe, hostgroup = "web servers", environment = production |
| Value | 8080 |
