15

My packer build is failing with the following message:

sudo: sorry, you must have a tty to run sudo.

My host is Windows 8 with vagrant and virtualbox, my guest is centos7. On researching it is my understanding that not requiring tty for sudo is the reason for the message. But I have the following in ks.cfg:

sed -i 's/^.*requiretty/#Defaults requiretty/' /etc/sudoers

Could the issue be that there's something I need to set on the windows vagrant ssh side so that a psuedo-tty is created?

This is my first go at packer.

I am using a packer build that I downloaded.

packer.json below:

{
  "variables": {
    "version": "{{env `VERSION`}}"
  },
  "provisioners": [
    {
      "type": "shell",
      "execute_command": "sudo {{.Vars}} sh {{.Path}}",
      "scripts": [
        "scripts/vagrant.sh",
        "scripts/vmtools.sh",
        "scripts/cleanup.sh",
        "scripts/zerodisk.sh"
      ]
    }
  ],
  "post-processors": [
    {
      "type": "vagrant",
      "output": "INSANEWORKS-CentOS-7.0-x86_64-{{user `version`}}-{{.Provider}}.box"
    }
  ],
  "builders": [
    {
      "type": "virtualbox-iso",
      "iso_url": "http://ftp.iij.ad.jp/pub/linux/centos/7/isos/x86_64/CentOS-7-x86_64-NetInstall-1503.iso",
      "iso_checksum": "498bb78789ddc7973fe14358822eb1b48521bbaca91c17bd132c7f8c903d79b3",
      "iso_checksum_type": "sha256",
      "ssh_username": "vagrant",
      "ssh_password": "vagrant",
      "ssh_wait_timeout": "45m",
      "ssh_disable_agent": "true",
      "boot_command": [
        "<tab> text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg<enter><wait>"
      ],
      "disk_size": "40000",
      "hard_drive_interface": "sata",
      "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso",
      "guest_additions_sha256": "7b61f523db7ba75aebc4c7bb0cae2da92674fa72299e4a006c5c67517f7d786b",
      "guest_os_type": "RedHat_64",
      "headless": "true",
      "http_directory": "http",
      "shutdown_command": "sudo /sbin/halt -p",
      "vboxmanage": [
        [ "modifyvm", "{{.Name}}", "--memory", "1024" ],
        [ "modifyvm", "{{.Name}}", "--cpus", "1" ]
      ]
    }
  ]
}

Thanks in advance.

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
Harris.Atlarge
  • 171
  • 1
  • 1
  • 5

2 Answers2

28

You have to enable a PTY in your ssh connection. Add in your builders section following configuration item:

"ssh_pty" : "true"

See also https://packer.io/docs/templates/communicator.html#ssh_pty

Your "execute_command" in provisioner section should be "execute_command" : "echo 'vagrant' | {{ .Vars }} sudo -E -S sh '{{ .Path }}'"

Sandra Parsick
  • 704
  • 7
  • 14
  • Thx! I will attempt and report if this solves the issue – Harris.Atlarge Aug 05 '15 at 14:16
  • 2
    This is the correct answer. I wish I had seen this yesterday because it would have saved me a lot of time. Just to clarify what you need to do a little, you want something like this `"builders" : [ { "type" : "virtualbox-iso", "communicator": "ssh", "ssh_pty": "true",` – Dan Finucane Aug 11 '15 at 15:24
  • 1
    In previous version `ssh_pty=true ` was the default. Since version 0.8.0 it has changed. See https://github.com/mitchellh/packer/issues/1804 – Sandra Parsick Aug 11 '15 at 15:46
  • This was the solution. I'm really surprised that it's not made clearer in the various repos that make use of packer. Thanks so much. – LoganEtherton May 23 '18 at 19:29
5

For a similar error message - 'sudo: no tty present and no askpass program specified' - I found the solution in this article:

http://blog.endpoint.com/2014/03/provisioning-development-environment_14.html

In addition to adding "ssh_pty" : "true" in the builder section, add the following provisioner:

{
  "type": "shell",
  "execute_command": "echo '{{user `ssh_pass`}}' | {{ .Vars }} sudo -E -S sh '{{ .Path }}'",
  "inline": [
    "echo '%sudo    ALL=(ALL)  NOPASSWD:ALL' >> /etc/sudoers"
  ]
}

Stack:

  • Host - Mac
  • Packer builder type - virtualbox-iso
  • (using vagrant)
Yair Segal
  • 108
  • 1
  • 7