jcdarwin.ec2

ansible-role-ec2

一个可通过 Ansible Galaxy 获取的 Ansible 角色,用于创建 EC2 Ubuntu 镜像。

您可能需要修改/覆盖:

  • defaults/main.yml

安装

我们需要 boto 来创建 EC2 实例。

# 使用 pip 安装 boto
pip install boto

# 存储我们的 AWS 凭证以供 boto 使用
echo -e "[Credentials]\naws_access_key_id = REDACTED\naws_secret_access_key = REDACTED" > ~/.boto

假设 requirements.yml 如下所示:

# 从 GitHub 安装一个角色
- name: ansible-role-ec2
src: https://github.com/jcdarwin/ansible-role-ec2

我们可以使用 requirements.yml 文件在本地安装该角色:

# 从 GitHub 安装一个角色
- name: ansible-role-ec2
src: https://github.com/jcdarwin/ansible-role-ec2
path: roles/

安装角色:

ansible-galaxy install -r requirements.yml -p ./roles

要求

无。

角色变量

可用变量如下所示,包含在 defaults/main.yml 中的默认值:

site:
  name: demo
  environment: development

ansible_role_ec2:
  instance_type: m3.large
  security_group: "{{ site.environment }}-{{ site.name }}"
  # 来源于 https://cloud-images.ubuntu.com/locator/ec2/
  image: ami-bf86aedc # ubuntu 16.04 (ap-southeast-2)
  image_user: ubuntu
  region: ap-southeast-2
  # image: ami-ba4ea4da # amazon linux (us-west-2)
  # image_user: ec2-user
  # region: us-west-2
  keypair: aws
  private_key: ~/.ssh/aws.pem
  count: 1

依赖

无。

示例剧本

我们的 hosts 文件在执行 Ansible 角色时会填充,看起来大概是这样:

[local]
127.0.0.1 ansible_python_interpreter=/usr/local/bin/python

[demo]
52.63.226.70 ansible_user=ubuntu ansible_private_key_file=~/.ssh/aws.pem

我们在 main.yml 中包含了一个剧本。

运行剧本:

# 注意我们假设我们的 hosts 文件是由我们的 vagrant 仓库生成的
ansible all -m ping -i ansible/hosts -l all

ansible-playbook -l all create.yml -i ansible/hosts --tags create

然后我们应该能够使用我们的密钥对进行 ssh 登录——注意 IP 地址由我们的 ansible 角色添加到 ./ansible/hosts 文件中:

# 实际 IP 可以在 ansible/hosts 中找到
ssh -i ~/.ssh/aws.pem ubuntu@52.63.226.70

完成 EC2 实例后,我们可以销毁它:

ansible-playbook -l all destroy.yml -i ansible/hosts --tags destroy

许可证

MIT

作者信息

http://github.com/jcdarwin

关于项目

An Ansible role that creates an EC2 instance running Ubuntu.

安装
ansible-galaxy install jcdarwin.ec2
许可证
Unknown
下载
71
拥有者