使用ansible启动ec2实例时出错

时间:2016-05-12 21:12:27

标签: python ansible ansible-playbook

我试图用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

有人可以提出我可能出错的地方吗?

1 个答案:

答案 0 :(得分:0)

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

Ansible适用于python并选择python目录。因此,请确保使用pip而不是apt-get install awscli安装awscli。使用chrome-extension://

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

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