{"id":1707,"date":"2024-10-23T20:59:28","date_gmt":"2024-10-23T19:59:28","guid":{"rendered":"http:\/\/ve2cuy.com\/420-3c3\/?page_id=1707"},"modified":"2024-10-23T22:03:30","modified_gmt":"2024-10-23T21:03:30","slug":"vagrant-automatisation-de-la-creation-des-vm","status":"publish","type":"page","link":"https:\/\/ve2cuy.com\/420-3c3\/?page_id=1707","title":{"rendered":"Vagrant &#8211; Automatisation de la cr\u00e9ation des VM"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1 &#8211; Installation de vagrant<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.vagrantup.com\/downloads\">Downloads | Vagrant by HashiCorp (vagrantup.com)<\/a><\/p>\n\n\n\n<p>Vagrant cr\u00e9e les machines virtuelles \u00e0 partir de &lsquo;Boxe&rsquo;.<\/p>\n\n\n\n<p>La liste des &lsquo;Boxes&rsquo; est disponible <a href=\"https:\/\/portal.cloud.hashicorp.com\/vagrant\/discover?providers=virtualbox&amp;query=\">ici<\/a>.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">NOTE IMPORTANTE<\/mark><\/strong>:  \u00c0 la date du 2024.10.23 &#8211; Vagrant n&rsquo;est pas compatible avec VirtualBox 7.1.  Voici un lien vers la version <a href=\"https:\/\/download.virtualbox.org\/virtualbox\/7.0.22\/VirtualBox-7.0.22-165102-Win.exe\">7.0.22 de virtualbox<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2 &#8211; Cr\u00e9ation d&rsquo;un playbook Vagrant &#8211; Fichier Vagrantfile<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Dans un terminal, taper la commande suivante:\n\n$ vagrant init\n\nou bien\n\n$ vagrant init ubuntu\/focal64\n\nA `Vagrantfile` has been placed in this directory. You are now\nready to `vagrant up` your first virtual environment! Please read\nthe comments in the Vagrantfile as well as documentation on\n`vagrantup.com` for more information on using Vagrant. <\/pre>\n\n\n\n<p>Ceci devrait produire le fichier Vagrantfile suivant:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># -*- mode: ruby -*-\n# vi: set ft=ruby :\n\n# All Vagrant configuration is done below. The \"2\" in Vagrant.configure\n# configures the configuration version (we support older styles for\n# backwards compatibility). Please don't change it unless you know what\n# you're doing.\nVagrant.configure(\"2\") do |config|\n  # The most common configuration options are documented and commented below.\n  # For a complete reference, please see the online documentation at\n  # https:\/\/docs.vagrantup.com.\n\n  # Every Vagrant development environment requires a box. You can search for\n  # boxes at https:\/\/vagrantcloud.com\/search.\n\n  config.vm.box = \"base\"\n\nNOTE: Remplacer la ligne pr\u00e9c\u00e9dente par\n  config.vm.box = \"ubuntu\/focal64\"\n\n  # Disable automatic box update checking. If you disable this, then\n  # boxes will only be checked for updates when the user runs\n  # `vagrant box outdated`. This is not recommended.\n  # config.vm.box_check_update = false\n\n  # Create a forwarded port mapping which allows access to a specific port\n  # within the machine from a port on the host machine. In the example below,\n  # accessing \"localhost:8080\" will access port 80 on the guest machine.\n  # NOTE: This will enable public access to the opened port\n  # config.vm.network \"forwarded_port\", guest: 80, host: 8080\n\n  # Create a forwarded port mapping which allows access to a specific port\n  # within the machine from a port on the host machine and only allow access\n  # via 127.0.0.1 to disable public access\n  # config.vm.network \"forwarded_port\", guest: 80, host: 8080, host_ip: \"127.0.0.1\"\n\n  # Create a private network, which allows host-only access to the machine\n  # using a specific IP.\n  # config.vm.network \"private_network\", ip: \"192.168.33.10\"\n\n  # Create a public network, which generally matched to bridged network.\n  # Bridged networks make the machine appear as another physical device on\n  # your network.\n  # config.vm.network \"public_network\"\n\n  # Share an additional folder to the guest VM. The first argument is\n  # the path on the host to the actual folder. The second argument is\n  # the path on the guest to mount the folder. And the optional third\n  # argument is a set of non-required options.\n  # config.vm.synced_folder \"..\/data\", \"\/vagrant_data\"\n\n  # Disable the default share of the current code directory. Doing this\n  # provides improved isolation between the vagrant box and your host\n  # by making sure your Vagrantfile isn't accessible to the vagrant box.\n  # If you use this you may want to enable additional shared subfolders as\n  # shown above.\n  # config.vm.synced_folder \".\", \"\/vagrant\", disabled: true\n\n  # Provider-specific configuration so you can fine-tune various\n  # backing providers for Vagrant. These expose provider-specific options.\n  # Example for VirtualBox:\n  #\n  # config.vm.provider \"virtualbox\" do |vb|\n  #   # Display the VirtualBox GUI when booting the machine\n  #   vb.gui = true\n  #\n  #   # Customize the amount of memory on the VM:\n  #   vb.memory = \"1024\"\n  # end\n  #\n  # View the documentation for the provider you are using for more\n  # information on available options.\n\n  # Enable provisioning with a shell script. Additional provisioners such as\n  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the\n  # documentation for more information about their specific syntax and use.\n  # config.vm.provision \"shell\", inline: &lt;&lt;-SHELL\n  #   apt-get update\n  #   apt-get install -y apache2\n  # SHELL\nend\n<\/pre>\n\n\n\n<p>Dans un r\u00e9pertoire vide, cr\u00e9er un fichier nomm\u00e9 &lsquo;Vagrantfile&rsquo; avec le contenu suivant:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Ficher: Vagrantfile\n# Auteur: Alain Boudreault\n# Date:   2024.10.23\n# ---------------------------------------------------------------\n# Description: Mise en place de trois (3) VM Ubuntu,\n#              dans le but de tester des cl\u00e9s priv\u00e9es\/publiques\n# ---------------------------------------------------------------\n# NOTE: La syntaxe utilis\u00e9e dans un fichier Vagrant est Ruby\n# ---------------------------------------------------------------\nVagrant.configure(\"2\") do |config|\n    numberSrv=3\n    # Serveurs de tests pour les cl\u00e9s RSA\n    (1..numberSrv).each do |i|\n      config.vm.define \"CFG-serveur-0#{i}\" do |knode|\n        # D\u00e9sactiver le stockage li\u00e9 avec l'h\u00f4te.\n        knode.vm.synced_folder '.', '\/vagrant', disabled: true\n        # Renseigner la 'box' \u00e0 utiliser\n        knode.vm.box = \"ubuntu\/focal64\"        \n        knode.vm.hostname = \"mon-serveur-0#{i}\"\n\n        # D\u00e9finition du type d'acc\u00e8s r\u00e9seau\n        # R\u00e9f\u00e9rence: https:\/\/www.vagrantup.com\/docs\/networking\n        knode.vm.network \"private_network\", ip: \"192.168.56.10#{i}\"\n        # config.vm.network \"public_network\"\n        # config.vm.network \"public_network\", ip: \"192.168.0.17\"\n        # config.vm.network \"public_network\", bridge: \"en1: Wi-Fi (AirPort)\"\n\n        # Utiliser un hyperviseur pour cr\u00e9er\/d\u00e9marrer les VM\n        knode.vm.provider \"virtualbox\" do |v|\n         v.name = \"VM-Serveur-0#{i}\"\n          v.memory = 1024\n          v.cpus = 1\n        end\n        # Activation du ssh hors vagrant.\n        # Note: Par d\u00e9faut, l'acc\u00e8s ssh est d\u00e9sactiv\u00e9 dans les BOX Vagrant.\n        config.vm.provision \"shell\", inline: &lt;&lt;-SHELL\n          # Activer les connexions ssh dans le fichier \/etc\/ssh\/sshd_config\n          sed -i 's\/ChallengeResponseAuthentication no\/ChallengeResponseAuthentication yes\/g' \/etc\/ssh\/sshd_config    \n          service ssh restart\n        SHELL\n      end\n    end\n  end<\/pre>\n\n\n\n<p><strong><span style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">NOTE<\/span><\/strong>:  Les fichiers Vagrantfile sont programm\u00e9s en utilisant le langage <a href=\"https:\/\/www.ruby-lang.org\/en\/\">Ruby<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3 &#8211; D\u00e9marrage des VM<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Dans le r\u00e9pertoire qui contient le fichier \"Vagrantfile\"\n\n$ vagrant up<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4 &#8211; Connexion \u00e0 une VM<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ vagrant ssh nom-du-fichier-conf-de-la-vm\n$ vagrant ssh CFG-serveur-01\n\n# NOTE: Pour obtenir la liste des fichiers conf, utiliser la commande\n  vagrant status\n\n# Ou bien\n$ ssh vagrant@192.168.56.101\n# Le mot de passe est vagrant<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5 &#8211; Shutdown des VM<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ vagrant halt<\/pre>\n\n\n\n<p>6 &#8211; Suppression des VM<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Note: -f = effacer sans confirmation\n$ vagrant destroy -f<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<p><a href=\"https:\/\/www.vagrantup.com\/docs\">R\u00e9f\u00e9rence Vagrant<\/a><\/p>\n\n\n\n<p>Document d&rsquo;introduction externe (<a href=\"https:\/\/opensource.com\/article\/19\/12\/beginner-vagrant\">en anglais<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1 &#8211; Installation de vagrant Downloads | Vagrant by HashiCorp (vagrantup.com) Vagrant cr\u00e9e les machines virtuelles \u00e0 partir de &lsquo;Boxe&rsquo;. La liste des &lsquo;Boxes&rsquo; est disponible ici. NOTE IMPORTANTE: \u00c0 la date du 2024.10.23 &#8211; Vagrant n&rsquo;est pas compatible avec VirtualBox 7.1. Voici un lien vers la version 7.0.22 de virtualbox 2 &#8211; Cr\u00e9ation d&rsquo;un [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"_links":{"self":[{"href":"https:\/\/ve2cuy.com\/420-3c3\/index.php?rest_route=\/wp\/v2\/pages\/1707"}],"collection":[{"href":"https:\/\/ve2cuy.com\/420-3c3\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/ve2cuy.com\/420-3c3\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/420-3c3\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ve2cuy.com\/420-3c3\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1707"}],"version-history":[{"count":18,"href":"https:\/\/ve2cuy.com\/420-3c3\/index.php?rest_route=\/wp\/v2\/pages\/1707\/revisions"}],"predecessor-version":[{"id":2488,"href":"https:\/\/ve2cuy.com\/420-3c3\/index.php?rest_route=\/wp\/v2\/pages\/1707\/revisions\/2488"}],"wp:attachment":[{"href":"https:\/\/ve2cuy.com\/420-3c3\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}