【发布时间】:2018-01-03 21:47:30
【问题描述】:
我想要实现的目标
我想使用一个 Ansible playbook 创建一个安装了 LAMP 堆栈的 EC2 实例。
问题
实例创建工作正常,我可以在 EC2 控制台中对其进行修改,但在尝试访问实例时出现问题,例如安装 apache 或创建密钥。
这是错误:
致命:[35.154.26.86]:无法访问! => { “改变”:错误, "msg": "[Errno None] 无法连接到端口 22 或 35.154.26.86", “无法到达”:是的 }
代码
这是我的剧本:
---
- name: Power up an ec2 with LAMP stack installed
hosts: localhost
become: true
become_user: root
gather_facts: False
vars:
keypair: myKeyPair
security_group: launch-wizard-1
instance_type: t2.micro
image: ami-47205e28
region: x-x-x
tasks:
- name: Adding Python-pip
apt: name=python-pip state=latest
- name: Install Boto Library
pip: name=boto
- name: Launch instance (Amazon Linux)
ec2:
key_name: "{{ keypair }}"
group: "{{ security_group }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
wait: true
region: "{{ region }}"
aws_access_key: "xxxxxxxxxxxxxxxxxxx"
aws_secret_key: "Xxxxxxxxxxxxxxxxxxx"
register: ec2
- name: Print all ec2 variables
debug: var=ec2
- name: Add all instance public IPs to host group
add_host: hostname={{ item.public_ip }} groups=ec2hosts
with_items: "{{ ec2.instances }}"
- hosts: ec2hosts
remote_user: ec2-user
become: true
gather_facts: false
tasks:
#I need help here, don't know what to do.
- name: Create an EC2 key
ec2_key:
name: "privateKey"
region: "x-x-x"
register: ec2_key
- name: Save private key
copy: content="{{ ec2_key.private_key }}" dest="./privateKey.pem" mode=0600
when: ec2_key.changed
# The Rest is installing LAMP
信息:
1- 我的主机文件是默认的。
2- 我使用这个命令来运行 playbook:
sudo ansible-playbook lamp.yml -vvv -c paramiko
3-launch-wizard-1 有 SSH。
4- myKeyPair 是从我的设备导入到控制台的公钥(不知道这样是否可以)
5- 我是个大新手
【问题讨论】:
-
你为什么不按照docs的例子呢?请注意等待 SSH 启动 任务。您将遇到的下一个问题是在远程主机上执行
ec2_key。 -
您的安全组中是否打开了 22 端口?如果是,您可以分享您的主机文件吗?
标签: amazon-web-services amazon-ec2 ssh ansible