入门:在 Azure VM 上配置 Ansible

本文介绍如何在 Azure 中的 Ubuntu VM 上安装 Ansible

在这篇文章中,你将学会如何:

  • 创建资源组
  • 创建 Ubuntu 虚拟机
  • 在虚拟机上安装 Ansible
  • 通过 SSH 连接到虚拟机
  • 在虚拟机上配置 Ansible

先决条件

  • Azure 订阅:如果没有 Azure 订阅,请在开始之前创建 一个免费帐户
  • Azure 服务主体创建服务主体,记下以下值: appIddisplayName密码租户

创建虚拟机

  1. 创建 Azure 资源组。

    az group create --name QuickstartAnsible-rg --location eastus
    

    可能需要将 --location 参数替换为适合你的环境的值。

  2. 为 Ansible 创建 Azure 虚拟机。

    az vm create \
    --resource-group QuickstartAnsible-rg \
    --name QuickstartAnsible-vm \
    --image Ubuntu2204 \
    --admin-username azureuser \
    --admin-password <password>
    

    <password>替换为您的密码。

  3. 获取 Azure 虚拟机的公共 IP 地址。

    az vm show -d -g QuickstartAnsible-rg -n QuickstartAnsible-vm --query publicIps -o tsv
    

通过 SSH 连接到虚拟机

使用 SSH 命令连接到虚拟机的公共 IP 地址。

ssh azureuser@<vm_ip_address>

将该值 <vm_ip_address> 替换为前面命令中返回的相应值。

在虚拟机上安装 Ansible

Ansible 配合 azure.azcollection 使用

运行以下命令,在 Ubuntu 上配置 Ansible:

#!/bin/bash

sudo apt update

sudo apt install software-properties-common

sudo add-apt-repository --yes --update ppa:ansible/ansible

sudo apt install ansible

# Install Ansible az collection for interacting with Azure. (optional)
ansible-galaxy collection install azure.azcollection --force 

# Install Ansible modules for Azure (optional)
sudo pip3 install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements.txt

要点

  • Ansible 控制节点需要安装 Python 2(版本 2.7)或 Python 3(版本 3.5 及更高版本)。 Ansible 4.0.0 和 ansible-core 2.11 对 Python 3.8 具有软依赖关系,但也可兼容较低版本。 但是,Ansible 5.0.0 和 ansible-core 2.12 需要 3.8 及更新版。

创建 Azure 凭据

若要配置 Ansible 凭据,需要以下信息:

  • 您的 Azure 订阅 ID 和租户 ID
  • 服务主体应用程序 ID 和密钥

使用以下技术之一配置 Ansible 凭据:

选项 1:创建 Ansible 凭据文件

在本部分中,将创建一个本地凭据文件,以便向 Ansible 提供凭据。 出于安全原因,凭据文件只应在开发环境中使用。

有关定义 Ansible 凭据的详细信息,请参阅 向 Azure 模块提供凭据

  1. 成功连接到主机虚拟机后,创建并打开名为 credentials

    mkdir ~/.azure
    vi ~/.azure/credentials
    
  2. 将以下行插入文件中。 将占位符替换为服务主体值。

    [default]
    subscription_id=<subscription_id>
    client_id=<service_principal_app_id>
    secret=<service_principal_password>
    tenant=<service_principal_tenant_id>
    
  3. 保存并关闭该文件。

选项 2:定义 Ansible 环境变量

在主机虚拟机上,导出服务主体值以配置 Ansible 凭据。

export AZURE_SUBSCRIPTION_ID=<subscription_id>
export AZURE_CLIENT_ID=<service_principal_app_id>
export AZURE_SECRET=<service_principal_password>
export AZURE_TENANT=<service_principal_tenant_id>

测试 Ansible 安装

现在已安装并配置了 Ansible 的虚拟机!

本部分介绍如何在新 Ansible 配置中创建测试资源组。 如果不需要执行此操作,可以跳过该部分。

选项 1:使用临时 ansible 命令

运行以下临时 Ansible 命令以创建资源组:


#Ansible with azure.azcollection
ansible localhost -m azure.azcollection.azure_rm_resourcegroup -a "name=<resource_group_name> location=<location>"

<resource_group_name><location> 替换为自定义值。

选项 2:编写并运行 “Ansible playbook”

  1. 将以下代码保存为 create_rg.yml.

    使用 azure.azcollection 的 Ansible

    - hosts: localhost
      connection: local
      collections:
        - azure.azcollection
      tasks:
        - name: Creating resource group
          azure_rm_resourcegroup:
            name: "<resource_group_name"
            location: "<location>"
    

    <resource_group_name><location> 替换为自定义值。

  2. 使用 ansible-playbook 运行 playbook。

    ansible-playbook create_rg.yml
    

详细了解 azure.azcollection

清理资源

  1. 将以下代码保存为 delete_rg.yml.

    ---
    - hosts: localhost
      tasks:
        - name: Deleting resource group - "{{ name }}"
          azure_rm_resourcegroup:
            name: "{{ name }}"
            state: absent
          register: rg
        - debug:
            var: rg
    
  2. 使用 ansible-playbook 命令来运行 playbook。 将占位符替换为要删除的资源组的名称。 资源组中的所有资源都将被删除。

    ansible-playbook delete_rg.yml --extra-vars "name=<resource_group>"
    

    要点

    • 因为 playbook 中的register变量和debug部分,命令完成后会显示结果。

后续步骤