在本地计算机上使用Packer的Provision Chef

问题描述

我是Chef / Packer的新手,所以如果这是一个新手问题,我深表歉意,基本上,我试图让Packer使用我的本地计算机来构建映像并执行Shell脚本。以下是我的packer-build.json

Input

使用此文件运行构建会产生输出

{
  "builders": [
    {
      "type": "file","name": "example","target": "./test_artifact.txt","content": "example content"
    }
  ],"provisioners": [
    {
      "type": "chef-solo","cookbook_paths": ["/Users/bakthak/code/nc_deployment/chef-repo/cookbooks"],"staging_directory": "/Users/bakthak","execute_command": "sh /Users/bakthak/check.sh"
    }
  ]
}

我对此有一些疑问:

  1. 打包机是否在使用本地计算机来安装Chef并构建映像?
  2. 看起来好像没有执行shell脚本==> example: Provisioning with chef-solo example: Installing Chef... example: Creating directory: /Users/bakthak example: Creating directory: /Users/bakthak/cookbooks-0 example: Creating configuration file 'solo.rb' example: Creating JSON attribute file example: Executing Chef: sh /Users/bakthak/check.sh Build 'example' finished. ,因为该脚本在目录中创建了一堆文件,打包程序构建完成后该目录不存在。

感谢您的帮助:)

解决方法

Packer将在"builders":部分中标识或创建的机器/目标上连接并运行“ provisioner”。根据{{​​1}}构建器上的文档:

file Packer构建器并不是真正的构建器,它只是根据文件创建工件。它可用于调试后处理器,而不会产生大量等待时间。

因此,使用此构建器不会在任何地方创建连接。但是,有一个名为file的构建器,可用于建立SSH会话并运行配置程序。

请考虑以下示例,其中null是我的计算机的IP地址(运行192.168.1.102的本地主机),并且具有可以通过SSH进行登录的凭据:

packer

也就是说,对于{ "builders": [ { "type": "null","ssh_host": "192.168.1.102","ssh_username": "user1","ssh_password": "changeit" } ],"provisioners": [ { "type": "chef-solo","cookbook_paths": ["/home/user1/.chef/cookbooks"],"run_list": "my_cookbook","execute_command": "sh /home/user1/myscript.sh" } ] } 预配器,最好坚持默认的execute_command

chef-solo

并从Chef资源中运行脚本:

my_cookbook / recipes / default.rb

chef-solo --no-color -c <ConfigPath> -j <.JsonPath>