We need some packages for our server. Now our server is configured to use Chef and talk to a Chef server, let's install a few packages such as the Apache server, PHP, and MariaDB to build a classic LAMP server on a CentOS 7.2 server.
To work through this recipe, you will need the following:
To install a package on a Red Hat-based system, we'd use either yum
(until CentOS 7) or dnf
(for Fedora after version 22). As we're using a CentOS 7 server, the Apache2 HTTP server package name is httpd
, (it's apache2
on Debian-based systems). Manually, we would have typed the following:
$ dnf install httpd $ yum install httpd
Let's see how this translates into a repeatable process with a Chef cookbook.
Let's start by creating an empty cookbook from inside the Chef repository cookbooks
folder to install Apache2 using the chef
command:
$ cd chef-repo/cookbooks $ chef generate cookbook apache Generating cookbook apache [...] Your cookbook is ready. Type `cd apache` to enter it. [...] If you'd prefer to dive right in, the default recipe can be found at: recipes/default.rb
Now we need to tell Chef to install a package using the package
resource.
Open that apache/recipes/default.rb
file and type in the following:
package "httpd"
That's the most basic way we can tell Chef to install a package. This will do the install
action by default. To be a little bit more comprehensive, we can use the full block to do the same:
package "httpd" do action :install end
Still from inside the Chef repository, we now need to upload this new apache
cookbook to the Chef server, so our servers can access it. To do this, we use the knife
command on our workstation:
$ knife cookbook upload apache Uploading apache [0.1.0] Uploaded 1 cookbook.
We just uploaded our first cookbook on the Chef server!
Let's confirm the cookbook is available remotely on the Chef server:
$ knife cookbook list apache 0.1.0 starter 1.0.0
Now we have the apache
cookbook remotely available, let's tell the Chef server that our particular node has to run it. Two options here are as follows:
knife
CLI on the workstation, run the following:$ knife node run_list add <nodename> apache nodename: run_list: recipe[apache]
Either way, we just told the Chef server to apply the apache
cookbook on this particular server. Let's launch the Chef client on our remote node:
$ sudo chef-client Starting Chef Client, version 12.15.19 resolving cookbooks for run list: ["apache"] Synchronizing Cookbooks: - apache (0.1.0) Installing Cookbook Gems: Compiling Cookbooks... Converging 1 resources Recipe: apache::default * yum_package[httpd] action install - install version 2.4.6-40.el7.centos.4 of package httpd Running handlers: Running handlers complete Chef Client finished, 1/1 resources updated in 32 seconds
Chef just installed the Apache HTTP server package for us! If we launch the Chef client, it won't install it again, as it knows it's already there (look at the largely different execution times):
$ sudo chef-client [...] Recipe: apache::default * yum_package[httpd] action install (up to date) [...] Chef Client finished, 0/1 resources updated in 04 seconds
Verify if the package is really installed:
$ which httpd /usr/sbin/httpd $ httpd -v Server version: Apache/2.4.6 (CentOS) Server built: Jul 18 2016 15:30:14
Let's use our knowledge to create a MariaDB cookbook the same way we just deployed Apache, from the Chef repository:
$ chef generate cookbook cookbooks/mariadb
We want to install two packages: mariadb
for the client and the libraries, and mariadb-server
for the server. Add the following on the mariadb/recipes/default.rb
file:
package "mariadb" do action :install end package "mariadb-server" do action :install end
Alternatively, as we're writing plain Ruby, let's rewrite it in a more idiomatic way:
%w(mariadb mariadb-server).each do |name| package name do action :install end end
Upload the cookbook from your workstation:
$ knife cookbook upload mariadb
Add the mariadb
cookbook to the remote node's run list from your workstation:
$ knife node run_list add <nodename> mariadb
Run the Chef client on the remote host:
$ sudo chef-client Starting Chef Client, version 12.15.19 resolving cookbooks for run list: ["apache", "mariadb"] Synchronizing Cookbooks: - apache (0.1.0) - mariadb (0.1.0) [...] Recipe: mariadb::default * yum_package[mariadb] action install - install version 5.5.50-1.el7_2 of package mariadb * yum_package[mariadb-server] action install - install version 5.5.50-1.el7_2 of package mariadb-server [...] Chef Client finished, 2/3 resources updated in 25 seconds
Verify that the MariaDB package is correctly installed:
$ which mysql /usr/bin/mysql $ mysql --version mysql Ver 15.1 Distrib 5.5.50-MariaDB, for Linux (x86_64) using readline 5.1
Let's reuse our knowledge to create a cookbook that will install the packages needed for PHP support:
$ chef generate cookbook cookbooks/php
Let's add our Chef code that will install the following three packages: the php
, php-cli
, and php-mysql
packages (respectively for PHP support, command line, and PHP/MySQL support) in the cookbooks/php/recipes/default.rb
file:
%w(php php-mysql).each do |name| package name do action :install end end
Upload this new php
cookbook from your workstation:
$ knife cookbook upload php
Add the php
cookbook to the remote node's run list from your workstation:
$ knife node run_list add vagrant php
Run the Chef client on the remote node:
$ sudo chef-client $ php --version PHP 5.4.16 (cli) (built: Aug 11 2016 21:24:59)
We now know the very basics of deploying cookbooks and installing packages on a remote node, using Chef!
Using Puppet, a package installation is done using the package
resource directive. The following example shows how to install an Apache 2.x server on Ubuntu systems:
package { 'apache2: ensure => installed; }
To deploy a LAMP server on the box web.pomes.pro
, we need Apache2, PHP, and the MariaDB server. In order to do a real example, perform the following steps:
puppetcode
directory, which is the shared folder between your workstation and the Puppet server: cd puppetcode
apache
, php
, and mariadb
), so let's create a minimalist module layout for them:mkdir modules/apache mkdir modules/apache/manifests mkdir modules/apache/templates mkdir modules/php mkdir modules/php/manifests mkdir modules/php/templates mkdir modules/mariadb mkdir modules/mariadb/manifests mkdir modules/mariadb/templates
module/apache/manifests/init.pp
manifest file with the following content:class apache { package {'apache2': ensure => present, } }
module/php/manifests/init.pp
manifest file with the following content:class php { package {['php','php-mysql','libapache2-mod-php']: ensure => present, } }
module/mariadb/manifests/init.pp
manifest file with the following content:class mariadb { package {'mariadb-server': ensure => present, } }
manifests/site.pp
, with the following content:node 'web.pomes.pro' { class { 'apache':; 'php':; 'mariadb':; } }
That's it! With a few lines of code, all necessary binaries will be installed. We can now apply changes, using puppet agent --test
:
$ vagrant ssh web.pomes.pro Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-51-generic x86_64) ... ... vagrant@web:~$ sudo -i root@web:~# puppet agent --test Info: Creating a new SSL key for web.pomes.pro Info: Caching certificate for ca Info: csr_attributes file loading from /etc/puppetlabs/puppet/csr_attributes.yaml Info: Creating a new SSL certificate request for web.pomes.pro Info: Certificate Request fingerprint (SHA256): 12:9E:DD:E5:85:C9:F2:56:92:1B:92:93:0A:3C:7B:00:DE:2A:45:C0:D9:F8:F6:D0:EC:9D:0B:6E:42:7E:74:33 Info: Caching certificate for web.pomes.pro Info: Caching certificate_revocation_list for ca Info: Caching certificate for ca Info: Using configured environment 'production' Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for web.pomes.pro Info: Applying configuration version '1477085080' Notice: /Stage[main]/Apache/Package[apache2]/ensure: created Notice: /Stage[main]/Php/Package[php]/ensure: created Notice: /Stage[main]/Php/Package[php-mysql]/ensure: created Notice: /Stage[main]/Php/Package[libapache2-mod-php]/ensure: created Notice: /Stage[main]/Mariadb/Package[mariadb-server]/ensure: created Notice: Applied catalog in 59.77 seconds
package
resource documentation: https://docs.chef.io/resource_package.html18.117.232.239