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

快速入门:使用 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 free 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. 打开 Cloud ShellOpen Cloud Shell.

  3. 创建一个名为 azure-vm-stop.yml 的文件,并在编辑器中将其打开:Create a file named azure-vm-stop.yml, and open it in the editor:

    code azure-vm-stop.yml
    
  4. 将以下示例代码粘贴到编辑器中: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
    
  5. 将占位符 {{ resource_group_name }}{{ vm_name }} 替换成自己的值。Replace the {{ resource_group_name }} and {{ vm_name }} placeholders with your values.

  6. 保存文件并退出编辑器。Save the file and exit the editor.

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

    ansible-playbook azure-vm-stop.yml
    
  8. 运行 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.

  2. 打开 Cloud ShellOpen Cloud Shell.

  3. 创建一个名为 azure-vm-start.yml 的文件,并在编辑器中将其打开:Create a file named azure-vm-start.yml, and open it in the editor:

    code azure-vm-start.yml
    
  4. 将以下示例代码粘贴到编辑器中: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 }}
    
  5. 将占位符 {{ resource_group_name }}{{ vm_name }} 替换成自己的值。Replace the {{ resource_group_name }} and {{ vm_name }} placeholders with your values.

  6. 保存文件并退出编辑器。Save the file and exit the editor.

  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
    

后续步骤Next steps