Ceci est une ancienne révision du document !


Version : 2023.01

Dernière mise-à-jour : 2023/11/01 15:09

LDF703 - Cookbooks et Recipes

Contenu du Module

  • LDF703 - Cookbooks et Recipes
    • Contenu du Module
    • Présentation,
      • Chef Cookbooks,
      • Chef Recipes.
    • LAB #1 - Installer et Configurer Apache
      • 1.1 - Création du Cookbook
      • 1.2 - Téléverser le Cookbook vers le Chef Infra Server
      • 1.3 - Configurer un Client
      • 1.4 - Vérifier l'Installation et la Configuration du Client
    • LAB #2 - Chef Supermarket
      • 2.1 - Présentation
      • 2.2 - Se Connecter et Configurer
      • 2.3 - Publication d'un Cookbook
    • LAB #3 - Installer et Configurer un Conteneur Docker
      • 3.1 - Préparation
      • 3.2 - Installation

Présentation

Chef Cookbooks

Un Chef Cookbook est une collection de :

  • Recipes ou Recettes,
  • Metadata ou Metadonnées,
  • Attributes ou Attributs,
  • Ressources ou Resources,
  • Templates ou Gabarits.

Un Cookbook ainsi qu'une recipe :

  • peut être exécuté sur un client,
  • est stocké dans un Chef Infra Server.

Recipes

Une Chef Recipe est :

  • un fichier écrit en Ruby qui utilise le Chef DSL,
  • définit l'état du client ainsi que les tâches nécessaires pour y parvenir.

LAB #1 - Installer et Configurer Apache

1.1 - Création du Cookbook

Commencez par vous placer dans le répertoire /root/chef-repo/cookbooks :

root@workstation:~/chef-repo/.chef# cd ../cookbooks
root@workstation:~/chef-repo/cookbooks# 

Utilisez la commande chef generate cookbook pour créer une arborescence vierge d'un Cookbook dénommé apache2 :

root@workstation:~/chef-repo/cookbooks# chef generate cookbook apache2
+---------------------------------------------+
            Chef License Acceptance

Before you can continue, 3 product licenses
must be accepted. View the license at
https://www.chef.io/end-user-license-agreement/

Licenses that need accepting:
  * Chef Workstation
  * Chef Infra Client
  * Chef InSpec

Do you accept the 3 product licenses (yes/no)?

> yes

Vous obtiendrez :

root@workstation:~/chef-repo/cookbooks# chef generate cookbook apache2
+---------------------------------------------+
            Chef License Acceptance

Before you can continue, 3 product licenses
must be accepted. View the license at
https://www.chef.io/end-user-license-agreement/

Licenses that need accepting:
  * Chef Workstation
  * Chef Infra Client
  * Chef InSpec

Do you accept the 3 product licenses (yes/no)?

> yes

Persisting 3 product licenses...
✔ 3 product licenses persisted.

+---------------------------------------------+
Generating cookbook apache2
- Ensuring correct cookbook content
- Committing cookbook files to git

Your cookbook is ready. Type `cd apache2` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list of local testing commands.

Why not start by writing an InSpec test? Tests for the default recipe are stored at:

test/integration/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb

Placez-vous dans le répertoire apache2 et constatez son contenu :

root@workstation:~/chef-repo/cookbooks# cd apache2

root@workstation:~/chef-repo/cookbooks/apache2# ls -l
total 40
-rw-r--r-- 1 root root  152 oct.  31 10:02 CHANGELOG.md
-rw-r--r-- 1 root root 1176 oct.  31 10:02 chefignore
-rw-r--r-- 1 root root  741 oct.  31 10:02 kitchen.yml
-rw-r--r-- 1 root root   70 oct.  31 10:02 LICENSE
-rw-r--r-- 1 root root  680 oct.  31 10:02 metadata.rb
-rw-r--r-- 1 root root  510 oct.  31 10:02 Policyfile.rb
-rw-r--r-- 1 root root   55 oct.  31 10:02 README.md
drwxr-xr-x 2 root root 4096 oct.  31 10:02 recipes
drwxr-xr-x 3 root root 4096 oct.  31 10:02 spec
drwxr-xr-x 3 root root 4096 oct.  31 10:02 test

Dans le répertoire recipes se trouve un fichier default.rb, un recipe par défaut, mais qui est actuellement vide :

root@workstation:~/chef-repo/cookbooks/apache2# cd recipes/

root@workstation:~/chef-repo/cookbooks/apache2/recipes# ls
default.rb

root@workstation:~/chef-repo/cookbooks/apache2/recipes# cat default.rb 
#
# Cookbook:: apache2
# Recipe:: default
#
# Copyright:: 2023, The Authors, All Rights Reserved.

