簡體   English   中英

如何有條件地使用打包器構建 aws ami?

[英]how to build aws ami with packer conditionally?

僅當結果(shell 命令)與“provisioner”部分中的值匹配時,我才使用打包器構建新的 ami

我正在尋找在“供應商”部分有條件語句的解決方案

"provisioners": [
  {
    "type": "shell",
    "inline": [
      res=f(20)
    ]

在這個例子中,我想定義一個條件,如果 res = 10 然后繼續(所以打包器將生成 aws ami)否則停止執行(並打印一條消息)

我將從免責聲明開始:有條件地構建並不是供應商真正打算做的事情。 理想情況下,這種邏輯應該在打包程序構建過程之外處理,也許在@MattSchuchard 建議的構建管道中處理。 構建管道工具的示例有:Jenkins、CircleCI、Drone.IO

但是:如果您確實需要將此邏輯內置到配置程序中,Packer 會終止並退出非零錯誤代碼,因此您可以執行以下操作:

"provisioners": [
    {
        "type": "shell",
        "inline": [
            "if [ $res -eq f(20) ]; then echo $res && exit 0; else echo "Incorrect result" && exit 1; fi"
        ]
    }
]

您可以通過使用valid_exit_codes選項並定義您希望從您要驗證的特定情況中獲得哪些退出代碼來進一步調整它。 參考: https://www.packer.io/docs/provisioners/shell.html#valid_exit_codes

示例 Output:

$ packer build -var-file=provisioner-test.json build.json
==> amazon-ebs: Prevalidating AMI Name: Test-37Cv9mXMGqw5zAV
    amazon-ebs: Found Image ID: ami-09693313102a30b2c
==> amazon-ebs: Creating temporary keypair: packer_5d96a8b4-ef4d-a705-a393-076457bdc3ea
==> amazon-ebs: Launching a source AWS instance...
==> amazon-ebs: Adding tags to source instance
    amazon-ebs: Adding tag: "Name": "Packer Builder"
    amazon-ebs: Instance ID: i-0ca4d944fe99255da
==> amazon-ebs: Waiting for instance (i-0ca4d944fe99255da) to become ready...
==> amazon-ebs: Using ssh communicator to connect: 10.0.24.189
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Provisioning with shell script: /var/folders/mg/wc582qjx0y759zw3hfwxwjmm0000gp/T/packer-shell814735146
    amazon-ebs: Incorrect result
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: No volumes to clean up, skipping
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' errored: Script exited with non-zero exit status: 1.Allowed exit codes are: [0]

==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: Script exited with non-zero exit status: 1.Allowed exit codes are: [0]

==> Builds finished but no artifacts were created.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM