使用 ansible 启动 aws ec2 实例的最佳方式
Best way to launch aws ec2 instances with ansible
我正在尝试在 Amazon AWS 上使用 ansible 创建一个小型 webapp 基础设施,我想完成所有过程:启动实例、配置服务等,但我找不到合适的工具或模块来处理来自ansible的那个。主要是 EC2 Launch。
非常感谢。
EC2 module 专为创建和销毁实例而设计。
如果您想要 "best" 方式,很难击败 CloudFormation,它可以从 Ansible 启动。
正如其他人所说,云模块包含您需要的几乎所有 AWS 供应支持。也就是说,一旦有一台现有的 SSH:able 机器可以定位和连接,Ansible 的范式就最有意义。相比之下,实例化阶段本质上要求您以本地计算机为目标并从那里调用 AWS API 端点。
和您一样,我想要一个从 EC2 实例化到其配置的优雅过渡的单次命令。在 the documentation 中有关于如何完成这样的事情的建议,但它依赖于 add_host 模块来调整 Ansible 对当前主机清单的想法,即使那样我也找不到解决方案感觉我是在反对而不是与系统一起工作。
最后我选择了两个不同的剧本:provision.yml 使用 ec2、ec2_group、ec2_vol、ec2_eip 和 route53 模块来确保我有"hardware" 到位,然后是 configure.yml,更像是传统的 Ansible site.yml,它能够将主机库存(在我的情况下是静态的,但动态会很好地工作)作为给定的并进行所有良好的声明式状态转换。
这两本剧本都是幂等的,但 configure.yml 意味着要在 运行.
中一遍又一遍地重复 运行
这是对您问题的简短回答,如果您需要详细信息和完全自动化的角色,请告诉我。谢谢
先决条件:
Ansible
Python博图库
在环境设置中设置 AWS 访问和秘密密钥
(最好在~./boto里面)
创建 EC2 实例:
为了创建 EC2 实例,请修改这些参数,您可以在 "vars" 下的 "ec2_launch.yml" 文件中找到这些参数:
- region # 想要启动实例的地方,美国,澳大利亚,爱尔兰等
count # 要创建的实例数
一旦您提到了这些参数,请运行执行以下命令:
ansible-playbook -i hosts ec2_launch.yml
hosts 文件的内容:
[local]
localhost
[webserver]
ec2_launch.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: t1.micro
security_group: webserver # Change the security group name here
image: ami-98aa1cf0 # Change the AMI, from which you want to launch the server
region: us-east-1 # Change the Region
keypair: ansible # 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
我正在尝试在 Amazon AWS 上使用 ansible 创建一个小型 webapp 基础设施,我想完成所有过程:启动实例、配置服务等,但我找不到合适的工具或模块来处理来自ansible的那个。主要是 EC2 Launch。
非常感谢。
EC2 module 专为创建和销毁实例而设计。
如果您想要 "best" 方式,很难击败 CloudFormation,它可以从 Ansible 启动。
正如其他人所说,云模块包含您需要的几乎所有 AWS 供应支持。也就是说,一旦有一台现有的 SSH:able 机器可以定位和连接,Ansible 的范式就最有意义。相比之下,实例化阶段本质上要求您以本地计算机为目标并从那里调用 AWS API 端点。
和您一样,我想要一个从 EC2 实例化到其配置的优雅过渡的单次命令。在 the documentation 中有关于如何完成这样的事情的建议,但它依赖于 add_host 模块来调整 Ansible 对当前主机清单的想法,即使那样我也找不到解决方案感觉我是在反对而不是与系统一起工作。
最后我选择了两个不同的剧本:provision.yml 使用 ec2、ec2_group、ec2_vol、ec2_eip 和 route53 模块来确保我有"hardware" 到位,然后是 configure.yml,更像是传统的 Ansible site.yml,它能够将主机库存(在我的情况下是静态的,但动态会很好地工作)作为给定的并进行所有良好的声明式状态转换。
这两本剧本都是幂等的,但 configure.yml 意味着要在 运行.
中一遍又一遍地重复 运行这是对您问题的简短回答,如果您需要详细信息和完全自动化的角色,请告诉我。谢谢
先决条件:
Ansible
Python博图库
在环境设置中设置 AWS 访问和秘密密钥
(最好在~./boto里面)
创建 EC2 实例:
为了创建 EC2 实例,请修改这些参数,您可以在 "vars" 下的 "ec2_launch.yml" 文件中找到这些参数:
- region # 想要启动实例的地方,美国,澳大利亚,爱尔兰等
count # 要创建的实例数
一旦您提到了这些参数,请运行执行以下命令:
ansible-playbook -i hosts ec2_launch.yml
hosts 文件的内容:
[local]
localhost
[webserver]
ec2_launch.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: t1.micro
security_group: webserver # Change the security group name here
image: ami-98aa1cf0 # Change the AMI, from which you want to launch the server
region: us-east-1 # Change the Region
keypair: ansible # 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