Puppet (2)
Metaparamters:
Multiple resources exist in order, and can be defined in order chain form:
package[' Nginx '), file['/etc/nginx/conf.d/default.conf ', service[' Nginx ']
Multiple resources exist in order of precedence, which can be defined as the form of a notification chain using "~>":
package[' Nginx '] ~> file['/etc/nginx/conf.d/default.conf '] ~> service[' Nginx ']
package{' Nginx ':
Ensure=> installed,
Provider = RPM,
Source=> '/tmp/nginx-1.6.2-1.el6.ngx.x86_64.rpm ',
},
file {'/etc/nginx/conf.d/default.conf ':
ensure=> file,
Source=> '/tmp/default.conf ',
} ~>
service{' Nginx ':
Ensure=> running,
Enable=> true,
require=> package[' Nginx ',
Restart=> '/etc/rc.d/init.d/nginx Reload ',
}
Puppet variable and variable scope:
The variable name should start with $ and the assignment symbol =;
Any data of the non-regular expression type can be assigned directly to the variable name;
Data type:
Boolean: True and false;
UNDEF: The type of the value of the variable that has never been declared;
Character type: unstructured text string; Reference dispensable, single quotation mark as strong reference, double quotation mark as weak reference;
Numeric: integer or floating-point number; The default is a character type, which is treated as a numeric value only in a specific context;
Array: A comma-delimited list of item in brackets [], such as [Item1,item2,...,], whose item can be any data type available, including arrays and hashes, and the index is counted starting at 0, and can be negative; for example: [package[' Nginx '], file[' nginx.conf '],file[' default.conf '];
Hash: defined in {}, a comma-delimited list of "key + value" is the hash type data, where the key is a character type, value can puppet any data type supported, and when referenced, its index is key;
Regular Expressions:
Syntax structure:
/(? <enabled option>:<subpattern>)/
/(?-<disabled option>:<subpattern>)/
OPTION:
I: Ignore character case
M: Use the dot number as a newline character.
x: Ignore whitespace characters and comments in the pattern
Customary usage: (? i-mx:pattern)
$webpackage = $operatingsystem? {
/(? i-mx:ubuntu|debian)/= ' apache2 ',
/(? i-mx:fedora|redhat|centos)/= ' httpd ',
}
Puppet expression:
Comparison operator: = =,! =, <, <=, >=, =~,!~, in
Logical operators: And, OR,!
Arithmetic operators: +,-, *,/, >>, <<
Scope:
The range of code in which the variable is valid;
In puppet, scope can be used to limit the scope of variables and resource default properties, but not to qualify resource names and resource references
Classification of scopes:
Top scope: A top-level scope with a global scope
Node scope: The scope of the nodes, valid for a single node
Class Scope:
Parent scope
Child scope
Referencing a non-current scope accessible variable must be done using the FQN format:
$::c1::c2::webpackage
Types of variables that can be used in puppet:
Custom variables: $webpackage
Facter variable: The variable shown in the execution result of "facter-p" command can be directly referenced;
Built-in variables:
Client Built-in:
$clientcert
$clientversion
Server-side built-in:
$servername
$serverip
$serverversion
$module _name
Append assignment for variable: + =
$users = [' User1 ', ' User2 ',]
$users + = [' User3 ',]
Note: Append-assigned data types are supported:
String: String connection
Arrays: Array Merging
Hash:hash Merging
Puppet condition Judgment:
Conditional statement: If, case, selector, unless
(1) If statement
If CONDITION {...}
If CONDITION {...} else {...}
If CONDITION {...} elsif CONDITION {...} ... else {...}
Available Condition:
Variable
An expression
Functions that have return values
If $operatingsystem =~/^ (? I-mx: (ubuntu| RedHat)/{notice ("Welcome to $ Linux distribution.")} else {notice ("Welcome to Unkown World.")}
(2) Case
Syntax structure:
Case Control_expression {case1: {}case2, case3: {}default: {}}
Control_expression:
A variable, an expression, a function with a return value
Available data types for each case:
A string (reference), a variable, a function with a single return value, a regular expression pattern, or default;
Case $operatingsystem {' CentOS ', ' Redhat ', ' Fedora ': {notice ("Welcome to Redhat OS Family.")} /^ (i-mx: (Ubuntu|debian))/: {Notice ("Welcome to Linux Distribution.")} Default: {Notice ("Alien.")}
(3) Selector
Similar to case, but unlike case matching to a branch after the corresponding code snippet is executed, selector returns a value directly after matching to a branch;
Syntax structure:
Selector Control_variables? {case1 = value1,case2=> value2,... default = Valuen,}
Control_variables: A variable or a function with a return value;
Available data types for each case:
A string (reference), a variable, a function with a single return value, a regular expression pattern, or default;
Note: The selector case cannot use the list;
The data types available for the value of each case:
A direct value other than a hash, a variable, a function that can call a return value, or other selector;
Class in Puppet:
A set of resources for public purposes, named code blocks, which can be called globally after creation, and classes can be inherited; puppet;
Syntax format:
Class Name {... puppet code ...}
A defined class is executed only if it is called (declared);
Class Nginxsrv {package{' nginx ':ensure=> installed,provider = rpm,source=> '/tmp/nginx-1.6.2-1.el6.ngx.x86 _64.rpm ',}-file {'/etc/nginx/conf.d/default.conf ':ensure=> file,source=> '/tmp/default.conf ',} ~> service{' Nginx ':ensure=> running,enable=> true,require=> package[' nginx '],restart=> '/etc/rc.d/init.d/ Nginx reload ',}}include nginxsrv
Note: (1) The class name can contain lowercase letters, numbers, and underscores, but only lowercase letters begin;
(2) The class will introduce a new variable scope;
This article is from the "My Study blog" blog, please be sure to keep this source http://houzhimeng.blog.51cto.com/3938990/1735177
Puppet variables, data types and classes (03)