title: Packer and Ansible SSH/Sudo
body: <p>I'm trying to setup packer and ansible-remote to create an AMI based on my pre-existing ansible scripts.  I run into one of two issues.</p>
  
  <p>First I had a problem with SSH where I received <code>SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh</code>.  I added the <code>connection: local</code> in my ansible config and it seems to have resolved that.</p>
  
  <p>Now I am running into an issue <code>sudo: a password is required</code> from Ansible.  I'm unclear why as the user I am specifying has sudo access with NOPASSWD and I've verified this by connecting using the temp key's setup by packer.  I receive the following error and have tried passing in <code>ansible_become_user</code> and <code>ansible_become_pass</code> as vars through packer with no luck.  It seems like maybe it's trying to sudo against my local connection now but needs the password?  Any ideas how to set this up properly.</p>
  
  <p><strong>Packer:</strong></p>
  
  <pre><code>{
    "variables": {
      "aws_access_key": "",
      "aws_secret_key": ""
    },
    "builders": [{
      "type": "amazon-ebs",
      "access_key": "{{user `aws_access_key`}}",
      "secret_key": "{{user `aws_secret_key`}}",
      "region": "us-east-1",
      "subnet_id": "subnet-56343453",
      "source_ami": "ami-61bbf104",
      "instance_type": "t2.micro",
      "ssh_username": "centos",
      "ssh_pty" : true,
      "ami_name": "packer-example {{timestamp}}"
    }],
    "provisioners": [
      {
        "type": "shell",
        "inline": ["sudo sed -i 's/requiretty/!requiretty/' /etc/sudoers"]
      },
      {
        "type": "ansible",
        "playbook_file": "../config/site/packer.yml",
        "user": "centos",
        "ansible_env_vars": [ "ansible_become_user=centos", "ansible_become_pass=packer", "ANSIBLE_HOST_KEY_CHECKING=False", "ANSIBLE_SSH_ARGS='-o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s'" ]
      }
    ]
  }
  </code></pre>
  
  <p><strong>Ansible:</strong></p>
  
  <pre><code>---
    - name: run base centos playbooks
      hosts: all
      connection: local
      become: true
      roles:
       - base_centos7
  </code></pre>