Modifiez ce fichier avec le contenu suivant :

root@workstation:~/chef-repo/cookbooks/apache2/recipes# vi default.rb 
root@workstation:~/chef-repo/cookbooks/apache2/recipes# cat default.rb 
#
# Cookbook:: apache2
# Recipe:: default
#
# Copyright:: 2023, The Authors, All Rights Reserved.
package 'apache2'

service 'apache2' do
  action [:enable, :start]
end

template '/var/www/html/index.html' do
  source 'index.html.erb'
end

Dans ce fichier :

  • package 'apache2' installe le paquet du même nom. Puisque aucune action n'est précisée, l'action par défaut est install,
  • service 'apache2' active le service grâce à l'action :enable et démarre le service grâce à l'action :start,
  • template '/var/www/html/index.html' crée le fichier référencé sur le client en utilisant le gabarit index.html.erb.

Puisque le Cookbook fait référence à un gabarit, nous avons besoin d'un répertoire templates dans l'arborescence de notre Cookbook :

root@workstation:~/chef-repo/cookbooks/apache2/recipes# cd ..

root@workstation:~/chef-repo/cookbooks/apache2# ls
CHANGELOG.md  kitchen.yml  metadata.rb    README.md  spec
chefignore    LICENSE      Policyfile.rb  recipes    test

root@workstation:~/chef-repo/cookbooks/apache2# mkdir templates

root@workstation:~/chef-repo/cookbooks/apache2# ls
CHANGELOG.md  kitchen.yml  metadata.rb    README.md  spec       test
chefignore    LICENSE      Policyfile.rb  recipes    templates

Créez donc le fichier index.html.erb dans le répertoire templates :

root@workstation:~/chef-repo/cookbooks/apache2# vi templates/index.html.erb
root@workstation:~/chef-repo/cookbooks/apache2# cat templates/index.html.erb
<html>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

1.2 - Téléverser le Cookbook vers le Chef Infra Server

Le Cookbook est maintenant terminé. Il convient donc de le téléverser vers le Chef Infra Server en utilisant la commande knife cookbook upload et en spécifiant le nom du Cookbook apache2 :

root@workstation:~/chef-repo/cookbooks# knife cookbook upload apache2
Uploading apache2      [0.1.0]
Uploaded 1 cookbook.

Il est possible de visualiser les sous-commandes disponibles de la commande knife cookbook :

root@workstation:~/chef-repo/cookbooks# knife cookbook --help
FATAL: Cannot find subcommand for: 'cookbook --help'
Available cookbook subcommands: (for details, knife SUB-COMMAND --help)

** COOKBOOK COMMANDS **
knife cookbook bulk delete REGEX (options)
knife cookbook delete COOKBOOK VERSION (options)
knife cookbook download COOKBOOK [VERSION] (options)
knife cookbook list (options)
knife cookbook metadata COOKBOOK (options)
knife cookbook metadata from file FILE (options)
knife cookbook show COOKBOOK [VERSION] [PART] [FILENAME] (options)
knife cookbook upload [COOKBOOKS...] (options)

Il est possible d'obtenir de l'aide concernant les sous-commandes telle knife cookbook upload :

root@workstation:~/chef-repo/cookbooks# knife cookbook upload --help
knife cookbook upload [COOKBOOKS...] (options)
    -a, --all                        Upload all cookbooks, rather than just a single cookbook.
    -s, --server-url URL             Chef Infra Server URL.
        --chef-zero-host HOST        Host to start Chef Infra Zero on.
        --chef-zero-port PORT        Port (or port range) to start Chef Infra Zero on. Port ranges like 1000,1010 or 8889-9999 will try all given ports until one works.
    -k, --key KEY                    Chef Infra Server API client key.
        --[no-]color                 Use colored output, defaults to enabled.
        --concurrency NUMBER_OF_THREADS
                                     How many concurrent threads will be used.
    -c, --config CONFIG              The configuration file to use.
        --config-option OPTION=VALUE Override a single configuration option.
    -o, --cookbook-path PATH:PATH    A colon-separated path to look for cookbooks in.
        --defaults                   Accept default values for all questions.
        --include-dependencies       Also upload cookbook dependencies.
    -d, --disable-editing            Do not open EDITOR, just accept the data as is.
    -e, --editor EDITOR              Set the editor to use for interactive commands.
    -E, --environment ENVIRONMENT    Set ENVIRONMENT's version dependency match the version you're uploading.
        --[no-]fips                  Enable FIPS mode.
        --force                      Update cookbook versions even if they have been frozen.
    -F, --format FORMAT              Which format to use for output. (valid options: 'summary', 'text', 'json', 'yaml', or 'pp')
        --freeze                     Freeze this version of the cookbook so that it cannot be overwritten.
        --[no-]listen                Whether a local mode (-z) server binds to a port.
    -z, --local-mode                 Point knife commands at local repository instead of Chef Infra Server.
    -u, --user USER                  Chef Infra Server API client username.
        --print-after                Show the data after a destructive operation.
        --profile PROFILE            The credentials profile to select.
    -V, --verbose                    More verbose output. Use twice for max verbosity.
    -v, --version                    Show Chef Infra Client version.
    -y, --yes                        Say yes to all prompts for confirmation.
    -h, --help                       Show this help message.

Connectez-vous ensuite à votre VM ChefServer_10.0.2.110_VNC ou ChefServer_10.0.3.110_VNC selon votre numéro de stagiaire.

Ouvrez le navigateur Web et naviguez à l'adresse 10.0.2.110 ou 10.0.3.110 selon le cas :

Cliquez sur l'onglet Policy et vérifiez que le Cookbook y a été téléchargé :

Cliquez sur le Cookbook apache2 :

Dans la panneau inférieur, cliquez sur Recipes :

Cliquez sur default.rb pour voir le contenu du fichier :

Ensuite, cliquez sur Templates puis index.html.erb pour voir le contenu du fichier :

1.3 - Configurer un Client

Connectez-vous ensuite à votre VM ChefServer_10.0.2.110_SSH ou ChefServer_10.0.3.110_SSH selon votre numéro de stagiaire.

Éditez le fichier /etc/hosts dans votre VM workstation, selon votre numéro de stagiaire, pour référencer les VMs client.ittraining.loc et docker.ittraining.loc.

Stagiaires 11, 12, 14, 15, 16 et 17 :

root@workstation:~# vi /etc/hosts
root@workstation:~# cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       workstation
10.0.2.110      chefserver.ittraining.loc   chefserver
10.0.2.111      workstation.ittraining.loc  workstation
10.0.2.112      client.ittraining.loc       client
10.0.2.114      docker.ittraining.loc       docker

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Stagiaires 18, 19, 20, 21, 22 et 23 :

root@workstation:~# vi /etc/hosts
root@workstation:~# cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       workstation
10.0.3.110      chefserver.ittraining.loc   chefserver
10.0.3.111      workstation.ittraining.loc  workstation
10.0.3.112      client.ittraining.loc       client
10.0.3.114      docker.ittraining.loc       docker

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Connectez-vous à votre VM client.

Stagiaires 11, 12, 14, 15, 16 et 17 :

root@workstation:~/chef-repo/cookbooks# ssh -l trainee client
The authenticity of host 'client (10.0.2.112)' can't be established.
ECDSA key fingerprint is SHA256:RRU4Tim5H5klLxXUa8hyodLq5DSGrKuZ07jziXLVmFw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'client,10.0.2.112' (ECDSA) to the list of known hosts.
trainee@client's password: trainee
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-150-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Expanded Security Maintenance for Infrastructure is not enabled.

0 updates can be applied immediately.

122 additional security updates can be applied with ESM Infra.
Learn more about enabling ESM Infra service for Ubuntu 18.04 at
https://ubuntu.com/18-04

New release '20.04.6 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2023.
Last login: Mon Oct 30 13:11:30 2023 from 10.0.2.1

trainee@client:~$

Stagiaires 18, 19, 20, 21, 22 et 23 :

root@workstation:~/chef-repo/cookbooks# ssh -l trainee client
The authenticity of host 'client (10.0.3.112)' can't be established.
ECDSA key fingerprint is SHA256:RRU4Tim5H5klLxXUa8hyodLq5DSGrKuZ07jziXLVmFw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'client,10.0.3.112' (ECDSA) to the list of known hosts.
trainee@client's password: trainee
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-150-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Expanded Security Maintenance for Infrastructure is not enabled.

0 updates can be applied immediately.

122 additional security updates can be applied with ESM Infra.
Learn more about enabling ESM Infra service for Ubuntu 18.04 at
https://ubuntu.com/18-04

New release '20.04.6 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2023.
Last login: Mon Oct 30 13:11:30 2023 from 10.0.3.1

trainee@client:~$

Devenez l'utilisateur root et éditez le fichier /etc/ssh/sshd_config en modifiant la directive PermitRootLogin à yes :

trainee@client:~$ sudo su -
[sudo] password for trainee: trainee

root@client:~# vi /etc/ssh/sshd_config 

root@client:~# cat /etc/ssh/sshd_config | grep PermitRoot
PermitRootLogin yes 
# the setting of "PermitRootLogin without-password".

