使用 Ansible 在 Azure 上进行自动部署!
使用Ansible在Azure上引导虚拟机的步骤。

准备Microsoft Azure的一面
-
- Azure アカウント(割愛:https://azure.microsoft.com/ja-jp/get-started/)
Azure CLI インストール
ストレージアカウント
管理証明書
安装 Azure CLI
這個很方便,所以我會安裝上。(適用於Ubuntu 14.04的情況)
$ sudo apt-get install nodejs-legacy
$ sudo apt-get install npm
$ sudo npm install -g azure-cli
npm http GET https://registry.npmjs.org/azure-cli
npm http 200 https://registry.npmjs.org/azure-cli
(略)
/usr/local/bin/azure -> /usr/local/lib/node_modules/azure-cli/bin/azure
azure-cli@0.9.9 /usr/local/lib/node_modules/azure-cli
$ azure help
info: Executing command help
info: _ _____ _ ___ ___
info: /_\ |_ / | | | _ \ __|
info: _ ___/ _ \__/ /| |_| | / _|___ _ _
info: (___ /_/ \_\/___|\___/|_|_\___| _____)
info: (_______ _ _) _ ______ _)_ _
info: (______________ _ ) (___ _ _)
info:
info: Microsoft Azure: Microsoft's Cloud Platform
info:
info: Tool version 0.9.9
(略)
help: Current Mode: asm (Azure Service Management)
$
使用 Azure CLI 时需要一些准备工作,并且需要进行一些设置才能验证 Azure 账户。
$ azure account download
info: Executing command account download
info: Launching browser to http://
help: Save the downloaded file, then execute the command
help: account import
info: account download command OK
按照指示下载文件,并按照以下方式进行导入。
$ azure account import credentials.publishsettings
info: Executing command account import
info: account import command OK
这样就可以使用 Azure CLI 了。
存储帐户
可以使用Azure门户或Azure CLI进行创建。在创建时,请注意选择合适的位置。例如,在日本选择”日本西部(Japan West)”。这样可以与稍后启动的虚拟机保持相同的位置。创建帐户后,会确认存储帐户名称。具体的创建步骤不再赘述。
只要虚拟机被创建过至少一次,存储帐户将自动根据其创建的条件创建。
证书管理
为了在Azure上验证从外部API访问的帐户所需的东西。有几种方法,但最简单的方法是使用Azure CLI。如果使用Azure CLI,则已经注册了证书,您可以从那里导出并获取。
$ azure acount cert export -f ./manage.cer
总结一下,在Azure CLI中,可以执行以下操作来确认订阅ID、存储账户名,并获取管理证书。
$ azure account list
$ azure storage account list
$ azure acount cert export -f ./manage.cer
准备 Ansible 机器
Ansible 安装
准备一个安装有Ansible的Linux计算机。在这里我们使用Ubuntu 14.04。
实际上,为了在Azure上启动基于Windows的虚拟机,需要使用更高版本的Ansible(2.0.0或更高),而不是当前版本的1.9.x。
$ git clone https://github.com/ansible/ansible.git
$ cd ansible
$ git checkout v2.0.0-0.3.beta1
$ git submodule update --init
$ . hacking/env-setup
$ ansible --version
ansible 2.0.0 (detached HEAD 5ae74f7d38) last updated 2015/10/12 07:19:07 (GMT +000)
lib/ansible/modules/core: (detached HEAD d659c79db7) last updated 2015/10/12 07:19:50 (GMT +000)
lib/ansible/modules/extras: (detached HEAD 0ff1776a84) last updated 2015/10/12 07:20:14 (GMT +000)
config file = /etc/ansible/ansible.cf
以下是Ansible中提供的Azure模块在以下环境中的支持。
-
- Python >= 2.6
- Python azure module >= 0.8.0 (Ansible マニュアルは少し古い情報)
安装Python的Azure模块以进行补充。
$ sudo pip install azure==0.11.1
$ sudo pip show azure
---
Name: azure
Version: 0.11.1
Location: /usr/local/lib/python2.7/dist-packages
Requires: python-dateutil, futures
目前,此模块的最新版本是1.0.2,但由于已知1.0.2与Ansible的Azure模块不兼容,所以我们正在使用0.11.1版本。
此次更新已经合并到开发中的2.0.0版本。
https://github.com/ansible/ansible-modules-core/pull/2114
Ansible的准备工作(清单文件和剧本)
实施Ansible需要的文件包括”清单文件”和”playbook”文件。本次分别如下所述创建。
127.0.0.1 ansible_connection=local
---
- hosts: localhost
gather_facts: no
tasks:
- name: Create Virtual Machine on Azure (for Windows)
azure: # Ansible の azure モジュールを使うことを宣言し、以下はそのオプション
auto_updates: no
enable_winrm: yes
endpoints: "3389"
image: 'bd507d3a70934695bc2128e3e5a255ba__RightImage-Windows-2012R2-x64-v14.2'
location: 'Japan West'
management_cert_path: /home/vagrant/azure/manage.cer # 取得した管理証明書
hostname: hogehoge
name: hogehoge
os_type: windows
user: azureuser
password: "Password_123"
role_size: Basic_A2
state: present
storage_account: # 取得したストレージアカウント名
subscription_id: # 取得したすサブスクリプションID
在上述的每个选项中,只有在获取Azure端信息后才能知道它们的内容。这些选项包括image、location和role_size。您可以自行选择要指定的内容,但可以使用Azure CLI来确认image和location。
$ azure vm image list
$ azure vm location list
根据以下内容,role_size代表了虚拟机的大小(规格):
“ExtraSmall”、”Small”、”Medium”、”Large”、”ExtraLarge”、”A5″、”A6″、”A7″、”A8″、”A9″、”A10″、”A11″、”Basic_A0″、”Basic_A1″、”Basic_A2″、”Basic_A3″、”Basic_A4″、”Standard_D1″、”Standard_D2″、”Standard_D3″、”Standard_D4″、”Standard_D11″、”Standard_D12″、”Standard_D13″、”Standard_D14″、”Standard_DS1″、”Standard_DS2″、”Standard_DS3″、”Standard_DS4″、”Standard_DS11″、”Standard_DS12″、”Standard_DS13″、”Standard_DS14″、”Standard_G1″、”Standard_G2″、”Standard_G3″、”Standard_G4″、”Standard_G55″。
可用的规格因位置而异。role_size的默认值是”Small”。
执行 Ansible
$ ansible-playbook -i hosts playbook.yml
确认
从Azure CLI或Azure门户中进行确认
补充
重置首次登录时的密码参数已被废弃。
根据这个问题,不能使用Ansible Azure模块选项reset_pass_atlogon。我已经存在的问题2245。
请参考
-
- https://azure.microsoft.com/ja-jp/get-started/
-
- https://azure.microsoft.com/ja-jp/documentation/articles/xplat-cli-install/
-
- https://azure.microsoft.com/ja-jp/documentation/
-
- https://azure.microsoft.com/ja-jp/documentation/articles/virtual-machines-command-line-tools/
- http://docs.ansible.com/ansible/intro_installation.html