빠른 시작: Ansible에 대한 Azure 서비스 주체 만들기

이 빠른 시작에서는 AzureCLI 또는 Azure PowerShell을 사용하여 Azure 서비스 주체를 만들고 Ansible에서 Azure에 인증합니다.

이 문서에서는 다음 방법을 설명합니다.

  • Azure CLI를 사용하여 Azure 서비스 주체 만들기
  • Azure PowerShell을 사용하여 Azure 서비스 주체 만들기
  • Azure 서비스 주체에 역할 할당
  • 서비스 주체에서 키 정보 가져오기
  • Ansible이 서비스 주체 값을 검색할 수 있도록 환경 변수 설정
  • 서비스 주체를 테스트합니다.

필수 조건

  • Azure 구독: Azure 구독이 아직 없는 경우 시작하기 전에 체험 계정을 만듭니다.

Azure 서비스 주체 만들기

Azure 서비스 주체는 Ansible을 사용하여 Azure 리소스를 관리하는 전용 계정을 제공합니다.

다음 코드를 실행하여 Azure 서비스 주체를 만듭니다.

az ad sp create-for-rbac --name ansible \
            --role Contributor \
            --scopes /subscriptions/<subscription_id>

참고 항목

출력의 암호를 안전한 위치에 저장합니다.

Azure 서비스 주체에 역할 할당

기본적으로 서비스 주체는 Azure에서 리소스를 관리하는 데 필요한 액세스 권한이 없습니다.

다음 명령을 실행하여 서비스 주체에 기여자 역할을 할당합니다.

az role assignment create --assignee <appID> \
    --role Contributor \
    --scope /subscriptions/<subscription_id>

<appID>az ad sp create-for-rba 명령의 출력에서 제공된 값으로 바꿉니다.

참고 항목

보안을 강화하려면 구독 대신 리소스 그룹에 대한 역할 할당의 범위를 변경합니다.

Azure 서비스 주체 정보 가져오기

서비스 주체를 사용하여 Azure에 인증하려면 다음 정보가 필요합니다.

  • SubscriptionID
  • 서비스 주체 ApplicationId
  • 서비스 주체 암호
  • TenantID

다음 명령을 실행하여 서비스 주체 정보를 가져옵니다.

az account show --query '{tenantId:tenantId,subscriptionid:id}';

az ad sp list --display-name ansible --query '{clientId:[0].appId}'

서비스 주체를 사용하여 Azure에 인증

다음 명령을 실행하여 Ansible 서버에서 필요한 환경 변수를 채웁니다.

export AZURE_SUBSCRIPTION_ID=<SubscriptionID>
export AZURE_CLIENT_ID=<ApplicationId>
export AZURE_SECRET=<Password>
export AZURE_TENANT=<TenantID>

, <ApplicationId><Password><TenantID> 서비스 주체 계정의 값으로 바꿉<SubscriptionID>다.

서비스 주체 권한 테스트

다음 명령을 실행하여 새 Azure 리소스 그룹을 만듭니다.

ansible localhost -m azure_rm_resourcegroup -a "name=<resource_group_name> location=<resource_group_location>"

새 리소스 그룹 값으로 대체 <resource_group_name> 합니다 <resource_group_location> .

[WARNING]: No inventory was parsed, only implicit localhost is available
localhost | CHANGED => {
    "changed": true,
    "contains_resources": false,
    "state": {
        "id": "/subscriptions/<subscriptionID>/resourceGroups/azcli-test",
        "location": "eastus",
        "name": "azcli-test",
        "provisioning_state": "Succeeded",
        "tags": null
    }
}

다음 명령을 실행하여 Azure 리소스 그룹을 삭제합니다.

ansible localhost -m azure_rm_resourcegroup -a "name=<resource_group_name> state=absent force_delete_nonempty=yes"

리소스 그룹의 이름으로 바꿉 <resource_group_name> 니다.

[WARNING]: No inventory was parsed, only implicit localhost is available
localhost | CHANGED => {
    "changed": true,
    "contains_resources": false,
    "state": {
        "id": "/subscriptions/subscriptionID>/resourceGroups/azcli-test",
        "location": "eastus",
        "name": "azcli-test",
        "provisioning_state": "Succeeded",
        "status": "Deleted",
        "tags": null
    }
}

다음 단계