Activez le compte root en spécifiant le mot de passe fenestros :

root@client:~# passwd
Enter new UNIX password: fenestros
Retype new UNIX password: fenestros
passwd: password updated successfully

Redémarrez le service ssh et quittez la VM client :

root@client:~# systemctl restart ssh

root@client:~# systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2023-10-31 12:17:39 CET; 6s ago
  Process: 774 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
  Process: 769 ExecReload=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
  Process: 1039 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
 Main PID: 1040 (sshd)
    Tasks: 1 (limit: 4656)
   CGroup: /system.slice/ssh.service
           └─1040 /usr/sbin/sshd -D

oct. 31 12:17:39 client.ittraining.loc systemd[1]: Starting OpenBSD Secure Shell serve
oct. 31 12:17:39 client.ittraining.loc sshd[1040]: Server listening on 0.0.0.0 port 22
oct. 31 12:17:39 client.ittraining.loc sshd[1040]: Server listening on :: port 22.
oct. 31 12:17:39 client.ittraining.loc systemd[1]: Started OpenBSD Secure Shell server

root@client:~# exit
logout

trainee@client:~$ exit
logout
Connection to client closed.

Éditez le fichier /etc/hosts dans votre VM chefserver, selon votre numéro de stagiaire, pour référencer les VMs chefserver.ittraining.loc, workstation.ittraining.loc, client.ittraining.loc et docker.ittraining.loc.

Stagiaires 11, 12, 14, 15, 16 et 17 :

root@workstation:~/chef-repo/cookbooks# exit
logout

trainee@workstation:~$ exit
logout
Connection to 10.0.2.111 closed.

trainee@chefserver:~$ sudo su -
[sudo] password for trainee: trainee

root@chefserver:~# vi /etc/hosts

root@chefserver:~# cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       chefserver
10.0.2.110      chefserver.ittraining.loc   chefserver
10.0.2.111      workstation.ittraining.loc  workstation
10.0.2.112      client.ittraining.loc       client
10.0.2.114      docker.ittraining.loc       docker

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Stagiaires 18, 19, 20, 21, 22 et 23 :

root@workstation:~/chef-repo/cookbooks# exit
logout

trainee@workstation:~$ exit
logout
Connection to 10.0.3.111 closed.

trainee@chefserver:~$ sudo su -
[sudo] password for trainee: trainee

root@chefserver:~# vi /etc/hosts

root@chefserver:~# cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       chefserver
10.0.3.110      chefserver.ittraining.loc   chefserver
10.0.3.111      workstation.ittraining.loc  workstation
10.0.3.112      client.ittraining.loc       client
10.0.3.114      docker.ittraining.loc       docker

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Retournez ensuite dans votre VM workstation.

Stagiaires 11, 12, 14, 15, 16 et 17 :

root@chefserver:~# ssh -l trainee workstation
The authenticity of host 'workstation (10.0.2.111)' can't be established.
ECDSA key fingerprint is SHA256:RRU4Tim5H5klLxXUa8hyodLq5DSGrKuZ07jziXLVmFw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'workstation' (ECDSA) to the list of known hosts.
trainee@workstation's password: trainee
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-150-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Expanded Security Maintenance for Infrastructure is not enabled.

0 updates can be applied immediately.

122 additional security updates can be applied with ESM Infra.
Learn more about enabling ESM Infra service for Ubuntu 18.04 at
https://ubuntu.com/18-04

New release '20.04.6 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2023.
Last login: Tue Oct 31 09:55:34 2023 from 10.0.2.110

Stagiaires 18, 19, 20, 21, 22 et 23 :

root@chefserver:~# ssh -l trainee workstation
The authenticity of host 'workstation (10.0.3.111)' can't be established.
ECDSA key fingerprint is SHA256:RRU4Tim5H5klLxXUa8hyodLq5DSGrKuZ07jziXLVmFw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'workstation' (ECDSA) to the list of known hosts.
trainee@workstation's password: trainee
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-150-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Expanded Security Maintenance for Infrastructure is not enabled.

0 updates can be applied immediately.

122 additional security updates can be applied with ESM Infra.
Learn more about enabling ESM Infra service for Ubuntu 18.04 at
https://ubuntu.com/18-04

New release '20.04.6 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2023.
Last login: Tue Oct 31 09:55:34 2023 from 10.0.2.110

Placez-vous de nouveau dans le répertoire /root/chef-repo/cookbooks en tant que root :

trainee@workstation:~$ sudo su -
[sudo] password for trainee: trainee

root@workstation:~# cd /root/chef-repo/cookbooks
root@workstation:~/chef-repo/cookbooks# 

Utilisez la commande knife bootstrap pour référencer le client avec le Chef Infra Server et exécuter le recipe.

