Terraform Provider for Windows Active Directory (AD)

repository·main·Indexed 41 days ago

https://github.com/hashicorp/terraform-provider-ad

A community-supported Terraform provider in Technical Preview for managing Windows Active Directory resources, including users, groups, computer objects, and Group Policy Objects (GPOs). It supports WinRM connections with Basic, NTLM, and Kerberos authentication, as well as local execution on Windows. Requires Windows Server 2012R2 or greater and Terraform 0.12.x or greater.

Tokens
12.4K
Snippets
38
Records
55
Agent score
100%

What's inside Terraform Provider AD

  1. Overview of the Terraform Provider for Windows Active Directory (AD)

    main

    The terraform-provider-ad allows you to manage Windows Active Directory objects, specifically users, groups, and group policies, using Terraform configuration.

    Warning: Technical Preview This provider is currently in a Technical Preview status. It is a community-supported project that is still in development and is NOT INTENDED FOR PRODUCTION USE. It may contain bugs, undergo significant changes, and is provided on an "as-is" basis without official HashiCorp support.

  2. Configure Kerberos Authentication

    main

    Starting with version 0.4.0, the provider supports Kerberos authentication for WinRM connections. The provider automatically switches to Kerberos authentication when the krb_realm setting is provided.

    If no krb_conf file is supplied, the provider uses a default configuration derived from krb_realm and winrm_hostname.

    provider "ad" {
      winrm_hostname = var.hostname
      winrm_username = var.username
      winrm_password = var.password
      krb_realm      = "YOURDOMAIN.COM"
    }
  3. Getting started with the Windows AD provider

    main

    To begin using the provider in your Terraform projects:

    1. Download the provider: Obtain the latest build from the Terraform Registry.
    2. Configure your project: Write your Terraform configuration using the AD provider resources.
    3. Initialize: Run terraform init in your Terraform configuration directory. This allows Terraform to detect and install the provider plugin.
    4. Explore documentation: Review the docs/ folder in the repository for specific configuration options and the examples/ folder for implementation patterns.
    # Run this in your Terraform configuration directory
    terraform init
  4. Manage Active Directory groups with ad_group

    main

    The ad_group resource allows you to manage Group objects within an Active Directory tree. You can define the group's name, SAM account name, scope, category, and its location within a container (such as an OU).

    variable name { default = "TestOU" }
    variable path { default = "dc=yourdomain,dc=com" }
    variable description { default = "some description" }
    variable protected { default = false }
    
    variable name { default = "test group" }
    variable sam_account_name { default = "TESTGROUP" }
    variable scope { default = "global" }
    variable category { default = "security" }
    
    resource "ad_ou" "o" {
      name        = var.name
      path        = var.path
      description = var.description
      protected   = var.protected
    }
    
    
    resource "ad_group" "g" {
      name             = var.name
      sam_account_name = var.sam_account_name
      scope            = var.scope
      category         = var.category
      container        = ad_ou.o.dn
    }
  5. Import an existing ad_group_membership resource

    main

    To import an existing group membership into Terraform, use the following ID format: {group_uuid}_{random_uuid}. The group UUID and the random UUID are joined by an underscore.

    # Example import using the group's UUID and a random UUID joined by an underscore
    $ terraform import ad_group_membership 9CB8219C-31FF-4A85-A7A3-9BCBB6A41D02_E9079B50-95C5-4101-8400-E01CC83CF53B
  6. Configure Double Hop Authentication

    main

    Starting with version 0.4.3, you can point the provider to a management host that is not a Domain Controller. The provider then performs management tasks through that host.

    Requirements for Double Hop:

    • Must use winrm_proto = "https".
    • Authentication between the management host and the DC is performed via Kerberos.
    • The AD PowerShell module and the Group Policy PowerShell Module must be installed on the management server.

    Example configuration for a management host:

    provider "ad" {
      winrm_hostname         = "10.0.0.1"
      winrm_username         = var.username
      winrm_password         = var.password
      krb_realm              = "YOURDOMAIN.COM"
      krb_conf               = "${path.module}/krb5.conf"
      krb_spn                = "winserver1"
      winrm_port             = 5986
      winrm_proto            = "https"
      winrm_pass_credentials = true
    }
  7. Execute AD commands locally (Windows only)

    main

    If the operating system running Terraform is Windows, you can execute commands locally instead of over WinRM. To enable local execution, you must set winrm_hostname, winrm_username, and winrm_password to null (or empty strings) in the provider configuration.

    provider "ad" {
      winrm_hostname = ""
      winrm_username = ""
      winrm_password = ""
    }