首页 > 解决方案 > Packer 的 ansible-local 不会将 packer 变量作为额外的变量获取

问题描述

我有一个在 azure 上创建图像的打包程序代码。我正在使用 ansible-local 配置器来安装所需的工具。我执行以下命令

packer build -force -var azure_subscription_id=**** -var azure_tennant_id=**** -var azure_application_id=**** -var azure_application_password=**** -var nexus_user=**** -var nexus_password=**** build_packer_azure_image.json

但是,ansible 本地配置程序执行以下 comaand 作为打包程序配置程序的一部分。我需要两个变量 nexus_username 和 nexus_password 来继承上面打包器命令的值。

cd /tmp/packer-provisioner-ansible-local/5b04201f-b988-90e6-b769-24822ffc5333 && ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook /tmp/packer-provisioner-ansible-local/5b04201f-b988-90e6-b769- 24822ffc5333/provision.yml --extra-vars "packer_build_name=azure-arm packer_builder_type=azure-arm packer_http_addr=" --extra-vars 'nexus_username= nexus_password=' -c local -i /tmp/packer-provisioner-ansible-local /5b04201f-b988-90e6-b769-24822ffc5333/packer-provisioner-ansible-local158977121

可以看出,变量没有得到填充。我将示例代码片段放置在打包程序文件中以供参考。

Packer 变量配置:

{
"variables": {
    "azure_subscription_id": "{{env `azure_subscription_id`}}",
    "azure_tennant_id": "{{env `azure_tennant_id`}}",
    "azure_application_id": "{{env `azure_application_id`}}",
    "azure_application_password": "{{env `azure_application_password`}}",
    "ansible_nexus_user": "{{env `nexus_user`}}",
    "ansible_nexus_password": "{{env `nexus_password`}}"
}

Packer 供应商配置:

"provisioners": [
    {
    "type": "shell",
    "scripts": [
         "../scripts/install_ansible.sh"
      ]
    },
    {
    "type": "ansible-local",
    "playbook_dir": "../workstation-playbook",
    "playbook_file": "../workstation-playbook/provision.yml",
    "extra_arguments": [ "--extra-vars 'nexus_username={{user `ansible_nexus_user`}} nexus_password={{user `ansible_nexus_password`}}'" ]
    },
    {
     "execute_command": "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'",
     "inline": [
       "/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"
     ],
     "inline_shebang": "/bin/sh -x",
     "type": "shell"
   }]
}

请帮我解决问题。

标签: ansiblepacker

解决方案


问题已解决。变量的位置需要在变量部分互换。我也尝试保持左侧和右侧的变量名称相同。有效


推荐阅读