To define a resource:
Type {' title ':
attribute1 = value1,
Attribute2 = value2,
}
Important: Type indicates that the resource type must be lowercase; title is the name of the resource and must be unique under the same type;
Common resource types:
User, group, file, package, service, exec, Cron, notify
Class 3 Special properties for resources:
Namevar: Name variable, the value must be unique (if the property of the Namevar type does not specify a value, the value defined by item is used);
Ensure: Used to control the existence of resources
Ensure =>file exists and is a common file
Ensure =>directory exists and is a directory
Ensure =>present exist, can be used to describe the above three types (default is present)
Ensure =>absent does not exist
Metaparameters: meta-parameter, typically a property that is supported by all types.
Group
Manage Group Resources
Common Properties:
Name: Group name, Namevar
Gid:gid
System:true|false #是否为系统组
Ensure:present, absent
Members: Member in Group
User
Manage users
Common Properties:
Commet: Comment Information
Ensure:present, absent
Expiry: Account expiration period;
GID: Basic Group ID
Groups: Additional groups
Home: Home Directory
Shell: Default shell
Name:namevar
System: Is it user, True|false
Uid:uid
Password
File
Manage files and their content, dependencies, and permissions, which can be given directly through the content property or downloaded from the remote server path via the Source property;
Indicate the source of the file content:
Content: Direct file contents, support \ n (newline), \ t (tab);
Source: Download the file from the specified location;
Recurse:true|false files under the recursive download directory;
Ensure:file, directory, link, present, absent #前3个用于区别文件类型;
Common Properties:
Force: Forced operation, available value Yes, no, true, false (such as creating a file with the name of the directory, the default is not overwritten, if you want to force the creation will delete the directory only retain files);
Group: Genus
Owner: Master
Mode: permissions, support octal format permissions, and U,g,o's empowerment mode
Path: target path;
Source: The path to a file, either a local file path (stand-alone model) or a puppet:///modules/module_name/file_name;
Target: When ensure is "link", target indicates that the file pointed to by path is a symbolic link file that targets the path pointed to by this target property, at which time the content and source properties are automatically invalidated;
#mkdir/manifests
# Vim test1.pp
group{' distro ':
gid = ,
ensure = present,
}
user{' CentOS ':
uid = ,
gid =,
shell =/ Bin/bash '
home = '/home/centos '
ensure = present,
}
file{'/tmp/mydir ':
ensure = Directory,
} #创建空目录/tmp/mydir
file{'/tmp/puppet.file ':
content = ' puppet Testing\nsecond line. ',
ensure = file,
owner = ' CentOS ',
group = ' distro ',
mode = ' 0400 ',
} #创建文件/tmp/puppet.file
file{'/tmp/fstab.puppet ':
Source = '/etc/fstab ',
ensure = File,
} #复制
file{'/tmp/puppet.link ':
ensure = link,
target = '/tmp/puppet.file ',
#创建链接文件, name/tmp/puppet.link, link to/tmp/puppet.file file
#puppet apply-v test1.pp
Exec:
Run an external command, and the command should have "idempotent";
Idempotent: Commands can be run multiple times (such as creating a file, the first time can be created, the second run after the file already exists);
1, the command itself is idempotent;
2, resources have Onlyif, unless,creates and other attributes to achieve the conditional operation of the command;
3, the resource has the Refreshonly attribute, in order to realize only the subscription resources changes, only then executes;
Command: Namevar; (if command is unspecified, the value defined by item will be used)
Creates: Executes this command only if the file specified by this property does not exist;
CWD: Run the command under the path specified in this property to CD to the directory before executing the command;
User: Runs the command as a specified user;
Group: Specifies the identity of the Run command;
Onlyif: Given a test command, Run command-specified commands only if the command executes successfully (returns a status code of 0);
Unless: Given a test command, Run command-specified commands only if the command execution fails (the return status code is not 0);
Refresh: When you accept refresh notifications from other resources, the default is to re-execute the Command,refresh property of the exec definition to change this behavior to specify commands that run only at refresh time;
Refreshonly: This resource is only run when a refresh notification is received; True|false
Returns: The expected status return value, which indicates that the command execution failed when it returns a non-value;
Tries: Number of attempts to execute;
Timeout: Super time length;
Path: Indicates the command search path, its function type PATH environment variable, and its value is usually list [' path1 ', ' path2 ', ...] If this property is not defined, the absolute path of the command must be given;
#vim test2.pp
exec{'/usr/sbin/modprobe ext4 ':
user = root,
Group = root,
Refresh = '/usr/sbin/modprobe-r ext4 &&/usr/sbin/modprobe ext4 ',
Timeout = 5,
Tries = 2,
}
exec{'/bin/echo mageedu >/tmp/hello.txt ':
user = root,
Group = root,
creates = '/tmp/hello.txt ',
}
exec{'/bin/echo mageedu >/tmp/hello2.txt ':
user = root,
Group = root,
unless = '/usr/bin/test-e/tmp/hello2.txt ',
}
Notify
Used to display information
Core Properties:
Message: The content of the messages to be sent; Namevar
#vim test3.pp
notify:{"Hello There":}
#puppet apply-v test3.pp #会显示信息
Cron:
Manage cron task scheduling;
Common Properties:
Ensure:present, absent
Command: the job to run;
Hour
Minute
Month
MonthDay:
Weekday
Name
User: Running Users
Environment: Environment variables at runtime;
#vim test4.pp
cron{"Sync Time":
Command = '/usr/sbin/ntpdate 172.16.0.1 &>/dev/null ',
minute = ' */10 ', #每10分钟运行一次
ensure = present, #默认为present
}
#crontab-L #查看生成了计划任务
#vim test5.pp
cron{"Sync Time":
Command = '/usr/sbin/ntpdate 172.16.0.1 &>/dev/null ',
minute = ' */10 ', #每10分钟运行一次
ensure = absent,
}
#crontab-L #查看计划任务已被删除
Package
Manage packages: Install, uninstall, upgrade, etc features
Common Properties:
ensure:installed, Latest, VERSION (2.3.1-2.EL7), present, absent
Name: package names;
Source: Package origin; Can local file path or URL;
Provider: Indicates the way to install packages, such as RPM or yum. (The default is Yum installation)
package{' zsh ':
ensure = latest,
}
package{' JDK ':
ensure = installed,
Source = '/usr/local/src/jdk-8u25-linux-x64.rpm ',
Provider = RPM,
}
Service
Manage the running services;
Common Properties:
Enable: Whether the boot automatically start, true|false;
Special properties: Metaparameters
There are two main categories: defining dependencies and defining notification relationships; Metaparameters uses 4 meta-parameters to ensure the correlation between resources (Before,require,notify,subscribe);
Each of these 4 meta-parameters is the value of a different other resource or array of resources, also known as a resource reference, and a resource reference to be made through "type[' title '", such as user[' magedu '), when a resource reference is capitalized on the first letter of its type name.
To define a dependency relationship:
Used in dependent resources: before;
Resources that depend on other resources: require;
->: Chain-dependent
Here's an example: to make sure that the additional group Linux is pre-existing when creating a SuSE user, you can refer to the user resource in the group resource to indicate that the user resource group resource is to be created beforehand.
Used in dependent resources: before:
Resources that depend on other resources: require:
depend on multiple resources:
To define a notification relationship:
Used in Dependent resources: Notify
Resources for listening to other resources: Subscribe
~>: Chained notifications
1. Used in Dependent resources: Notify:
To define a resource:
The file resource was modified to modify the port listening in the configuration file to the 808,file resource to notify the Service[nginx] resource to do the refresh operation.
2. Monitoring resources for other resources: Subscribe
Puppet variable and its scope
Variable names begin with $, and the assignment symbol is =; Any data of a non-regular expression type can be assigned to a variable;
Scope: Defines the code's effective scope for inter-code isolation;
Can only isolate: variable, the default property of the resource;
Cannot isolate: The name of the resource, and the reference;
Two reference paths for each variable:
Relative path
Absolute path: $::scope::scope::variable
Assignment Symbols for variables:
=
+ =: Append Assignment
Data type:
Boolean type: Ture, False
UNDEF: The value type of a variable that is not declared (you can also manually assign a undef value to a variable, that is, use an unquoted undef string directly)
Character type: Can not use quotation marks, support single quotation mark (strong reference), double quotation mark (weak reference)
Numeric: integers and floating-point numbers;
Arrays: [Item1, Item2, ...], elements can be any available data type, including arrays and hashes; The index starts at 0 and can also use negative numbers;
Hash:{key = value, key = value,...}, the key is a string, and the value can be any data type;
Regular Expressions:
Non-standard data types, cannot be assigned to variables;
Syntax structure:
(? <enabled option>:<subpattern>)
(?-<disabled option>:<subpattern>)
OPTION:
I: ignoring character case;
M: put. When line break;
x: Ignore whitespace and annotations in the pattern;
The value of the OperatingSystem variable, if it is Ubuntu or Debian, assigns the value of the packages variable to apache2
An expression:
Comparison operators: = =,! =, <, <=, >=, =~ (can be matched by the right pattern),!~ (cannot be matched by right mode), in
Logical operators: And, OR,!
Arithmetic operators: +,-, *,/,%, >> (right Shift), << (left shift)
Examples of variables:
Define a global package name variable, and modify the value of the variable directly, depending on the package installed:
Types of variables in puppet:
Custom variables
Facter variable: Can be directly referenced, (each agent to report the properties of the system and a variety of hardware equipment, such as information to the server side, the server side of the corresponding template to replace the variable name referenced in the client's property values)
See each of the facts supported by puppet:
Facter-p
Built-in variables:
Client Built-in:
$clientcert
$clientversion: Client puppet version number
Server-side built-in
$servername: Server name
$serverip: Server IP Address
$serverversion: Server program file version number
$module _name: module name
Conditional judgment
If, case, selector, unless
If statement:
Single branch:
If CONDITION {
...
}
Dual Branch:
If CONDITION {
...
}
else {
...
}
Usage of condition:
1. Comparison expressions
2. Variable reference
3, there is a return value function call
If statement Example 1:
If $processorcount >1 {
Notice ("SMP Host.") #notice是内置函数, what do you give me?
} else {
Notice ("Poor guy.")
}
If Statement Example 2:
Case statement:
Case Control_expression {
Case1, Case2: {statement}
CASE3, Case4, CASE5: {statement}
...
Default: {statment}
}
Control_expression: expressions, variables, functions (with return values);
Case
String, variable, return value function, schema, default
Case Examples:
Selector statement:
Similar to case, but the function of a branch is not to execute a code fragment, but to return a direct value;
Control_variable? {
Case1 = value1,
Case2 = value2,
...
Default = Valuen
}
Control_variable: A variable, a function with a return value, but not an expression;
Case: Direct value (requires quotation marks), variable, function with return value, regular expression pattern, or default
Puppet defining resources, configuring languages (2)