2
votes

I have set up Terraform Enterprise on an Ubuntu Azure VM and have integrated it with Service Now and Azure Dev Ops. Whenever there is a new ticket created in Service Now using specific catalog item, terraform workspace gets provisioned and starts a run using passed variable values. The terraform code runs at Ubuntu18.04 Azure VM. I have a requirement where I need to run Az commands using "Local-Exec" provisioner to install a custom script extension on a VM where it is already installed and need to run a powershell on that VM. I am using below code.

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"]
}

I have installed PowerShell Core on my Ubuntu machine and it resides here - /usr/bin/pwsh on my machine. Whenever I am triggering the run, it is failing with below error message.

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:

Does anybody faced the same issue or can anybody help me on this?

4

4 Answers

2
votes

Thanks Nancy and MoonHouse for your reply but terraform enterprise uses a default disposable docker container with limited softwares to execute the HCL code or ocal-exec. We need to create a custom worker image as alternative worker image where you can install your executables and can point that custom image as default container to execute your code.

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

Alternative Terraform worker image TFE runs terraform plan and terraform apply operations in a disposable Docker containers. There are cases where runs may make frequent use of additional tools that are not available in the default Docker image. To allow use of these tools for any plan or apply, users can build their own image and configure TFE to use that instead. In order for this to happen the name of the alternative docker image must be set in the config by using the Custom image tag field as shown below:

Terraform Enterprise docker image

»Requirements The base image must be ubuntu:xenial. The image must exist on the Terraform Enterprise host. It can be added by running docker pull from a local registry or any other similar method. All necessary PEM-encoded CA certificates must be placed within the /usr/local/share/ca-certificates directory. Each file added to this directory must end with the .crt extension. The CA certificates configured in the CA Bundle settings will not be automatically added to this image at runtime. Terraform must not be installed on the image. Terraform Enterprise will take care of that at runtime.

0
votes

For the error message, you could double-check:

  • If you have installed the PowerShell Core successfully
  • If you have enough permission to access that pwsh file or directory.
  • If the installed directory is added to the environment variable $PATH.

You could run echo $PATH to check the current PATH configuration and run export PATH=$PATH:/xxx/xxx to add a directory into the $PATH.

For more information, you can follow these documents. It works on my side.

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

Last but not least, If you want to run az cli, you need to install Azure CLI with apt first.

0
votes

It seems that you run az cli on Ubuntu not az powershell module. Be aware that az cli and az powershell are different from each other. If you want to run az cli, just install az cli with curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash .

Attention: The local-exec provisioner invokes a local executable after a resource is created. This invokes a process on the machine running Terraform, not on the resource. https://www.terraform.io/docs/provisioners/local-exec.html

0
votes

Please use the following set of commands to create a new Docker Image and update your Terraform worker to use this image instead.

Step 1: Login to the Terraform worker VM. Create a new folder and create new file "Dockerfile" in it using touch Dockerfile command

Step 2: Update the following script in Dockerfile created:


#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 ; \
          }"

======================================================================== Step 3: Build the docker image

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

Step 4: Test the container interactively

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

On the prompt type : pwsh to start PowerShell

Step 5: Push the container to repository

docker push custom-image-name:v1

Step 6: Pull the image on Terraform worker

docker pull custom-image-name:v1