您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用 Ansible 在 Azure 中部署虚拟机规模集Tutorial: Deploy apps to virtual machine scale sets in Azure using Ansible

重要

运行本文中的示例 playbook 需要 Ansible 2.7(或更高版本)。Ansible 2.7 (or later) is required to run the sample playbooks in this article.

Azure 虚拟机规模集是一项 Azure 功能,可让你配置一组相同的、负载均衡的 VM。Azure virtual machine scale sets is an Azure feature that lets you configure a group of identical, load balanced VMs. 规模集不需要额外的成本,它们是从虚拟机构建的。There's no additional cost to scale sets and they're built from virtual machines. 只需为基础的计算资源(例如 VM 实例、负载均衡器或托管磁盘存储)付费。You pay only for the underlying compute resources such as the VM instances, load balancers, or Managed Disk storage. 使用规模集时,会提供管理层和自动层来运行和缩放应用程序。With scale sets, the management and automation layers are provided to run and scale your applications. 你可以改为手动创建和管理各个 VM。You could instead manually create and manage individual VMs. 但是,使用规模集有两个主要优点。However, there are two key benefits to using scale sets. 它们内置于 Azure 中,并自动缩放虚拟机以满足应用程序需求。They're built into Azure and they automatically scale your virtual machines to meet application needs.

在本教程中,Ansible 用于:In this tutorial, Ansible is used to:

  • 检索一组 Azure VM 的主机信息Retrieve host information for a group of Azure VMs
  • 克隆并构建示例应用Clone and build the sample app
  • 在规模集上安装 JRE(Java 运行时环境)Install the JRE (Java Runtime Environment) on a scale set
  • 将 Java 应用程序部署到规模集Deploy the Java application to a scale set

先决条件Prerequisites

  • Azure 订阅:如果还没有 Azure 订阅,可以在开始前创建一个 免费帐户Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
  • git - git 用于下载本教程中使用的 Java 示例。git - git is used to download a Java sample used in this tutorial.
  • Java SE 开发工具包 (JDK) - 此 JDK 用于生成示例 Java 项目。Java SE Development Kit (JDK) - The JDK is used to build the sample Java project.
  • Apache Maven - Apache Maven 用于构建示例 Java 项目。Apache Maven - Apache Maven is used to build the sample Java project.

获取主机信息Get host information

本部分中的 playbook 代码检索一组虚拟机的主机信息。The playbook code in this section retrieves host information for a group of virtual machines. 该代码获取指定资源组中的公共 IP 地址和负载均衡器,并在清单中创建名为 scalesethosts 的主机组。The code gets the public IP addresses and load balancer within a specified resource group and creates a host group named scalesethosts in inventory.

将以下示例 playbook 保存为 get-hosts-tasks.ymlSave the following sample playbook as get-hosts-tasks.yml:

- name: Get facts for all Public IPs within a resource groups
  azure_rm_publicipaddress_facts:
    resource_group: "{{ resource_group }}"
  register: output_ip_address

- name: Get loadbalancer info
  azure_rm_loadbalancer_facts:
    resource_group: "{{ resource_group }}"
    name: "{{ loadbalancer_name }}"
  register: output

- name: Add all hosts
  add_host:
    groups: scalesethosts
    hostname: "{{ output_ip_address.ansible_facts.azure_publicipaddresses[0].properties.ipAddress }}_{{ item.properties.frontendPort }}"
    ansible_host: "{{ output_ip_address.ansible_facts.azure_publicipaddresses[0].properties.ipAddress }}"
    ansible_port: "{{ item.properties.frontendPort }}"
    ansible_ssh_user: "{{ admin_username }}"
    ansible_ssh_pass: "{{ admin_password }}"
  with_items:
    - "{{ output.ansible_facts.azure_loadbalancers[0].properties.inboundNatRules }}"

准备用于部署的应用程序Prepare an application for deployment

本部分中的 playbook 代码使用 git 从 GitHub 克隆 Java 示例项目并构建项目。The playbook code in this section uses git to clone a Java sample project from GitHub and builds the project.

将以下 playbook 保存为 app.ymlSave the following playbook as app.yml:

- hosts: localhost
  vars:
    repo_url: https://github.com/spring-guides/gs-spring-boot.git
    workspace: ~/src/helloworld

  tasks:
  - name: Git Clone sample app
    git:
      repo: "{{ repo_url }}"
      dest: "{{ workspace }}"

  - name: Build sample app
    shell: mvn package chdir="{{ workspace }}/complete"

使用以下命令运行示例 Ansible playbook:Run the sample Ansible playbook with the following command:

