快速入门:使用 Ansible 管理 Azure 中的 Linux 虚拟机Quickstart: Manage Linux virtual machines in Azure using Ansible

使用 Ansible 可以在环境中自动部署和配置资源。Ansible allows you to automate the deployment and configuration of resources in your environment. 在本文中,你将使用 Ansible playbook 来启动和停止 Linux 虚拟机。In this article, you use an Ansible playbook to start and stop a Linux virtual machine.

先决条件Prerequisites

  • Azure 订阅:如果没有 Azure 订阅,可在开始前创建一个 试用帐户Azure subscription: If you don't have an Azure subscription, create a trial account before you begin.

停止虚拟机Stop a virtual machine

在本部分中,你将使用 Ansible 解除分配(停止)Azure 虚拟机。In this section, you use Ansible to deallocate (stop) an Azure virtual machine.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 使用 SSH 命令访问已成功安装并配置了 Ansible 的 Linux 虚拟机。Use SSH command to access the linux virtual machine which the Ansible has been installed and configureed successfully. 将 <your-linux-public-ip-address> 占位符替换为 Linux 虚拟机中的实际 IP 地址。Replace the <your-linux-public-ip-address> placeholder with the actual IP address from the linux virtual machine.

    ssh <your-linux-account>@<your-linux-public-ip-address>
    
  3. 创建一个名为 azure-vm-stop.yml 的文件,并在编辑器中将其打开:Create a file named azure-vm-stop.yml, and open it in theeditor:

    vi azure-vm-stop.yml
    
  4. 通过选择 I 键选择插入模式。Select insert mode by selecting the I key.

  5. 将以下示例代码粘贴到编辑器中:Paste the following sample code into the editor:

    - name: Stop Azure VM
      hosts: localhost
      connection: local
      tasks:
        - name: Stop virtual machine
          azure_rm_virtualmachine:
            resource_group: {{ resource_group_name }}
            name: {{ vm_name }}
            allocated: no
    
  6. 将占位符 {{ resource_group_name }}{{ vm_name }} 替换成自己的值。Replace the {{ resource_group_name }} and {{ vm_name }} placeholders with your values.

  7. Esc 键退出插入模式。Exit insert mode by selecting the Esc key.

  8. 保存文件,然后键入以下命令退出 vi 编辑器:Save the file and exit the vi editor by typing the following command:

    :wq
    
  9. 使用 ansible-playbook 命令运行 playbook:Run the playbook using the ansible-playbook command:

    ansible-playbook azure-vm-stop.yml
    
  10. 运行 playbook 后,可看到类似于以下结果的输出:After running the playbook, you see output similar to the following results:

    PLAY [Stop Azure VM] ********************************************************
    
    TASK [Gathering Facts] ******************************************************
    ok: [localhost]
    
    TASK [Deallocate the Virtual Machine] ***************************************
    changed: [localhost]
    
    PLAY RECAP ******************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0
    

启动虚拟机Start a virtual machine

在本部分中,你将使用 Ansible 启动已解除分配(停止)的 Azure 虚拟机。In this section, you use Ansible to start a deallocated (stopped) Azure virtual machine.

  1. 登录到 Azure 门户Sign in to the Azure portal.

1. 连接已成功安装 ansible 的 linux 虚拟机。1.Connect the linux virtual machine which the ansible have been installed on successfully.

```
ssh <your-linux-account>@<your-linux-public-ip-address>
```
  1. 创建一个名为 azure-vm-start.yml 的文件,并在编辑器中将其打开:Create a file named azure-vm-start.yml, and open it in the editor:

    vi azure-vm-start.yml
    
  2. 通过选择 I 键选择插入模式。Select insert mode by selecting the I key.

  3. 将以下示例代码粘贴到编辑器中:Paste the following sample code into the editor:

    - name: Start Azure VM
      hosts: localhost
      connection: local
      tasks:
        - name: Start virtual machine
          azure_rm_virtualmachine:
            resource_group: {{ resource_group_name }}
            name: {{ vm_name }}
    
  4. 将占位符 {{ resource_group_name }}{{ vm_name }} 替换成自己的值。Replace the {{ resource_group_name }} and {{ vm_name }} placeholders with your values.

  5. Esc 键退出插入模式。Exit insert mode by selecting the Esc key.

  6. 保存文件,然后键入以下命令退出 vi 编辑器:Save the file and exit the vi editor by typing the following command:

    :wq
    
  7. 使用 ansible-playbook 命令运行 playbook:Run the playbook using the ansible-playbook command:

    ansible-playbook azure-vm-start.yml
    
  8. 运行 playbook 后,可看到类似于以下结果的输出:After running the playbook, you see output similar to the following results:

    PLAY [Start Azure VM] ********************************************************
    
    TASK [Gathering Facts] ******************************************************
    ok: [localhost]
    
    TASK [Start the Virtual Machine] ********************************************
    changed: [localhost]
    
    PLAY RECAP ******************************************************************
    localhost                  : ok=2    changed=1    unreachable=0    failed=0