Stagiaires 11, 12, 14, 15, 16 et 17 :

root@workstation:~/chef-repo/cookbooks# knife bootstrap 10.0.2.112 --ssh-user root --ssh-password fenestros --node-name client --run-list 'recipe[apache2]'
--ssh-user: This flag is deprecated. Use -U/--connection-user instead.
--ssh-password: This flag is deprecated. Use -P/--connection-password instead.
Connecting to 10.0.2.112
The authenticity of host '10.0.2.112 ()' can't be established.
fingerprint is SHA256:s6fmqsOVfoId4uzkHMfeFs67Qkr9GDvO/iom+YUwI3k.

Are you sure you want to continue connecting
? (Y/N) Y

Stagiaires 18, 19, 20, 21, 22 et 23 :

root@workstation:~/chef-repo/cookbooks# knife bootstrap 10.0.3.112 --ssh-user root --ssh-password fenestros --node-name client --run-list 'recipe[apache2]'
--ssh-user: This flag is deprecated. Use -U/--connection-user instead.
--ssh-password: This flag is deprecated. Use -P/--connection-password instead.
Connecting to 10.0.3.112
The authenticity of host '10.0.3.112 ()' can't be established.
fingerprint is SHA256:s6fmqsOVfoId4uzkHMfeFs67Qkr9GDvO/iom+YUwI3k.

Are you sure you want to continue connecting
? (Y/N) Y

A l'issu de l'installation vous obtiendrez la sortie suivante.

Stagiaires 11, 12, 14, 15, 16 et 17 :

...
Running handlers:
 [10.0.2.112] Running handlers complete

 [10.0.2.112] Chef Infra Client finished, 2/4 resources updated in 22 seconds
 [10.0.2.112] [2023-10-31T12:32:50+01:00] WARN: This release of Chef Infra Client became end of life (EOL) on May 1st 2021. Please update to a supported release to receive new features, bug fixes, and security updates.

Stagiaires 18, 19, 20, 21, 22 et 23 :

...
Running handlers:
 [10.0.3.112] Running handlers complete

 [10.0.3.112] Chef Infra Client finished, 2/4 resources updated in 22 seconds
 [10.0.3.112] [2023-10-31T12:32:50+01:00] WARN: This release of Chef Infra Client became end of life (EOL) on May 1st 2021. Please update to a supported release to receive new features, bug fixes, and security updates.

1.4 - Vérifier l'Installation et la Configuration du Client

Connectez-vous ensuite à votre VM ChefServer_10.0.2.110_VNC ou ChefServer_10.0.3.110_VNC selon votre numéro de stagiaire.

Ouvrez un autre onglet dans le navigateur et naviguez à l'adresse 10.0.2.112 ou 10.0.3.112 :

LAB #2 - Chef Supermarket

2.1 - Présentation

Le Chef Supermarket est :

  • un site web communautaire destiné aux utilisateurs de Chef et hébergé par Chef Software,
  • disponible à l'adresse https://supermarket.chef.io,
  • un dépôt de Cookbooks.

Important - Notez qu'il est possible de créer votre propre Chef Supermarket hébérgé sur votre réseau.

2.2 - Se Connecter et Configurer

Connectez-vous ensuite à votre VM ChefServer_10.0.2.110_VNC ou ChefServer_10.0.3.110_VNC selon votre numéro de stagiaire.

Ouvrez le navigateur Web et naviguez à l'adresse https://supermarket.chef.io :

Cliquez sur le bouton Sign Up For a Chef Account :

A l'issu du processus de la création de votre compte, cliquez sur le lien dans l'email qui vous a été adressé :

Cliquez sur le lien Sign Out :

Retournez à https://supermarket.chef.io et connectez-vous avec votre compte :

IMAGE

Cliquez sur le bouton Yes afin que le Supermarket puisse utiliser votre compte :

Vous obtiendrez :

Ouvrez un autre onglet et naviguez à https://id.chef.io/id/signin. Connectez-vous en utilisant votre compte :

Cliquez sur le lien SIGNED IN AS: <votre_compte> :

Cliquez ensuite sur le bouton Get a New Key :

Important - Un fichier dénommé <votre_compte>.pem sera téléchargé vers le Chef Infra Server.

A Faire - Déconnectez-vous et reconnectez-vous au Chef Supermarket pour prendre en charge votre clef privée.

Connectez-vous ensuite à votre VM ChefServer_10.0.2.110_SSH ou ChefServer_10.0.3.110_SSH selon votre numéro de stagiaire.

Copiez le fichier .pem vers le répertoire /root/chef-repo/.chef dans la VM workstation.

