github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/website/source/intro/getting-started/provision.html.markdown (about)

     1  ---
     2  layout: "intro"
     3  page_title: "Provision"
     4  prev_url: "/intro/getting-started/build-image.html"
     5  next_url: "/intro/getting-started/parallel-builds.html"
     6  next_title: "Parallel Builds"
     7  description: |-
     8    In the previous page of this guide, you created your first image with Packer. The image you just built, however, was basically just a repackaging of a previously existing base AMI. The real utility of Packer comes from being able to install and configure software into the images as well. This stage is also known as the _provision_ step. Packer fully supports automated provisioning in order to install software onto the machines prior to turning them into images.
     9  ---
    10  
    11  # Provision
    12  
    13  In the previous page of this guide, you created your first image with
    14  Packer. The image you just built, however, was basically just a repackaging
    15  of a previously existing base AMI. The real utility of Packer comes from
    16  being able to install and configure software into the images as well.
    17  This stage is also known as the _provision_ step. Packer fully supports
    18  automated provisioning in order to install software onto the machines prior
    19  to turning them into images.
    20  
    21  In this section, we're going to complete our image by installing
    22  Redis on it. This way, the image we end up building actually contains
    23  Redis pre-installed. Although Redis is a small, simple example, this should
    24  give you an idea of what it may be like to install many more packages into
    25  the image.
    26  
    27  Historically, pre-baked images have been frowned upon because changing
    28  them has been so tedious and slow. Because Packer is completely automated,
    29  including provisioning, images can be changed quickly and integrated with
    30  modern configuration management tools such as Chef or Puppet.
    31  
    32  ## Configuring Provisioners
    33  
    34  Provisioners are configured as part of the template. We'll use the built-in
    35  shell provisioner that comes with Packer to install Redis. Modify the
    36  `example.json` template we made previously and add the following. We'll
    37  explain the various parts of the new configuration following the code
    38  block below.
    39  
    40  ```javascript
    41  {
    42    "variables": ["..."],
    43    "builders": ["..."],
    44  
    45    "provisioners": [{
    46      "type": "shell",
    47      "inline": [
    48        "sleep 30",
    49        "sudo apt-get update",
    50        "sudo apt-get install -y redis-server"
    51      ]
    52    }]
    53  }
    54  ```
    55  
    56  -> **Note:** The `sleep 30` in the example above is
    57  very important. Because Packer is able to detect and SSH into the instance
    58  as soon as SSH is available, Ubuntu actually doesn't get proper amounts
    59  of time to initialize. The sleep makes sure that the OS properly initializes.
    60  
    61  Hopefully it is obvious, but the `builders` section shouldn't actually
    62  contain "...", it should be the contents setup in the previous page
    63  of the getting started guide. Also note the comma after the `"builders": [...]`
    64  section, which was not present in the previous lesson.
    65  
    66  To configure the provisioners, we add a new section `provisioners` to the
    67  template, alongside the `builders` configuration. The provisioners section
    68  is an array of provisioners to run. If multiple provisioners are specified, they
    69  are run in the order given.
    70  
    71  By default, each provisioner is run for every builder defined. So if we had
    72  two builders defined in our template, such as both Amazon and DigitalOcean, then
    73  the shell script would run as part of both builds. There are ways to restrict
    74  provisioners to certain builds, but it is outside the scope of this getting
    75  started guide. It is covered in more detail in the complete
    76  [documentation](/docs).
    77  
    78  The one provisioner we defined has a type of `shell`. This provisioner
    79  ships with Packer and runs shell scripts on the running machine. In our
    80  case, we specify two inline commands to run in order to install Redis.
    81  
    82  ## Build
    83  
    84  With the provisioner configured, give it a pass once again through
    85  `packer validate` to verify everything is okay, then build it using
    86  `packer build example.json`. The output should look similar to when you
    87  built your first image, except this time there will be a new step where
    88  the provisioning is run.
    89  
    90  The output from the provisioner is too verbose to include in this
    91  guide, since it contains all the output from the shell scripts. But you
    92  should see Redis successfully install. After that, Packer once again
    93  turns the machine into an AMI.
    94  
    95  If you were to launch this AMI, Redis would be pre-installed. Cool!
    96  
    97  This is just a basic example. In a real world use case, you may be provisioning
    98  an image with the entire stack necessary to run your application. Or maybe
    99  just the web stack so that you can have an image for web servers pre-built.
   100  This saves tons of time later as you launch these images since everything
   101  is pre-installed. Additionally, since everything is pre-installed, you
   102  can test the images as they're built and know that when they go into
   103  production, they'll be functional.