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

快速入门:通过 Azure Cloud Shell 中的 Bash 运行 Ansible PlaybookQuickstart: Run Ansible playbooks via Bash in Azure Cloud Shell

Azure Cloud Shell 是一个用于管理 Azure 资源的交互式的可通过浏览器访问的 shell。Azure Cloud Shell is an interactive, browser-accessible shell for managing Azure resources. Cloud Shell 允许使用 Bash 或 Powershell 命令行。Cloud Shell provides enables you to use either a Bash or Powershell command line. 在本文中,你将使用 Azure Cloud Shell 中的 Bash 运行一个 Ansible Playbook。In this article, you use Bash within Azure Cloud Shell to run an Ansible playbook.

先决条件Prerequisites

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

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 通过 Cloud Shell 可以将 bashPowerShell 与 Azure 服务配合使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To launch Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。 Select Try It in the upper-right corner of a code block. 选择“试用” 不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell” 按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window
选择 Azure 门户右上方菜单栏中的“Cloud Shell” 按钮。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 启动 Cloud Shell。Launch Cloud Shell.

  2. 选择代码块上的“复制”按钮 以复制代码。Select the Copy button on a code block to copy the code.

  3. 在 Windows 和 Linux 上使用 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上使用 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.

  4. Enter 运行此代码。Press Enter to run the code.

自动凭据配置Automatic credential configuration

登录 Cloud Shell 后,Ansible 将通过 Azure 进行身份验证以管理基础架构,无需任何其他配置。When signed into the Cloud Shell, Ansible authenticates with Azure to manage infrastructure without any additional configuration.

使用多个订阅时,请通过导出 AZURE_SUBSCRIPTION_ID 环境变量来指定 Ansible 使用的订阅。When working with multiple subscriptions, specify the subscription Ansible uses by exporting the AZURE_SUBSCRIPTION_ID environment variable.

要列出所有 Azure 订阅,请运行以下命令:To list all of your Azure subscriptions, run the following command:

az account list

按如下所示使用 Azure 订阅 ID 设置 AZURE_SUBSCRIPTION_IDUsing your Azure subscription ID, set the AZURE_SUBSCRIPTION_ID as follows:

export AZURE_SUBSCRIPTION_ID=<your-subscription-id>

验证配置Verify the configuration

若要验证配置是否成功,请使用 Ansible 创建一个 Azure 资源组。To verify the successful configuration, use Ansible to create an Azure resource group.

  1. 在 Cloud Shell 中,创建名为 rg.yml 的文件。In Cloud Shell, create a file named rg.yml.

    code rg.yml
    
  2. 在编辑器中粘贴以下代码:Paste the following code into the editor:

    ---
    - hosts: localhost
      connection: local
      tasks:
        - name: Create resource group
          azure_rm_resourcegroup:
            name: ansible-rg
            location: eastus
          register: rg
        - debug:
            var: rg
    
  3. 保存文件并退出编辑器。Save the file and exit the editor.

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

    ansible-playbook rg.yml
    

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

PLAY [localhost] *********************************************************************************

TASK [Gathering Facts] ***************************************************************************
ok: [localhost]

TASK [Create resource group] *********************************************************************
changed: [localhost]

TASK [debug] *************************************************************************************
ok: [localhost] => {
    "rg": {
        "changed": true,
        "contains_resources": false,
        "failed": false,
        "state": {
            "id": "/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/ansible-rg",
            "location": "eastus",
            "name": "ansible-rg",
            "provisioning_state": "Succeeded",
            "tags": null
        }
    }
}

PLAY RECAP ***************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0

后续步骤Next steps