Shell Provisionner and ubuntu

Hi,

Ubuntu 20.04
User without sudo password, connected by ssh keys and sudoer NOPASSWD:ALL

jenkins.pkr.hcl

packer {
  required_plugins {
    docker = {
      version = ">= 0.0.7"
      source = "github.com/hashicorp/docker"
    }
  }
}

source "docker" "jenkins" {
    image = "jenkins/jenkins:2.319.1-lts-jdk11"
    commit = true
    pull   = true
}

build {
  name    = "jenkins"
  sources = [
    "source.docker.jenkins",
  ]

  provisioner "file" {
    source = "plugins.txt"
    destination = "/usr/share/jenkins/ref/plugins.txt"
  }

  provisioner "shell" {
      script = "./configure.sh"
      execute_command = "sudo /bin/bash -c '{{ .Vars }} {{ .Path }}'"
  }
}

configure.sh:

#!/bin/bash -e

curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add -
apt-add-repository 'deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main'
curl -fsSL https://get.docker.com -o get-docker.sh
DRY_RUN=1 sh ./get-docker.sh
rm get-docker.sh
curl -fsSL https://deb.nodesource.com/setup_16.x -o nodejs.sh
DRY_RUN=1 sh ./nodejs.sh
rm nodejs.sh
apt-get install packer python3-pip mariadb-client
pip3 install --no-cache-dir ansible PyMySQL --user jenkins
ansible-galaxy collection install community.general
chown jenkins:jenkins /usr/share/jenkins/ref/plugins.txt
jenkins-plugin-cli -f /usr/share/jenkins/ref/plugins.txt
usermod -aG docker jenkins

Logs:

==> jenkins.docker.jenkins: Provisioning with shell script: ./configure.sh
2022/01/08 11:15:44 packer-provisioner-shell plugin: Opening ./configure.sh for reading
2022/01/08 11:15:44 packer-provisioner-shell plugin: [INFO] 689 bytes written for 'uploadData'
2022/01/08 11:15:44 [INFO] 689 bytes written for 'uploadData'
2022/01/08 11:15:44 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:15:44 Copying to /tmp/script_9618.sh on container b73fcba188d16779f04d081fb7709ef737e2d2720d9e68a2a642863a993c7551.
2022/01/08 11:15:44 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:15:44 Copied 689 bytes for /tmp/script_9618.sh
2022/01/08 11:15:44 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:15:44 Executing docker exec -i b73fcba188d16779f04d081fb7709ef737e2d2720d9e68a2a642863a993c7551 /bin/sh -c (chmod 0755 /tmp/script_9618.sh):
2022/01/08 11:15:44 [INFO] RPC client: Communicator ended with: 1
2022/01/08 11:15:44 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:15:44 [INFO] RPC endpoint: Communicator ended with: 1
2022/01/08 11:15:44 [INFO] RPC endpoint: Communicator ended with: 1
2022/01/08 11:15:44 packer-provisioner-shell plugin: [INFO] RPC client: Communicator ended with: 1
2022/01/08 11:15:44 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:15:44 Executing docker exec -i b73fcba188d16779f04d081fb7709ef737e2d2720d9e68a2a642863a993c7551 /bin/sh -c (sudo /bin/bash -c 'PACKER_BUILDER_TYPE='docker' PACKER_BUILD_NAME='jenkins'  /tmp/script_9618.sh'):
==> jenkins.docker.jenkins: /bin/sh: 1: sudo: not found
2022/01/08 11:15:45 [INFO] 0 bytes written for 'stdout'
2022/01/08 11:15:45 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:15:45 [INFO] RPC endpoint: Communicator ended with: 127
2022/01/08 11:15:45 [INFO] 28 bytes written for 'stderr'
2022/01/08 11:15:45 [INFO] RPC client: Communicator ended with: 127
2022/01/08 11:15:45 [INFO] RPC endpoint: Communicator ended with: 127
2022/01/08 11:15:45 packer-provisioner-shell plugin: [INFO] 0 bytes written for 'stdout'
2022/01/08 11:15:45 packer-provisioner-shell plugin: [INFO] 28 bytes written for 'stderr'
2022/01/08 11:15:45 packer-provisioner-shell plugin: [INFO] RPC client: Communicator ended with: 127
2022/01/08 11:15:45 [INFO] (telemetry) ending shell

