github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/website/source/docs/provisioners/chef-client.html.md (about)

     1  ---
     2  description: |
     3      The Chef Client Packer provisioner installs and configures software on machines
     4      built by Packer using chef-client. Packer configures a Chef client to talk to a
     5      remote Chef Server to provision the machine.
     6  layout: docs
     7  page_title: 'Chef-Client Provisioner'
     8  ...
     9  
    10  # Chef Client Provisioner
    11  
    12  Type: `chef-client`
    13  
    14  The Chef Client Packer provisioner installs and configures software on machines
    15  built by Packer using [chef-client](https://docs.chef.io/chef_client.html).
    16  Packer configures a Chef client to talk to a remote Chef Server to provision the
    17  machine.
    18  
    19  The provisioner will even install Chef onto your machine if it isn't already
    20  installed, using the official Chef installers provided by Chef.
    21  
    22  ## Basic Example
    23  
    24  The example below is fully functional. It will install Chef onto the remote
    25  machine and run Chef client.
    26  
    27  ``` {.javascript}
    28  {
    29    "type": "chef-client",
    30    "server_url": "https://mychefserver.com/"
    31  }
    32  ```
    33  
    34  Note: to properly clean up the Chef node and client the machine on which packer
    35  is running must have knife on the path and configured globally, i.e,
    36  \~/.chef/knife.rb must be present and configured for the target chef server
    37  
    38  ## Configuration Reference
    39  
    40  The reference of available configuration options is listed below. No
    41  configuration is actually required.
    42  
    43  -   `chef_environment` (string) - The name of the chef\_environment sent to the
    44      Chef server. By default this is empty and will not use an environment.
    45  
    46  -   `config_template` (string) - Path to a template that will be used for the
    47      Chef configuration file. By default Packer only sets configuration it needs
    48      to match the settings set in the provisioner configuration. If you need to
    49      set configurations that the Packer provisioner doesn't support, then you
    50      should use a custom configuration template. See the dedicated "Chef
    51      Configuration" section below for more details.
    52  
    53  -   `encrypted_data_bag_secret_path` (string) - The path to the file containing
    54      the secret for encrypted data bags. By default, this is empty, so no
    55      secret will be available.
    56  
    57  -   `execute_command` (string) - The command used to execute Chef. This has
    58      various [configuration template
    59      variables](/docs/templates/configuration-templates.html) available. See
    60      below for more information.
    61  
    62  -   `guest_os_type` (string) - The target guest OS type, either "unix" or
    63      "windows". Setting this to "windows" will cause the provisioner to use
    64       Windows friendly paths and commands. By default, this is "unix".
    65  
    66  -   `install_command` (string) - The command used to install Chef. This has
    67      various [configuration template
    68      variables](/docs/templates/configuration-templates.html) available. See
    69      below for more information.
    70  
    71  -   `json` (object) - An arbitrary mapping of JSON that will be available as
    72      node attributes while running Chef.
    73  
    74  -   `knife_command` (string) - The command used to run Knife during node clean-up. This has
    75      various [configuration template
    76      variables](/docs/templates/configuration-templates.html) available. See
    77      below for more information.
    78  
    79  -   `node_name` (string) - The name of the node to register with the
    80      Chef Server. This is optional and by default is packer-{{uuid}}.
    81  
    82  -   `prevent_sudo` (boolean) - By default, the configured commands that are
    83      executed to install and run Chef are executed with `sudo`. If this is true,
    84      then the sudo will be omitted. This has no effect when guest_os_type is
    85      windows.
    86  
    87  -   `run_list` (array of strings) - The [run
    88      list](http://docs.chef.io/essentials_node_object_run_lists.html) for Chef.
    89      By default this is empty, and will use the run list sent down by the
    90      Chef Server.
    91  
    92  -   `server_url` (string) - The URL to the Chef server. This is required.
    93  
    94  -   `skip_clean_client` (boolean) - If true, Packer won't remove the client from
    95      the Chef server after it is done running. By default, this is false.
    96  
    97  -   `skip_clean_node` (boolean) - If true, Packer won't remove the node from the
    98      Chef server after it is done running. By default, this is false.
    99  
   100  -   `skip_install` (boolean) - If true, Chef will not automatically be installed
   101      on the machine using the Chef omnibus installers.
   102  
   103  -   `ssl_verify_mode` (string) - Set to "verify\_none" to skip validation of
   104      SSL certificates. If not set, this defaults to "verify\_peer" which validates
   105      all SSL certifications.
   106  
   107  -   `staging_directory` (string) - This is the directory where all the
   108      configuration of Chef by Packer will be placed. By default this is
   109      "/tmp/packer-chef-client" when guest_os_type unix and
   110      "$env:TEMP/packer-chef-client" when windows. This directory doesn't need to
   111      exist but must have proper permissions so that the user that Packer uses is
   112      able to create directories and write into this folder. By default the
   113      provisioner will create and chmod 0777 this directory.
   114  
   115  -   `client_key` (string) - Path to client key. If not set, this defaults to a
   116      file named client.pem in `staging_directory`.
   117  
   118  -   `validation_client_name` (string) - Name of the validation client. If not
   119      set, this won't be set in the configuration and the default that Chef uses
   120      will be used.
   121  
   122  -   `validation_key_path` (string) - Path to the validation key for
   123      communicating with the Chef Server. This will be uploaded to the
   124      remote machine. If this is NOT set, then it is your responsibility via other
   125      means (shell provisioner, etc.) to get a validation key to where Chef
   126      expects it.
   127  
   128  ## Chef Configuration
   129  
   130  By default, Packer uses a simple Chef configuration file in order to set the
   131  options specified for the provisioner. But Chef is a complex tool that supports
   132  many configuration options. Packer allows you to specify a custom configuration
   133  template if you'd like to set custom configurations.
   134  
   135  The default value for the configuration template is:
   136  
   137  ``` {.liquid}
   138  log_level        :info
   139  log_location     STDOUT
   140  chef_server_url  "{{.ServerUrl}}"
   141  client_key       "{{.ClientKey}}"
   142  {{if ne .EncryptedDataBagSecretPath ""}}
   143  encrypted_data_bag_secret "{{.EncryptedDataBagSecretPath}}"
   144  {{end}}
   145  {{if ne .ValidationClientName ""}}
   146  validation_client_name "{{.ValidationClientName}}"
   147  {{else}}
   148  validation_client_name "chef-validator"
   149  {{end}}
   150  {{if ne .ValidationKeyPath ""}}
   151  validation_key "{{.ValidationKeyPath}}"
   152  {{end}}
   153  node_name "{{.NodeName}}"
   154  {{if ne .ChefEnvironment ""}}
   155  environment "{{.ChefEnvironment}}"
   156  {{end}}
   157  {{if ne .SslVerifyMode ""}}
   158  ssl_verify_mode :{{.SslVerifyMode}}
   159  {{end}}
   160  ```
   161  
   162  This template is a [configuration
   163  template](/docs/templates/configuration-templates.html) and has a set of
   164  variables available to use:
   165  
   166  -   `ChefEnvironment` - The Chef environment name.
   167  -   `EncryptedDataBagSecretPath` - The path to the secret key file to decrypt
   168       encrypted data bags.
   169  -   `NodeName` - The node name set in the configuration.
   170  -   `ServerUrl` - The URL of the Chef Server set in the configuration.
   171  -   `SslVerifyMode` - Whether Chef SSL verify mode is on or off.
   172  -   `ValidationClientName` - The name of the client used for validation.
   173  -   `ValidationKeyPath` - Path to the validation key, if it is set.
   174  
   175  ## Execute Command
   176  
   177  By default, Packer uses the following command (broken across multiple lines for
   178  readability) to execute Chef:
   179  
   180  ``` {.liquid}
   181  {{if .Sudo}}sudo {{end}}chef-client \
   182    --no-color \
   183    -c {{.ConfigPath}} \
   184    -j {{.JsonPath}}
   185  ```
   186  
   187  When guest_os_type is set to "windows", Packer uses the following command to
   188  execute Chef. The full path to Chef is required because the PATH environment
   189  variable changes don't immediately propogate to running processes.
   190  
   191  ``` {.liquid}
   192  c:/opscode/chef/bin/chef-client.bat \
   193    --no-color \
   194    -c {{.ConfigPath}} \
   195    -j {{.JsonPath}}
   196  ```
   197  
   198  This command can be customized using the `execute_command` configuration. As you
   199  can see from the default value above, the value of this configuration can
   200  contain various template variables, defined below:
   201  
   202  -   `ConfigPath` - The path to the Chef configuration file.
   203  -   `JsonPath` - The path to the JSON attributes file for the node.
   204  -   `Sudo` - A boolean of whether to `sudo` the command or not, depending on the
   205      value of the `prevent_sudo` configuration.
   206  
   207  ## Install Command
   208  
   209  By default, Packer uses the following command (broken across multiple lines for
   210  readability) to install Chef. This command can be customized if you want to
   211  install Chef in another way.
   212  
   213  ``` {.text}
   214  curl -L https://www.chef.io/chef/install.sh | \
   215    {{if .Sudo}}sudo{{end}} bash
   216  ```
   217  
   218  When guest_os_type is set to "windows", Packer uses the following command to
   219  install the latest version of Chef:
   220  
   221  ``` {.text}
   222  powershell.exe -Command "(New-Object System.Net.WebClient).DownloadFile('http://chef.io/chef/install.msi', 'C:\\Windows\\Temp\\chef.msi');Start-Process 'msiexec' -ArgumentList '/qb /i C:\\Windows\\Temp\\chef.msi' -NoNewWindow -Wait"
   223  ```
   224  
   225  This command can be customized using the `install_command` configuration.
   226  
   227  ## Knife Command
   228  
   229  By default, Packer uses the following command (broken across multiple lines for
   230  readability) to execute Chef:
   231  
   232  ``` {.liquid}
   233  {{if .Sudo}}sudo {{end}}knife \
   234    {{.Args}} \
   235    {{.Flags}}
   236  ```
   237  
   238  When guest_os_type is set to "windows", Packer uses the following command to
   239  execute Chef. The full path to Chef is required because the PATH environment
   240  variable changes don't immediately propogate to running processes.
   241  
   242  ``` {.liquid}
   243  c:/opscode/chef/bin/knife.bat \
   244    {{.Args}} \
   245    {{.Flags}}
   246  ```
   247  
   248  This command can be customized using the `knife_command` configuration. As you
   249  can see from the default value above, the value of this configuration can
   250  contain various template variables, defined below:
   251  
   252  -   `Args` - The command arguments that are getting passed to the Knife command.
   253  -   `Flags` - The command flags that are getting passed to the Knife command..
   254  -   `Sudo` - A boolean of whether to `sudo` the command or not, depending on the
   255      value of the `prevent_sudo` configuration.
   256  
   257  
   258  ## Folder Permissions
   259  
   260  !> The `chef-client` provisioner will chmod the directory with your Chef keys
   261  to 777. This is to ensure that Packer can upload and make use of that directory.
   262  However, once the machine is created, you usually don't want to keep these
   263  directories with those permissions. To change the permissions on the
   264  directories, append a shell provisioner after Chef to modify them.
   265  
   266  ## Examples
   267  
   268  ### Chef Client Local Mode
   269  
   270  The following example shows how to run the `chef-cilent` provisioner in local
   271  mode, while passing a `run_list` using a variable.
   272  
   273  **Local environment variables**
   274  
   275      # Machines Chef directory
   276      export PACKER_CHEF_DIR=/var/chef-packer
   277      # Comma separated run_list
   278      export PACKER_CHEF_RUN_LIST="recipe[apt],recipe[nginx]"
   279      ...
   280  
   281  **Packer variables**
   282  
   283  Set the necessary Packer variables using environment variables or provide a [var
   284  file](/docs/templates/user-variables.html).
   285  
   286  ``` {.liquid}
   287  "variables": {
   288    "chef_dir": "{{env `PACKER_CHEF_DIR`}}",
   289    "chef_run_list": "{{env `PACKER_CHEF_RUN_LIST`}}",
   290    "chef_client_config_tpl": "{{env `PACKER_CHEF_CLIENT_CONFIG_TPL`}}",
   291    "packer_chef_bootstrap_dir": "{{env `PACKER_CHEF_BOOTSTRAP_DIR`}}" ,
   292    "packer_uid": "{{env `PACKER_UID`}}",
   293    "packer_gid": "{{env `PACKER_GID`}}"
   294  }
   295  ```
   296  
   297  **Setup the** `chef-client` **provisioner**
   298  
   299  Make sure we have the correct directories and permissions for the `chef-client`
   300  provisioner. You will need to bootstrap the Chef run by providing the necessary
   301  cookbooks using Berkshelf or some other means.
   302  
   303  ``` {.liquid}
   304  {
   305    "type": "file",
   306    "source": "{{user `packer_chef_bootstrap_dir`}}",
   307    "destination": "/tmp/bootstrap"
   308  },
   309  {
   310    "type": "shell",
   311    "inline": [
   312      "sudo mkdir -p {{user `chef_dir`}}",
   313      "sudo mkdir -p /tmp/packer-chef-client",
   314      "sudo chown {{user `packer_uid`}}.{{user `packer_gid`}} /tmp/packer-chef-client",
   315      "sudo sh /tmp/bootstrap/bootstrap.sh"
   316    ]
   317  },
   318  {
   319    "type": "chef-client",
   320    "server_url": "http://localhost:8889",
   321    "config_template": "{{user `chef_client_config_tpl`}}/client.rb.tpl",
   322    "skip_clean_node": true,
   323    "skip_clean_client": true,
   324    "run_list": "{{user `chef_run_list`}}"
   325  }
   326  ```