Stagiaires 11, 12, 14, 15, 16 et 17 :

root@workstation:~/chef-repo/cookbooks# cd ../.chef

root@workstation:~/chef-repo/.chef# scp trainee@10.0.2.110:/home/trainee/Downloads/teamittraining.pem .
trainee@10.0.2.110's password: 
teamittraining.pem                                  100% 1674     3.1MB/s   00:00 

Stagiaires 18, 19, 20, 21, 22 et 23 :

root@workstation:~/chef-repo/cookbooks# cd ../.chef

root@workstation:~/chef-repo/.chef# scp trainee@10.0.3.110:/home/trainee/Downloads/teamittraining.pem .
trainee@10.0.3.110's password: 
teamittraining.pem                                  100% 1674     3.1MB/s   00:00 

2.3 - Publier d'un Cookbook

Naviguez maintenant au répertoire /root/chef-repo/cookbooks et générez un Cookbook en spécifiant votre nom d'utilisateur Chef à la place de la chaîne teamittraining :

root@workstation:~/chef-repo/.chef# cd ../cookbooks/

root@workstation:~/chef-repo/cookbooks# chef generate cookbook test-teamittraining -C teamittraining
Hyphens are discouraged in cookbook names as they may cause problems with custom resources. See https://docs.chef.io/ctl_chef.html#chef-generate-cookbook for more information.
Generating cookbook test-teamittraining
- Ensuring correct cookbook content
- Committing cookbook files to git

Your cookbook is ready. Type `cd test-teamittraining` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list of local testing commands.

Why not start by writing an InSpec test? Tests for the default recipe are stored at:

test/integration/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb

Important - Notez que chaque Cookbook dans un Supermarket doit posséder un nom unique.

La commande knife supermarket utilise plusieurs sous-commandes pour interagir avec le Chef Supermarket :

  • download,
    • télécharge un Cookbook du Chef Supermarket,
  • install,
    • installe un Cookbook à partir du Chef Supermarket,
  • list,
    • liste les Cookbooks disponibles dans le Chef Supermarket,
  • search,
    • recherche un Cookbook spécifique dans le Chef Supermarket,
  • share,
    • publie un Cookbook vers le Chef Supermarket,
  • show,
    • montre les informations concernant un Cookbook dans le Chef Supermarket,
  • unshare,
    • annule la publication du Cookbook dans le Chef Supermarket.

Il est possible d'obtenir de l'aide concernant les sous-commandes telle knife supermarket share :

root@workstation:~/chef-repo/cookbooks# knife supermarket share --help
knife supermarket share COOKBOOK [CATEGORY] (options)
    -s, --server-url URL             Chef Infra Server URL.
        --chef-zero-host HOST        Host to start Chef Infra Zero on.
        --chef-zero-port PORT        Port (or port range) to start Chef Infra Zero on. Port ranges like 1000,1010 or 8889-9999 will try all given ports until one works.
    -k, --key KEY                    Chef Infra Server API client key.
        --[no-]color                 Use colored output, defaults to enabled.
    -c, --config CONFIG              The configuration file to use.
        --config-option OPTION=VALUE Override a single configuration option.
    -o, --cookbook-path PATH:PATH    A colon-separated path to look for cookbooks in.
        --defaults                   Accept default values for all questions.
    -d, --disable-editing            Do not open EDITOR, just accept the data as is.
    -n, --dry-run                    Don't take action, only print what files will be uploaded to Supermarket.
    -e, --editor EDITOR              Set the editor to use for interactive commands.
    -E, --environment ENVIRONMENT    Set the Chef Infra Client environment (except for in searches, where this will be flagrantly ignored).
        --[no-]fips                  Enable FIPS mode.
    -F, --format FORMAT              Which format to use for output. (valid options: 'summary', 'text', 'json', 'yaml', or 'pp')
        --[no-]listen                Whether a local mode (-z) server binds to a port.
    -z, --local-mode                 Point knife commands at local repository instead of Chef Infra Server.
    -u, --user USER                  Chef Infra Server API client username.
        --print-after                Show the data after a destructive operation.
        --profile PROFILE            The credentials profile to select.
    -m SUPERMARKET_SITE,             The URL of the Supermarket site.
        --supermarket-site
    -V, --verbose                    More verbose output. Use twice for max verbosity.
    -v, --version                    Show Chef Infra Client version.
    -y, --yes                        Say yes to all prompts for confirmation.
    -h, --help                       Show this help message.

Utilisez donc la commande knife supermarket share pour publier le Cookbook qui vient d'être créé en spécifiant votre nom d'utilisateur Chef à la place de la chaîne teamittraining :