ansible-playbook app.yml

运行 playbook 后,可看到类似于以下结果的输出:After running the playbook, you see output similar to the following results:

PLAY [localhost] 

TASK [Gathering Facts] 
ok: [localhost]

TASK [Git Clone sample app] 
changed: [localhost]

TASK [Build sample app] 
changed: [localhost]

PLAY RECAP 
localhost                  : ok=3    changed=2    unreachable=0    failed=0

将应用程序部署到规模集Deploy the application to a scale set

本部分中的 playbook 代码用于:The playbook code in this section is used to:

  • 在名为 saclesethosts 的主机组上安装 JREInstall the JRE on a host group named saclesethosts
  • 将 Java 应用程序部署到名为 saclesethosts 的主机组Deploy the Java application to a host group named saclesethosts

可通过两种方式获取示例 playbook:There are two ways to get the sample playbook:

  • 下载 playbook 并将其保存到 vmss-setup-deploy.ymlDownload the playbook and save it to vmss-setup-deploy.yml.
  • 新建名为 vmss-setup-deploy.yml 的文件,并将以下内容复制到其中:Create a new file named vmss-setup-deploy.yml and copy into it the following contents:
- hosts: localhost
  vars:
    resource_group: myResourceGroup
    scaleset_name: myScaleSet
    loadbalancer_name: myScaleSetLb
    admin_username: azureuser
    admin_password: "{{ admin_password }}"
  tasks:
  - include: get-hosts-tasks.yml

- name: Install JRE on a scale set
  hosts: scalesethosts
  become: yes
  vars:
    workspace: ~/src/helloworld
    admin_username: azureuser

  tasks:
  - name: Install JRE
    apt:
      name: default-jre
      update_cache: yes

  - name: Copy app to Azure VM
    copy:
      src: "{{ workspace }}/complete/target/gs-spring-boot-0.1.0.jar"
      dest: "/home/{{ admin_username }}/helloworld.jar"
      force: yes
      mode: 0755

  - name: Start the application
    shell: java -jar "/home/{{ admin_username }}/helloworld.jar" >/dev/null 2>&1 &
    async: 5000
    poll: 0

运行 playbook 之前,请参阅以下说明:Before running the playbook, see the following notes:

  • vars 部分中,请将 {{ admin_password }} 占位符替换为你自己的密码。In the vars section, replace the {{ admin_password }} placeholder with your own password.

  • 若要将 ssh 连接类型与密码一起使用,请安装 sshpass 程序:To use the ssh connection type with passwords, install the sshpass program:

    Ubuntu:Ubuntu:

    apt-get install sshpass
    

    CentOS:CentOS:

    yum install sshpass
    
  • 在某些环境中,可能会看到有关使用 SSH 密码而不是密钥的错误。In some environments, you may see an error about using an SSH password instead of a key. 如果收到该错误,可通过将以下行添加到 /etc/ansible/ansible.cfg~/.ansible.cfg 来禁用主机密钥检查:If you do receive that error, you can disable host key checking by adding the following line to /etc/ansible/ansible.cfg or ~/.ansible.cfg:

    [defaults]
    host_key_checking = False
    

使用以下命令运行 playbook:Run the playbook with the following command:

ansible-playbook vmss-setup-deploy.yml

运行 ansible-playbook 命令的输出指示示例 Java 应用程序已安装到规模集的主机组:The output from running the ansible-playbook command indicates that the sample Java application has been installed to the host group of the scale set:

PLAY [localhost]

TASK [Gathering Facts]
ok: [localhost]

TASK [Get facts for all Public IPs within a resource groups]
ok: [localhost]

TASK [Get loadbalancer info]
ok: [localhost]

TASK [Add all hosts]
changed: [localhost] ...

PLAY [Install JRE on scale set]

TASK [Gathering Facts]
ok: [40.114.30.145_50000]
ok: [40.114.30.145_50003]

TASK [Copy app to Azure VM]
changed: [40.114.30.145_50003]
changed: [40.114.30.145_50000]

TASK [Start the application]
changed: [40.114.30.145_50000]
changed: [40.114.30.145_50003]

PLAY RECAP
40.114.30.145_50000        : ok=4    changed=3    unreachable=0    failed=0
40.114.30.145_50003        : ok=4    changed=3    unreachable=0    failed=0
localhost                  : ok=4    changed=1    unreachable=0    failed=0

验证结果Verify the results

通过导航到规模集负载均衡器的 URL 来验证工作结果:Verify the results of your work by navigating to the URL of the load balancer for your scale set:

在 Azure 的规模集中运行的 Java 应用。

后续步骤Next steps