When you are building up a configuration file it is very useful to be able to use variables. If you can define your configuration in terms of some key variables, it can be changed more easily later, it is more transparent to the reader of the program and you can also choose to define the variables differently on different types of system. Another way of saying this is that cfengine variables also belong to classes. Cfengine makes use of variables in three ways.
Environment variables are fetched directly from the shell on whatever
system is running the program. An example of a special variable is the
domain
variable from the previous section. Straightforward macro
substitution allows you to define a symbol name to be replaced by an
arbitrary text string. All these definitions (apart from shell
environment variables, of course) are made in the control part of the
cfengine program:
control: myvar = ( /usr/local/mydir/lib/very/long/path ) # define macro ... links: $(myvar) -> /another/directory
Here we define a macro called myvar
, which is later used to
define the creation of a link. As promised we can also define
class-dependent variables:
control: sun4:: myvar = ( sun ) hpux:: myvar = ( HP )
Cfengine gives you access to the shell environment variables and allows you to define variables of your own. It also keeps a few special variables which affect the way in which cfengine works. When cfengine expands a variable it looks first at the name in its list of special variables, then in the list of user-defined macros and finally in the shell environment for a match. If none of these are found it expands to the empty string.
Variables are referred to in either of two different ways, depending on
your taste. You can use the forms $(variable)
or
${variable}
. The variable in braces or parentheses can be the
name of any user defined macro, environment variable or one of the
following special internal variables.
AllClasses
arch
binserver
class
sun4
, hpux
).
Non-definable.
domain
faculty
fqhost
host
MaxCfengines
OutputPrefix
OutputPrefix = ( "cfengine:$(host):")
RepChar
site
$(faculty)
and may be used interchangeably.
split
sysadm
timezone
control
.
UnderscoreClasses
These variables are kept special because they play a special role in setting up a system configuration. See section Designing a global system configuration. You are encouraged to use them to define fully generalized rules in your programs. Variables can be used to advantage in defining filenames, directory names and in passing arguments to shell commands. The judicious use of variables can reduce many definitions to a single one if you plan carefully.
NOTE: the above control variables are not case sensitive, unlike user macros, so you should not define your own macros with these names.
The following variables are also reserved and may be used to produce troublesome special characters in strings.
cr
dblquote
"
dollar
lf
n
quote
'
.
spc
tab
You can use variables in the following places:
$(binserver)
variable is not always appropriate in this context. For instance
links: osf:: /$(site)/${host}/directory -> somefile
shellcommands: any:: "/bin/echo $(timezone) | /bin/mail $(sysadm)" '/bin/echo "double quotes!"'
The latter possibility enables cfengine's variables to be passed on to user-defined scripts.
option=$(variable)
.
Variables can be defined differently under different classes by preceding the definition with a class name. For example:
control: sun4:: my_macro = ( User_string_1 ) irix:: my_macro = ( User_string_2 )
Here the value assigned to $(my_macro)
depends on which of the
classes evaluates to true. This feature can be used to good effect to
define the mail address of a suitable system administrator for different
groups of host.
control: physics:: sysadm = ( mark,fred ) chemistry:: sysadm = ( localsys@domain )
Note, incidentally, that the `-a' option can be used to print out the mail address of the system administrator for any wrapper scripts.
Go to the first, previous, next, last section, table of contents.