【问题标题】:exec: "powershell": executable file not found in $PATH while running local-exec in Terraformexec:“powershell”:在 Terraform 中运行 local-exec 时在 $PATH 中找不到可执行文件
【发布时间】:2020-10-26 06:58:07
【问题描述】:

我在 Ubuntu Azure VM 上设置了 Terraform Enterprise,并将其与 Service Now 和 Azure Dev Ops 集成。每当在 Service Now 中使用特定目录项创建新票证时,就会配置 terraform 工作区并使用传递的变量值开始运行。 terraform 代码在 Ubuntu18.04 Azure VM 上运行。我有一个要求,我需要使用“Local-Exec”配置程序运行 Az 命令,以在已经安装了自定义脚本扩展的 VM 上安装它,并且需要在该 VM 上运行 powershell。我正在使用下面的代码。

provisioner "local-exec" {
  command = <<EOH
  az login --identity
  az account set --subscription=${local.subscription_id}
  az vm extension set --resource-group ${data.azurerm_resource_group.rg.name} --vm-name ${var.azure_vm_name} --name CustomScriptExtension --publisher Microsoft.Compute --extension-instance-name CustomScriptExtension --settings .//settings.json --version 1.9 --force-update
  EOH
  interpreter = ["pwsh","-command"]
}

我已经在我的 Ubuntu 机器上安装了 PowerShell Core,它位于此处 - /usr/bin/pwsh 在我的机器上。 每当我触发运行时,它都会失败并显示以下错误消息。

Error: Error running command '          az login --identity
  az account set --subscription=<My Subscription ID>
  az vm extension set --resource-group <my resource group> --vm-name <VM Name> --name CustomScriptExtension --publisher Microsoft.Compute --extension-instance-name CustomScriptExtension --settings .//settings.json --version 1.9 --force-update 
exec: "pwsh": executable file not found in $PATH. Output:

