Resources
Getting Started
Basic concepts
The most basic concept of puppet is resources, puppet treats all you configurations on you machine as a resource, including any files, users, services, and tools, and packages. Via puppet, you can use puppet's resource tool to inspect any existing properties, you can also use puppet's DSL to describe a resource, and puppet and puppet's build-in providers would manage to make the resource there in line with you declarations:
For example, we can user puppet's resource tool to inspect an exists file on you system, command format like this:
puppet resource [resource type] [options]
To inspect the root profile:
puppet resource file /etc/profile
Would result:
file { '/etc/profile':
ensure => 'file',
content => '{md5}1153c583b1bf1dc7779c66af2e509a2b',
ctime => '2014-05-16 08:14:48 -0700',
group => '0',
mode => '0644',
mtime => '2013-10-02 02:06:18 -0700',
owner => '0',
selrange => 's0',
selrole => 'object_r',
seltype => 'etc_t',
seluser => 'system_u',
type => 'file',
}
This is how puppet inspect an exists property on you system, the result is how puppet describe a exists property in a DSL format, but in the opposite direction, the result is also the way how puppet declare a resource.
For example:
puppet apply -e "file { '/home/index.html': ensure => present, }"
Note puppet apply -e
will directly execute the following code as puppet code, normally we would use puppet apply file.pp
, file.pp is the puppet code file which has the resource DSL or classes etc...
This would result a /home/index.html to be create on you system if it's not exists
Resource Syntax
As you can see from above examples, this is how puppet resource DSL formatted:
- Type
- Title
- Attribute value pairs
type {'title':
attribute => 'value',
}
Take the file inspect result as an example:
file { '/etc/profile':
ensure => 'file',
content => '{md5}1153c583b1bf1dc7779c66af2e509a2b',
...
}
Resource Type
Word file
is the resource type, /etc/profile
is the resource title, and the left key value with hash rocket is the attribute value pairs. There are many resource types, file, user, host, cron etc...
Take user
resource type as an example, user below command to create a new user on you system.
puppet apply -e "user {'kobe': ensure => present}"
Which will result a user named with kobe created.
user { 'kobe':
ensure => 'present',
gid => '504',
home => '/home/kobe',
password => '!!',
password_max_age => '99999',
password_min_age => '0',
shell => '/bin/bash',
uid => '504',
}
And we can modify the just created user by:
puppet resource -e user kobe
This command will open a default editor on you system, vim is the default one for most of the linux system, and you can add some changes in the editor, fox example, add a comment in the output the previous command. Note attribute comment for user type, is kind of a full name, for different resource type may have different attributes.
comment => 'kobe bryant',
Once you save and exit the editor, puppet will execute the DSL we just modified and saved. and once again to execute puppet resource user kobe
, you will see the comment output there.
As we mentioned above, all these resource type have different attributes, and you can also define custom resource which would be covered in the up-coming sections. For more resource types, please refer to official puppet Resource Type website.