Ansible でADサーバー構築

ActiveDirectory

こんにちは。サクです。
今回は、Ansibleを使ってADサーバーを構築したのでその手順をまとめます。

バージョン

  • Ansibleサーバー
    • Ubuntu:22.04.5 LTS
    • ansible core:2.17.14
  • ADサーバー
    • Windows Server 2022
    • ドメイン/フォレストの機能レベル:Windows Server 2016

ディレクトリ構造

個人的にここの正解がわからない….
tasksの中は、細分化していきたいところ
ゆくゆくは、Hyper-Vの作成をAnsibleでやりたいところ


|-- group_vars
|   `-- activedirectory.yaml
|-- inventory.yaml
|-- roles
|   |-- win_ad
|   |   `-- tasks
|   |       |-- main.yaml
|   |       |-- main.yaml.save
|   |       `-- records.yaml
|   |-- win_common
|   `-- win_hyper-v
`-- site.yaml

inventory.yaml

Windows Serverに事前にOpenSSHをインストールしています。
Windows ServerにはSSHで接続するような形にしています。

---
windows:
  children:
    activedirectory:
      hosts:
        ad01:
          ansible_host: 192.168.200.230
        ad02:
          ansible_host: 192.168.200.231
      vars:
        ansible_connection: ssh
        ansible_password:  Passw0rd!
        ansible_user: Administrator
        ansible_shell_type: powershell

site.yaml

# site.yml
- hosts: windows
  roles:
    - win_common

- hosts: activedirectory
  roles:
    - win_ad

- hosts: hyperv
  roles:
    - win_hyper-v

Active Directory Domain Serviceの設定

  1. ADDSの役割のインストール
  2. ドメイン・フォレストの作成
  3. FSMOの持つADのNTPサーバの設定
  4. 2台目のドメインコントローラーのドメイン参加
  5. 2台目のドメインコントローラーへの昇格
  6. セントラルストアの作成。
# Install ADDS

- name: Install ADDS
  win_feature:
    name: AD-Domain-Services
    state: present
    include_management_tools: yes
    register: install_ad

# create Forest/Domain
- name: Create new domain in a new forest on the target host and reboot
  microsoft.ad.domain:
    dns_domain_name: "{{ domain_name }}"
    safe_mode_password: "{{ safe_mode_password }}"
    reboot: true
    reboot_timeout: 180
  when: inventory_hostname == "ad01"
  register: create_domain

# AD01 Setting NTP
- name: Setting NTP
  win_shell: w32tm /config /syncfromflags:manual /manualpeerlist:ntp.nict.jp /update
  when: inventory_hostname == "ad01"


# adding ad server

- name: Domain add New ad server
  microsoft.ad.membership: 
    dns_domain_name: "{{ domain_name }}"
    domain_admin_user: "{{ domain_admin_user }}@{{ domain_name }}"
    domain_admin_password: "{{ domain_admin_password }}"
    state: domain
    reboot: true
    reboot_timeout: 180
  when: inventory_hostname != 'ad01'
  register: join_domain
  tags: join_domain

# promote domain controller

- name: Promote server as a domain controller
  microsoft.ad.domain_controller:
    dns_domain_name: "{{ domain_name }}"
    domain_admin_user: "{{ domain_admin_user }}@{{ domain_name }}"
    domain_admin_password: "{{ domain_admin_password }}"
    safe_mode_password: "{{ safe_mode_password }}"
    state: domain_controller
    reboot: true
  when: inventory_hostname == 'ad02'
# Create Central Store
- name: Create Central Store
  win_robocopy:
    src: C:\Windows\PolicyDefinitions
    dest: \\ドメイン名\sysvol\ドメイン名\Policies\PolicyDefinitions
  when: inventory_hostname == "ad01"

実行結果

次のコマンドを実行して、Ansibleを実行

ansible-playbook -i inventory.yaml site.yaml --limit activedirectory

コメント