I tried with:

      execute_command = "{{ .Vars }} /bin/bash {{ .Path }}"

I not working anymore with this logs:

==> jenkins.docker.jenkins: Provisioning with shell script: ./configure.sh
2022/01/08 11:22:57 packer-provisioner-shell plugin: Opening ./configure.sh for reading
2022/01/08 11:22:58 packer-provisioner-shell plugin: [INFO] 705 bytes written for 'uploadData'
2022/01/08 11:22:58 [INFO] 705 bytes written for 'uploadData'
2022/01/08 11:22:58 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:22:58 Copying to /tmp/script_3050.sh on container a688bb1181f6b62e3ac5a0d9ace5da3989922192f611b40e2eb8598f4f483d44.
2022/01/08 11:22:58 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:22:58 Copied 705 bytes for /tmp/script_3050.sh
2022/01/08 11:22:58 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:22:58 Executing docker exec -i a688bb1181f6b62e3ac5a0d9ace5da3989922192f611b40e2eb8598f4f483d44 /bin/sh -c (chmod 0755 /tmp/script_3050.sh):
2022/01/08 11:22:58 [INFO] RPC client: Communicator ended with: 1
2022/01/08 11:22:58 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:22:58 [INFO] RPC endpoint: Communicator ended with: 1
2022/01/08 11:22:58 [INFO] RPC endpoint: Communicator ended with: 1
2022/01/08 11:22:58 packer-provisioner-shell plugin: [INFO] RPC client: Communicator ended with: 1
2022/01/08 11:22:58 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:22:58 Executing docker exec -i a688bb1181f6b62e3ac5a0d9ace5da3989922192f611b40e2eb8598f4f483d44 /bin/sh -c (PACKER_BUILDER_TYPE='docker' PACKER_BUILD_NAME='jenkins'  /bin/bash /tmp/script_3050.sh):
==> jenkins.docker.jenkins: /bin/bash: /tmp/script_3050.sh: Permission denied
2022/01/08 11:22:58 [INFO] 0 bytes written for 'stdout'
2022/01/08 11:22:58 packer-plugin-docker_v1.0.3_x5.0_linux_amd64 plugin: 2022/01/08 11:22:58 [INFO] RPC endpoint: Communicator ended with: 126
2022/01/08 11:22:58 [INFO] 50 bytes written for 'stderr'
2022/01/08 11:22:58 [INFO] RPC client: Communicator ended with: 126
2022/01/08 11:22:58 [INFO] RPC endpoint: Communicator ended with: 126
2022/01/08 11:22:58 packer-provisioner-shell plugin: [INFO] 0 bytes written for 'stdout'
2022/01/08 11:22:58 packer-provisioner-shell plugin: [INFO] 50 bytes written for 'stderr'
2022/01/08 11:22:58 packer-provisioner-shell plugin: [INFO] RPC client: Communicator ended with: 126
2022/01/08 11:22:58 [INFO] (telemetry) ending shell

I don’t understant. I saw somes thread about this, In tried many suggestions about ubuntu, but. Is it sudo from host or sudo into docker? Into docker, I think is root.

Thanks, :slightly_smiling_face:

The sudo is on the remote shell. Everything, excluding shell-local is remote.

Your execute_command is incorrect, if you like to override the shell then you can include the inline_shebag line, otherwise it defaults to the login shell.

  provisioner "shell" {
    execute_command = "sudo -S env {{ .Vars }} {{ .Path }}"
    inline_shebang  = "/bin/bash -ex"
    ....
 }

Hi,
Thanks!

That is used with your advice:

  provisioner "shell" {
      script = "./configure.sh"
      execute_command = "{{ .Vars }} {{ .Path }}"
      inline_shebang  = "/bin/bash -ex"
  }

I think now it’s another problem with jenkins, the image as jenkins user without sudo and without permissions. And I don’t know how to use root account without password.

If I found, I’ll post it.

Thanks! :hugs: