- Smart Variables
Smart variables allow you to create global parameters in your ENC data, with different values according to certain conditions. 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/ohadlevy/puppet-foreman/blob/master/foreman/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.
In the Variables section, click New Variable, and you will be presented with a set of input fields:
|Variable Name||Variable Description||Default Value||Type||Validation||Order|
- variable_name is what the parameter will be called in the ENC data
- variable_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 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 resulting Smart Variable. See below
- searchlist is a list of variables which Foreman will search (in order) for matches
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_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
The fourth and fifth fields in the Smart Variable combine to produce a validation criteria for the final value of the Smart Variable.
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.
All the ordering information is stored in the sixth field of the Smart Variable (searchlist). The order matters and is used to find the first match.
Example 1 - Simple change to a single host¶
All our hosts use "server.foo" for something, except bob.domain.com which uses "server2.bar":
| target | The target server to talk to | server.foo | string | | fqdn | | hostgroup | | os | | domain | | fqdn = bob.domain.com | 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 to the searchlist:
| port | The port to use | 80 | list | 80,443,8080 | fqdn | | region | | hostgroup | | os | | domain | | region = europe | 8080 | | fqdn = foo.domain | 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.
- Presently it is not possible to search on Parameters