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.
First I had a problem with SSH where I received SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh
. I added the connection: local
in my ansible config and it seems to have resolved that.
Now I am running into an issue sudo: a password is required
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 ansible_become_user
and ansible_become_pass
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.
Packer:
{
"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'" ]
}
]
}
Ansible:
---
- name: run base centos playbooks
hosts: all
connection: local
become: true
roles:
- base_centos7