Packer和Ansible SSH / Sudo

时间:2016-09-26 21:53:59

标签: amazon-web-services ansible packer

我正在尝试设置packer和ansible-remote,根据我之前存在的ansible脚本创建一个AMI。我遇到了两个问题之一。

首先我遇到了SSH问题,我收到了SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh。我在我的ansible配置中添加了connection: local,似乎已经解决了这个问题。

现在我遇到来自Ansible的问题sudo: a password is required。我不清楚为什么我指定的用户使用NOPASSWD进行sudo访问,我已经通过使用临时密钥的封装器设置来验证这一点。我收到以下错误,并尝试通过打包器传递ansible_become_useransible_become_pass作为vars而没有运气。看起来好像它现在试图sudo我的本地连接但需要密码?任何想法如何正确设置。

打包机:

{
  "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

3 个答案:

答案 0 :(得分:1)

SSH Error: data could not be sent to the remote host. Make sure this host can be reached over ssh

使用ansible_python_interpreter = / usr / bin / python3

修复此错误

因为packer生成库存文件,最好的方法是创建一个[ubuntu16] ansible组,并在group_vars中将ansible_python_interpreter设置为/ usr / bin / python3

不幸的是,它看起来像是一个连接问题。因为我花了几个小时来解决这个问题。

答案 1 :(得分:0)

sudo密码问题将完全取决于您的源AMI(“source_ami”:“ami-61bbf104”)。

如果AMI是自我管理的,您需要确保您使用'centos'的用户能够sudo并提供其他安全措施(ssh密钥),您可以删除密码。或者你可以在运行ansible时使用标志'--ask-become-pass',但我不确定这对Packer的效果如何。

如果AMI是AWS托管图像,我建议将“ec2-user”与Packer一起使用,然后Packer将使用公共角色配置图像上的用户,并使用“become_user”向相关用户应用配置。

有用的链接:

答案 2 :(得分:-1)

我认为您不需要那里的用户centos,因为您已经以centos用户身份连接。

阿尔瓦罗