Installing, configuring, and starting a service
There are many examples of this pattern online. In our simple example, we will create an Apache configuration file under /etc/httpd/conf.d/cookbook.conf
. The /etc/httpd/conf.d
directory will not exist until the httpd
package is installed. After this file is created, we would want httpd
to restart to notice the change; we can achieve this with a notify parameter.
How to do it...
We will need the same definitions as our last example; we need the package and service installed. We now need two more things. We need the configuration file and index page (index.html
) created. For this, we follow these steps:
- As in the previous example, we ensure the service is running and specify that the service requires the
httpd
package:service {'httpd': ensure => running, require => Package['httpd'], }
- We then define the package as follows:
package {'httpd': ensure => installed, }
- Now, we create the
/etc/httpd/conf.d/cookbook.conf
configuration file; the/etc/httpd/conf.d
directory will not exist until thehttpd
package is installed. Therequire
metaparameter tells Puppet that this file requires thehttpd
package to be installed before it is created:file {'/etc/httpd/conf.d/cookbook.conf': content => "<VirtualHost *:80>\nServernamecookbook\nDocumentRoot/var/www/cookbook\n</VirtualHost>\n", require => Package['httpd'], notify => Service['httpd'], }
- We then go on to create an
index.html
file for our virtual host in/var/www/cookbook
. This directory won't exist yet, so we need to create this as well, using the following code:file {'/var/www/cookbook': ensure => directory, } file {'/var/www/cookbook/index.html': content => "<html><h1>Hello World!</h1></html>\n", require => File['/var/www/cookbook'], }
How it works…
The require
attribute to the file resources tell Puppet that we need the /var/www/cookbook
directory created before we can create the index.html
file. The important concept to remember is that we cannot assume anything about the target system (node). We need to define everything on which the target depends. Anytime you create a file in a manifest, you have to ensure that the directory containing that file exists. Anytime you specify that a service should be running, you have to ensure that the package providing that service is installed.
In this example, using metaparameters, we can be confident that no matter what state the node is in before running Puppet, after Puppet runs, the following will be true:
httpd
will be running- The
VirtualHost
configuration file will exist httpd
will restart and be aware of theVirtualHost
file- The
DocumentRoot
directory will exist - An
index.html
file will exist in theDocumentRoot
directory
- 高效能辦公必修課:Word圖文處理
- 嵌入式系統(tǒng)及其開發(fā)應(yīng)用
- 輕松學(xué)PHP
- Natural Language Processing Fundamentals
- 來吧!帶你玩轉(zhuǎn)Excel VBA
- Python Data Science Essentials
- 讓每張照片都成為佳作的Photoshop后期技法
- 大數(shù)據(jù)安全與隱私保護(hù)
- 統(tǒng)計(jì)學(xué)習(xí)理論與方法:R語(yǔ)言版
- Windows游戲程序設(shè)計(jì)基礎(chǔ)
- 激光選區(qū)熔化3D打印技術(shù)
- 簡(jiǎn)明學(xué)中文版Photoshop
- FANUC工業(yè)機(jī)器人配置與編程技術(shù)
- 傳感器原理與工程應(yīng)用
- PHP求職寶典