【问题标题】:error starting ec2 instance with ansible使用 ansible 启动 ec2 实例时出错
【发布时间】:2016-05-14 15:27:14
【问题描述】:

我正在尝试从 ansible 开始,特别是使用 ansible playbook 来部署 ec2 实例,但我不断收到错误。

我已经关注了在这个线程中找到的代码:Best way to launch aws ec2 instances with ansible

我已经用我自己的详细信息代替了以下内容

主机文件:

[local]
localhost

[webserver]

create_instance.yml

---
  - name: Provision an EC2 Instance
    hosts: local
    connection: local
    gather_facts: False
    tags: provisioning
    # Necessary Variables for creating/provisioning the EC2 Instance
    vars:
      instance_type: t2.micro
      security_group: webserver # Change the security group name here
      image: ami-f95ef58a # Change the AMI, from which you want to launch     the server
      region: eu-west-1 # Change the Region
      keypair: MyKeyPair # Change the keypair name
      count: 1

      # Task that will be used to Launch/Create an EC2 Instance
        tasks:

    - name: Create a security group
      local_action: 
      module: ec2_group
      name: "{{ security_group }}"
      description: Security Group for webserver Servers
      region: "{{ region }}"
      rules:
        - proto: tcp
          type: ssh
          from_port: 22
          to_port: 22
          cidr_ip: 0.0.0.0/0
        - proto: tcp
          from_port: 80
          to_port: 80
          cidr_ip: 0.0.0.0/0
      rules_egress:
        - proto: all
          type: all
          cidr_ip: 0.0.0.0/0


  - name: Launch the new EC2 Instance
    local_action: ec2 
                  group={{ security_group }} 
                  instance_type={{ instance_type}} 
                  image={{ image }} 
                  wait=true 
                  region={{ region }} 
                  keypair={{ keypair }}
                  count={{count}}
    register: ec2

  - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
    local_action: lineinfile 
                  dest="./hosts" 
                  regexp={{ item.public_ip }} 
                  insertafter="[webserver]" line={{ item.public_ip }}
    with_items: ec2.instances


  - name: Wait for SSH to come up
    local_action: wait_for 
                  host={{ item.public_ip }} 
                  port=22 
                  state=started
    with_items: ec2.instances

  - name: Add tag to Instance(s)
    local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
    with_items: ec2.instances
    args:
      tags:
        Name: webserver    

然后我为我的 AWS 密钥创建环境变量,如下所示:

export AWS_ACCESS_KEY=my aws key
export AWS_SECRET_KEY=my aws secret key

当我运行我的代码时 sudo ansible-playbook -i hosts create_instance.yml 我收到以下错误:

PLAY [localhost]     **************************************************************

TASK: [make one instance] *****************************************************
failed: [localhost] => {"failed": true}
msg: No handler was ready to authenticate. 1 handlers were checked.     ['HmacAuthV4Handler'] Check your credentials

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/home/ubuntu/create_instance.retry

localhost                  : ok=0    changed=0    unreachable=0    failed=1

谁能建议我哪里出错了?

【问题讨论】:

  • 不要使用 sudo。 root 用户(大概)没有加载您的环境变量
  • 感谢您的建议,但仍然没有运气。只是为了确认我正确设置了密钥对,是否应该将我的 yml 文件中的“密钥对”变量设置为已上传到 AWS 的密钥对的名称?我的意思是我用来通过 AWS 控制台创建新 ec2 实例的密钥对的名称?这就是我目前一直在使用的,但只是想检查它是否正确。
  • 它并没有那么远。它失败了,因为您没有正确设置 aws aconnection 变量。如果您使用 sudo 那么它不会加载您在当前 shell 中导出的变量。另一种方法是在剧本或库存中指定 aws_access_keyaws_secret_key 变量。

标签: python ansible ansible-playbook


【解决方案1】:

当您的 ansible 主机无法与您的 AWS 账户建立连接时,就会出现此错误。为此,您需要确保正确设置了访问密钥并具有足够的权限来创建实例。

Ansible 在 python 上工作并选择 python 目录。因此,请确保您使用 pip 而不是 apt-get install awscli 安装了 awscli。使用sudo pip install awscli

在文件 ~/.aws/credentials 中指定您的访问密钥。

还要确保您安装了更新版本的 boto 和 python。 请参阅此http://www.dowdandassociates.com/blog/content/howto-install-aws-cli-security-credentials/。这里很好地提到了所有配置密钥的方法。

【讨论】:

  • 谢谢。我按照您的步骤进行操作,但仍然遇到问题。我得到的错误如下....身份验证或权限失败。在某些情况下,您可能已经能够进行身份验证并且没有远程目录的权限。考虑将 ansible.cfg 中的远程临时路径更改为以“/tmp”为根的路径。失败的命令是:mkdir -p $HOME/.ansible/tmp/ansible-tmp-1463751636.36-33202375925080 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1463751636.36-33202375925080 && echo $HOME/.ansible/ tmp/ansible-tmp-1463751636.36-33202375925080,退出结果为 1
  • 哦,这意味着你没有权限在你的 ansible 盒子上创建目录。你能确定在 $HOME 上创建的 /.ansible/tmp/ 目录的写权限和所有权吗?你正在用同一个用户运行这个 ansible playbook 吗?
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多