有没有人遇到过同样的问题,或者有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: terraform terraform-provider-azure


    【解决方案1】:

    感谢 Nancy 和 MoonHouse 的回复,但 terraform 企业使用默认的一次性 docker 容器和有限的软件 执行 HCL 代码或 ocal-exec。我们需要创建一个自定义工作映像作为替代工作映像,您可以在其中安装可执行文件,并将该自定义映像作为默认容器来执行您的代码。

    https://www.terraform.io/docs/enterprise/install/installer.html

    替代 Terraform 工作人员图像 TFE 在一次性 Docker 容器中运行 terraform plan 和 terraform apply 操作。在某些情况下,运行可能会频繁使用默认 Docker 映像中不可用的其他工具。要允许将这些工具用于任何计划或应用程序,用户可以构建自己的映像并配置 TFE 以使用它。为了实现这一点,必须在配置中使用 Custom image tag 字段设置备用 docker 镜像的名称,如下所示:

    Terraform Enterprise docker 镜像

    »要求 基础镜像必须是 ubuntu:xenial。 映像必须存在于 Terraform Enterprise 主机上。可以通过从本地注册表运行 docker pull 或任何其他类似方法来添加它。 所有必要的 PEM 编码 CA 证书必须放在 /usr/local/share/ca-certificates 目录中。添加到此目录的每个文件都必须以 .crt 扩展名结尾。在 CA Bundle 设置中配置的 CA 证书不会在运行时自动添加到此映像中。 不得在映像上安装 Terraform。 Terraform Enterprise 将在运行时处理这些问题。

    【讨论】:

      【解决方案2】:

      对于错误信息,您可以仔细检查:

      • 如果您已成功安装 PowerShell Core
      • 如果您有足够的权限访问该@​​987654324@ 文件或目录。
      • 如果安装目录添加到环境变量$PATH

      您可以运行 echo $PATH 来检查当前的 PATH 配置并运行 export PATH=$PATH:/xxx/xxx 将目录添加到$PATH

      有关更多信息,您可以关注这些文档。它对我有用。

      1.Install PowerShell on Linux---Ubuntu 18.04

      # Download the Microsoft repository GPG keys
      wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb   
      # Register the Microsoft repository GPG keys
      sudo dpkg -i packages-microsoft-prod.deb    
      # Update the list of products
      sudo apt-get update
      # Enable the "universe" repositories
      sudo add-apt-repository universe
      # Install PowerShell
      sudo apt-get install -y powershell
      # Start PowerShell
      pwsh
      

      2.Install Terraform on Ubuntu 18.04

      $ sudo apt-get update
      Again, we will install wget and unzip packages if they’re not already installed:
      $ sudo apt-get install wget unzip
      $ wget https://releases.hashicorp.com/terraform/0.12.28/terraform_0.12.28_linux_amd64.zip
      $ sudo unzip ./terraform_0.12.28_linux_amd64.zip -d /usr/local/bin/
      And finally, to test if our installation was successful:    
      $ terraform -v
      

      最后但同样重要的是,如果你想运行 az cli,你需要先install Azure CLI with apt

      【讨论】:

        【解决方案3】:

        您似乎在 Ubuntu 上运行 az cli 而不是 az powershell 模块。请注意,az cliaz powershell 彼此不同。如果要运行 az cli,只需使用 curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash 安装 az cli。

        注意:local-exec 配置器在创建资源后调用本地可执行文件。这会在运行 Terraform 的机器上调用一个进程,而不是在资源上。 https://www.terraform.io/docs/provisioners/local-exec.html

        【讨论】:

          【解决方案4】:

          请使用以下命令集创建新的 Docker 映像并更新您的 Terraform 工作人员以改用此映像。

          第 1 步:登录 Terraform 工作者虚拟机。创建一个新文件夹并使用 touch Dockerfile 命令在其中创建新文件“Dockerfile”

          第 2 步:在创建的 Dockerfile 中更新以下脚本:


          #This Dockerfile builds the image used for the worker containers.
          FROM ubuntu:xenial
          
          #Install software used by Terraform Enterprise.
          RUN apt-get update && apt-get install -y --no-install-recommends \
              unzip daemontools git-core ssh wget curl psmisc iproute2 openssh-client redis-tools netcat-openbsd ca-certificates
          
          
          #Docker image file that describes an Ubuntu18.04 image with PowerShell installed from Microsoft APT Repo
          ARG fromTag=18.04
          ARG imageRepo=ubuntu
          
          FROM ubuntu:18.04 AS installer-env
          ARG PS_VERSION=7.0.2-1
          ARG PS_PACKAGE_URL=https://github.com/PowerShell/PowerShell/releases/download/v7.0.2/powershell_7.0.2-1.ubuntu.18.04_amd64.deb
          
          #Define ENVs for Localization/Globalization
          ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false \
              LC_ALL=en_US.UTF-8 \
              LANG=en_US.UTF-8 \
              # set a fixed location for the Module analysis cache
              PSModuleAnalysisCachePath=/var/cache/microsoft/powershell/PSModuleAnalysisCache/ModuleAnalysisCache \
              POWERSHELL_DISTRIBUTION_CHANNEL=PSDocker-Ubuntu-18.04
          
          #Install dependencies and clean up
          RUN apt-get update \
              && apt-get install --no-install-recommends -y \
              # curl is required to grab the Linux package
                  curl \
              # less is required for help in powershell
                  less \
              # requied to setup the locale
                  locales \
              # required for SSL
                  ca-certificates \
                  gss-ntlmssp \
              # Download the Linux package and save it
              && echo ${PS_PACKAGE_URL} \
              && curl -sSL ${PS_PACKAGE_URL} -o /tmp/powershell.deb \
              && apt-get install --no-install-recommends -y /tmp/powershell.deb \
              && apt-get dist-upgrade -y \
              && apt-get clean \
              && rm -rf /var/lib/apt/lists/* \
              && locale-gen $LANG && update-locale \
              # remove powershell package
              && rm /tmp/powershell.deb \
              # intialize powershell module cache
              && pwsh \
                  -NoLogo \
                  -NoProfile \
                  -Command " \
                    \$ErrorActionPreference = 'Stop' ; \
                    \$ProgressPreference = 'SilentlyContinue' ; \
                    while(!(Test-Path -Path \$env:PSModuleAnalysisCachePath)) {  \
                      Write-Host "'Waiting for $env:PSModuleAnalysisCachePath'" ; \
                      Start-Sleep -Seconds 6 ; \
                    }"
          

          ================================================ ========================== 第三步:构建 docker 镜像

          docker build . -t 'custom-image-name'
          

          第 4 步:交互式测试容器

          docker container run -it custom-image-name:v1 /bin/bash
          

          在提示符下输入:pwsh to start PowerShell

          第 5 步:将容器推送到存储库

          docker push custom-image-name:v1
          

          第 6 步:在 Terraform worker 上拉取图像

          docker pull custom-image-name:v1
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-01-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多