root@workstation:~/chef-repo/cookbooks# knife supermarket share test-teamittraining --user teamittraining --key ~/chef-repo/.chef/teamittraining.pem
Generating metadata for test-teamittraining from /tmp/chef-test-teamittraining-build20231031-4103-1t9jfg0/test-teamittraining/metadata.rb
Making tarball test-teamittraining.tgz
Upload complete

Connectez-vous ensuite à votre VM ChefServer_10.0.2.110_VNC ou ChefServer_10.0.3.110_VNC selon votre numéro de stagiaire.

Cliquez sur l'onglet Dashboard de https://supermarket.chef.io et rafraîchissez la page de votre navigateur :

Important - Notez la présence de votre Cookbook dans la section Your Cookbooks de la page.

Connectez-vous ensuite à votre VM ChefServer_10.0.2.110_SSH ou ChefServer_10.0.3.110_SSH selon votre numéro de stagiaire.

Affichez les informations concernant le Cookbook publié :

root@workstation:~/chef-repo/cookbooks# knife supermarket show test-teamittraining
average_rating:  
category:        Other
created_at:      2023-10-31T14:12:39.472Z
deprecated:      false
description:     Installs/Configures test-teamittraining
external_url:    
issues_url:      
latest_version:  https://supermarket.chef.io/api/v1/cookbooks/test-teamittraining/versions/0.1.0
maintainer:      teamittraining
metrics:
  collaborators: 0
  downloads:
    total:    0
    versions:
      0.1.0: 0
  followers:     0
name:            test-teamittraining
source_url:      
up_for_adoption: 
updated_at:      2023-10-31T14:12:39.458Z
versions:        https://supermarket.chef.io/api/v1/cookbooks/test-teamittraining/versions/0.1.0

LAB #3 - Créer et Configurer un Conteneur Docker

3.1 - Préparation

Utilisez la commande knife supermarket download pour télécharger la version 8.0.0 du Cookbook docker :

root@workstation:~/chef-repo/cookbooks# knife supermarket download docker 8.0.0
Downloading docker from Supermarket at version 8.0.0 to /root/chef-repo/cookbooks/docker-8.0.0.tar.gz
Cookbook saved: /root/chef-repo/cookbooks/docker-8.0.0.tar.gz

Désarchivez le Cookbook :

root@workstation:~/chef-repo/cookbooks# ls
apache2  chefignore  docker-8.0.0.tar.gz  starter  test-teamittraining

root@workstation:~/chef-repo/cookbooks# tar xvf docker-8.0.0.tar.gz 
...

root@workstation:~/chef-repo/cookbooks# ls
apache2  chefignore  docker  docker-8.0.0.tar.gz  starter  test-teamittraining

Placez-vous dans le Cookbook docker et consultez son contenu :

root@workstation:~/chef-repo/cookbooks# cd docker

root@workstation:~/chef-repo/cookbooks/docker# ls
CHANGELOG.md  kitchen.dokken.yml  LICENSE        metadata.rb  templates
chefignore    libraries           metadata.json  README.md

Créez le répertoire recipes :

root@workstation:~/chef-repo/cookbooks/docker# mkdir recipes

Créez ensuite le fichier recipes/default.rb suivant :

root@workstation:~/chef-repo/cookbooks/docker# vi recipes/default.rb

root@workstation:~/chef-repo/cookbooks/docker# cat recipes/default.rb
#
# Cookbook:: docker
# Recipe:: default
#
docker_service 'default' do
        action [:create, :start]
end

#Pull latest nginx image
docker_image 'nginx' do
        tag 'latest'
        action :pull
end

#Run container mapping container port 80 to host port 80
docker_container 'chef_nginx' do
        repo 'nginx'
        tag 'latest'
        port '80:80'
end

3.2 - Installation

Le Cookbook est maintenant terminé. Il convient donc de le téléverser vers le Chef Infra Server en utilisant la commande knife cookbook upload et en spécifiant le nom du Cookbook docker :

root@workstation:~/chef-repo/cookbooks/docker# knife cookbook upload docker
Uploading docker         [8.0.0]
Uploaded 1 cookbook.

Utilisez la commande knife bootstrap pour référencer le client docker.ittraining.loc avec le Chef Infra Server et exécuter le recipe.

Stagiaires 11, 12, 14, 15, 16 et 17 :

<code>
root@workstation:~/chef-repo/cookbooks# knife bootstrap 10.0.2.114 --ssh-user root --ssh-password 'fenestros' --node-name docker --run-list 'recipe[docker]'
--ssh-user: This flag is deprecated. Use -U/--connection-user instead.
--ssh-password: This flag is deprecated. Use -P/--connection-password instead.
Connecting to 10.0.2.114
The authenticity of host '10.0.2.114 ()' can't be established.
fingerprint is SHA256:s6fmqsOVfoId4uzkHMfeFs67Qkr9GDvO/iom+YUwI3k.

Are you sure you want to continue connecting
? (Y/N)
...
 [10.0.2.114] Chef Infra Client finished, 28/53 resources updated in 01 minutes 16 seconds
 [10.0.2.114] [2023-11-01T15:31:08+01:00] WARN: This release of Chef Infra Client became end of life (EOL) on May 1st 2021. Please update to a supported release to receive new features, bug fixes, and security updates.

</code>

Stagiaires 18, 19, 20, 21, 22 et 23 :

root@workstation:~/chef-repo/cookbooks# knife bootstrap 10.0.3.114 --ssh-user root --ssh-password 'fenestros' --node-name docker --run-list 'recipe[docker]'
--ssh-user: This flag is deprecated. Use -U/--connection-user instead.
--ssh-password: This flag is deprecated. Use -P/--connection-password instead.
Connecting to 10.0.3.114
The authenticity of host '10.0.3.114 ()' can't be established.
fingerprint is SHA256:s6fmqsOVfoId4uzkHMfeFs67Qkr9GDvO/iom+YUwI3k.

Are you sure you want to continue connecting
? (Y/N)
...
 [10.0.3.114] Chef Infra Client finished, 28/53 resources updated in 01 minutes 16 seconds
 [10.0.3.114] [2023-11-01T15:31:08+01:00] WARN: This release of Chef Infra Client became end of life (EOL) on May 1st 2021. Please update to a supported release to receive new features, bug fixes, and security updates.

Connectez-vous à votre VM docker à partir de votre VM workstation.

Stagiaires 11, 12, 14, 15, 16 et 17 :

root@workstation:~/chef-repo/cookbooks/docker# ssh -l trainee 10.0.2.114
The authenticity of host '10.0.2.114 (10.0.2.114)' can't be established.
ECDSA key fingerprint is SHA256:RRU4Tim5H5klLxXUa8hyodLq5DSGrKuZ07jziXLVmFw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.2.114' (ECDSA) to the list of known hosts.
trainee@10.0.2.114's password: trainee
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-150-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Expanded Security Maintenance for Infrastructure is not enabled.

0 updates can be applied immediately.

123 additional security updates can be applied with ESM Infra.
Learn more about enabling ESM Infra service for Ubuntu 18.04 at
https://ubuntu.com/18-04

New release '20.04.6 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2023.
Last login: Wed Nov  1 14:25:39 2023 from 10.0.2.1
trainee@docker:~$

Stagiaires 18, 19, 20, 21, 22 et 23 :

root@workstation:~/chef-repo/cookbooks/docker# ssh -l trainee 10.0.3.114
The authenticity of host '10.0.3.114 (10.0.3.114)' can't be established.
ECDSA key fingerprint is SHA256:RRU4Tim5H5klLxXUa8hyodLq5DSGrKuZ07jziXLVmFw.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.3.114' (ECDSA) to the list of known hosts.
trainee@10.0.3.114's password: trainee
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-150-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

Expanded Security Maintenance for Infrastructure is not enabled.

0 updates can be applied immediately.

123 additional security updates can be applied with ESM Infra.
Learn more about enabling ESM Infra service for Ubuntu 18.04 at
https://ubuntu.com/18-04

New release '20.04.6 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Your Hardware Enablement Stack (HWE) is supported until April 2023.
Last login: Wed Nov  1 14:25:39 2023 from 10.0.3.1
trainee@docker:~$

Vérifiez la présence du conteneur chef_nginx :

trainee@docker:~$ sudo su -
[sudo] password for trainee: trainee
root@docker:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                NAMES
ab39bd7f4a58   nginx:latest   "/docker-entrypoint.…"   12 minutes ago   Up 12 minutes   0.0.0.0:80->80/tcp   chef_nginx

Quittez ensuite la VM docker.

Stagiaires 11, 12, 14, 15, 16 et 17 :

trainee@docker:~$ exit
logout
Connection to 10.0.2.114 closed.

Stagiaires 18, 19, 20, 21, 22 et 23 :

trainee@docker:~$ exit
logout
Connection to 10.0.3.114 closed.

Connectez-vous ensuite à votre VM ChefServer_10.0.2.110_VNC ou ChefServer_10.0.3.110_VNC selon votre numéro de stagiaire.

Ouvrez un autre onglet dans le navigateur et naviguez à l'adresse 10.0.2.114 ou 10.0.3.114 selon le cas :

Important - Constatez que vous pouvez visualiser la page d'accueil de Nginx.


Copyright © 2023 Hugh Norris.

Menu