我试图做一个开始与ansible,特别是使用ansible playbook来部署ec2实例,但我不断收到错误。错误开始ec2实例与ansible
我跟随代码中发现在这个线程:Best way to launch aws ec2 instances with ansible
我在我自己的细节取代给我下面的
hosts文件:
[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
当我与 须藤ansible-剧本-i主机上运行我的代码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。根用户(可能)没有你的环境变量加载 – ydaetskcoR
感谢您的建议,但仍然没有运气。为了确认我正确设置密钥对,是否应将我的yml文件中的“密钥对”变量设置为已上传到AWS的密钥对的名称?我的意思是我用来通过AWS控制台创建新的ec2实例的密钥对的名称?这是我目前使用的,但只是想检查它是正确的。 – Rjodo
它没有那么远。这是失败的,因为你没有正确设置aws连接变量。如果您使用sudo,那么它不会加载您在当前shell中导出的变量。另一种方法是在剧本或库存中指定“aws_access_key”和“aws_secret_key”变量。 – ydaetskcoR