Merge pull request #7821 from qianghaohao/master

完成翻译:“20180210 How to create AWS ec2 key using Ansible.md”
This commit is contained in:
Xingyu.Wang 2018-02-20 23:17:03 +08:00 committed by GitHub
commit 7545aab13c

View File

@ -1,36 +1,33 @@
@qianghaohao 翻译中
How to create AWS ec2 key using Ansible
如何使用 Ansible 创建 AWS ec2 密钥
======
我想使用 Ansible 工具创建 Amazon EC2 密钥对。不想使用 AWS CLI 来创建。可以使用 Ansible 来创建 AWS ec2 密钥吗?
I wanted to create Amazon EC2 Key pair using Ansible tool. I do not want to use AWS CLI. Is it possible to create AWS ec2 key using Ansible?
你需要使用 Ansible 的 ec2_key 模块。这个模块依赖于 python-boto 2.5 版本或者更高版本。 boto 只不过是亚马逊 Web 服务的一个 Python API。你可以将 boto 用于 Amazon S3Amazon EC2 等其他服务。简而言之,你需要安装 ansible 和 boto 模块。我们一起来看下如何安装 boto 并结合 Ansible 使用。
You need to use ec2_key module of Ansible. This module has a dependency on python-boto version 2.5 or above. boto is nothing but a python interface to Amazon Web Services using API. You can use boto for services like Amazon S3, Amazon EC2 and others. In short, you need ansible installed along with boto module. Let us see how to install boto and use it with Ansbile.
### Step 1 [Install latest version of Ansible on Ubuntu Linux][1]
You must [configure the PPA on your system to install the latest version of ansible][2]. To manage the repositories that you install software from various PPA (Personal Package Archives). It allow you to upload Ubuntu source packages to be built and published as an apt repository by Launchpad. Type the following [apt-get command][3] or [apt command][4]:
### 第一步 - [在 Ubuntu 上安装最新版本的 Ansible][1]
你必须[给你的系统配置 PPA 来安装最新版的 ansible][2]。为了管理你从各种 PPA(Personal Package Archives) 安装软件的仓库,你可以上传 Ubuntu 源码包并编译,然后通过 Launchpad 以 apt 仓库的形式发布。键入如下命令 [apt-get 命令][3]或者 [apt 命令][4]
```
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install software-properties-common
```
Next add ppa:ansible/ansible to your systems Software Source:
接下来给你的系统的软件源中添加 ppa:ansible/ansible
```
$ sudo apt-add-repository ppa:ansible/ansible
```
Update your repos and install ansible:
更新你的仓库并安装ansible:
```
$ sudo apt update
$ sudo apt install ansible
```
Install boto:
安装 boto
```
$ pip3 install boto3
```
#### A note about installing Ansible on CentOS/RHEL 7.x
#### 关于在CentOS/RHEL 7.x上安装Ansible的注意事项
You [need to setup EPEL repo on a CentOS and RHEL 7.x][5] along with the [yum command][6]:
你[需要在 CentOS 和 RHEL 7.x 上配置 EPEL 源][5]和 [yum命令][6]
```
$ cd /tmp
$ wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
@ -38,14 +35,14 @@ $ ls *.rpm
$ sudo yum install epel-release-latest-7.noarch.rpm
$ sudo yum install ansible
```
Install boto:
安装 boto
```
$ pip install boto3
```
### Step 2 Configure boto
### 第二步 2 配置 boto
You need to setup AWS credentials/API keys. See “[AWS Security Credentials][7]” documents on how to create a programmatic API key. Create a directory called ~/.aws using the mkdir command and setup API keys:
你需要配置 AWS credentials/API 密钥。参考 “[AWS Security Credentials][7]” 文档如何创建 API key。用 mkdir 命令创建一个名为 ~/.aws 的目录,然后配置 API key
```
$ mkdir -pv ~/.aws/
$ vi ~/.aws/credentials
@ -56,15 +53,15 @@ aws_access_key_id = YOUR-ACCESS-KEY-HERE
aws_secret_access_key = YOUR-SECRET-ACCESS-KEY-HERE
```
Also setup default [AWS region][8]:
还需要配置默认 [AWS 区域][8]
`$ vi ~/.aws/config`
Sample outputs:
输出样例如下:
```
[default]
region = us-west-1
```
Test your boto setup with API by creating a simple python program named test-boto.py:
通过创建一个简单的名为 test-boto.py 的 python 程序来测试你的 boto 配置是否正确:
```
#!/usr/bin/python3
# A simple program to test boto and print s3 bucket names
@ -72,11 +69,11 @@ import boto3
t = boto3.resource('s3')
for b in t.buckets.all():
print(b.name)
```
```
Run it as follows:
按下面方式来运行该程序:
`$ python3 test-boto.py`
Sample outputs:
输出样例:
```
nixcraft-images
nixcraft-backups-cbz
@ -84,11 +81,11 @@ nixcraft-backups-forum
```
The output confirmed that Python-boto working correctly using AWS API.
上面输出可以确定 Python-boto 可以使用 AWS API 正常工作。
### Step 3 Create AWS ec2 key using Ansible
### 步骤 3 - 使用 Ansible 创建 AWS ec2 密钥
Create a playbook named ec2.key.yml as follows:
创建一个名为 ec2.key.yml 的 playbook如下所示
```
---
- hosts: local
@ -107,45 +104,46 @@ Create a playbook named ec2.key.yml as follows:
when: ec2_key_result.changed
```
Where,
其中,
* ec2_key: Maintains ec2 key pair.
* name: nixcraft_key Name of the key pair.
* region: us-west-1 The AWS region to use.
* register: ec2_key_result : Save result of generated key to ec2_key_result variable.
* copy: content="{{ ec2_key_result.key.private_key }}" dest="./aws.nixcraft.pem" mode=0600 : Sets the contents of ec2_key_result.key.private_key to a file named aws.nixcraft.pem in the current directory. Set mode of the file to 0600 (unix file permissions).
* when: ec2_key_result.changed : Only save when ec2_key_result changed is set to true. We dont want to overwrite our key file.
* ec2_key: ec2 密钥对。
* name: nixcraft_key 密钥对的名称。
* region: us-west-1 使用的 AWS 区域。
* register: ec2_key_result : 保存生成的密钥到 ec2_key_result 变量。
* copy: content="{{ ec2_key_result.key.private_key }}" dest="./aws.nixcraft.pem" mode=0600 : 将 ec2_key_result.key.private_key 的内容保存到当前目录的一个名为 aws.nixcraft.pem 的文件中。设置该文件的权限为 0600 (unix 文件权限).
* when: ec2_key_result.changed : 仅仅在 ec2_key_result 改变时才保存。我们不想覆盖你的密钥文件。
You must create hosts file as follows too:
你还必须创建如下主机文件:
```
[local]
localhost
```
Run your playbook as follows:
如下运行你的 playbook
`$ ansible-playbook -i hosts ec2.key.yml`
![](https://www.cyberciti.biz/media/new/faq/2018/02/How-to-create-AWS-ec2-key-using-Ansible.jpg)
At the end you should have a private key named aws.nixcraft.pem that you can use with AWS EC2. To view your key use the [cat command][9]:
最后你应该有一个名为 aws.nixcraft.pem 私钥,该私钥可以和 AWS EC2 一起使用。查看你的密钥 [cat 命令][9]
```
$ cat aws.nixcraft.pem
```
If you have EC2 VM, use it as follows:
如果你有 EC2 虚拟机,请按如下方式使用:
```
$ ssh -i aws.nixcraft.pem user@ec2-vm-dns-name
```
#### Finding out info about python data structure variable names such as ec2_key_result.changed and ec2_key_result.key.private_key
#### 查看有关 python 数据结构变量名的信息,比如 ec2_key_result.changed 和 ec2_key_result.key.private_key
You must be wondering how come I am using variable names such as ec2_key_result.changed and ec2_key_result.key.private_key. Are they defined somewhere? Values are returned from API calls. Simply run the ansible-playbook command with the -v option to see such info:
你一定在想我是如何使用变量名的,比如 ec2_key_result.changed 和 ec2_key_result.key.private_key。它们在哪里定义过吗变量的值是通过 API 调用返回的。简单地使用 -v 选项运行 ansible-playbook 命令来查看这样的信息:
`$ ansible-playbook -v -i hosts ec2.key.yml`
![](https://www.cyberciti.biz/media/new/faq/2018/02/ansible-verbose-output.jpg)
### How do I delete a key?
### 我该如何删除一个密钥?
Use the following ec2-key-delete.yml:
使用如下 ec2-key-delete.yml
```
---
- hosts: local
@ -161,20 +159,20 @@ Use the following ec2-key-delete.yml:
state: absent
```
Run it as follows:
按照如下方式运行:
`$ ansible-playbook -i hosts ec2-key-delete.yml`
### about the author
### 关于作者
The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the **latest tutorials on SysAdmin, Linux/Unix and open source topics via[RSS/XML feed][10]** or [weekly email newsletter][11].
作者是 nixCraft 的创始人是一个经验丰富的系统管理员DevOps 工程师,同时是一个 Linux 操作系统/Unix shell 脚本培训师。**通过 [RSS/XML 提要][10]或[每周邮件简讯][11]获得关于系统管理Linux/Unix和开放源码主题的最新教程。**
--------------------------------------------------------------------------------
via: https://www.cyberciti.biz/faq/how-to-create-aws-ec2-key-using-ansible/
作者:[Vivek Gite][a]
译者:[译者ID](https://github.com/译者ID)
译者:[qianghaohao](https://github.com/qianghaohao)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出