0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AnsibleでActiveDiretoryのユーザー、セキュリティグループを作成

Posted at

概要

検証の都合で、Active Directoryに複数のユーザー、セキュリティグループを登録する必要がありました。
Ansibleを勉強中だったこともあり、Ansibleで実装してみました。

ユーザー、セキュリティグループの情報はCSVで定義しておき、playbookからはCSVを読み込んでloop処理で回す動きです。

CSVサンプル(抜粋)

※値をマスクした関係で、正しくない設定になっている部分があります。
※ユーザー登録用のCSVです。

uid,name,sAMAccountName,UserPrincipalName,Path,Company,Surname,GivenName,DisplayName,mail,password,group1,group2
XX0000,名前,XX0000,XX0000@local.com,"OU=Dev_XX,DC=local,DC=com",XX,サンプル,サンプル,サンプル,XX0000@local.com,initialpassword,"CN=GitLab,OU=Dev_XX,DC=local,DC=com",
XX0001,名前,XX0001,XX0001@local.com,"OU=Dev_XX,DC=local,DC=com",XX,サンプル,サンプル,サンプル,XX0001@local.com,initialpassword,"CN=GitLab_Dev_XX,OU=Dev_XX,DC=local,DC=com",
XX0002,名前,XX0002,XX0002@local.com,"OU=Dev_XX,DC=local,DC=com",XX,サンプル,サンプル,サンプル,XX0002@local.com,initialpassword,"CN=GitLab_Lib_XX,OU=Dev_XX,DC=local,DC=com",
XX0003,名前,XX0003,XX0003@local.com,"OU=Dev_XX,DC=local,DC=com",XX,サンプル,サンプル,サンプル,XX0003@local.com,initialpassword,"CN=GitLab_Dev_XX,OU=Dev_XX,DC=local,DC=com",
XX0004,名前,XX0004,XX0004@local.com,"OU=Dev_XX,DC=local,DC=com",XX,サンプル,サンプル,サンプル,XX0004@local.com,initialpassword,"CN=GitLab_Dev_XX,OU=Dev_XX,DC=local,DC=com","CN=GitLab_Dev_XX,OU=Dev_XX,DC=local,DC=com"
XX0005,名前,XX0005,XX0005@local.com,"OU=Dev_XX,DC=local,DC=com",XX,サンプル,サンプル,サンプル,XX0005@local.com,initialpassword,"CN=GitLab_Dev_XX,OU=Dev_XX,DC=local,DC=com","CN=GitLab_Lib_XX,OU=Dev_XX,DC=local,DC=com"

ユーザー登録用playbookサンプル

- name: create user from csv
  hosts: AD
  tasks:
  - name: read csv
    community.general.read_csv: 
      path: files/userList.csv
    register: users_list
    delegate_to: localhost  

  - name: create users
    microsoft.ad.user:
      identity: "{{ item.uid }}"
      name: "{{ item.name }}"
      firstname: "{{ item.GivenName }}"
      surname: "{{ item.Surname }}"
      password: "{{ item.password }}"
      password_never_expires: true
      display_name: "{{ item.DisplayName }}"
      email: "{{ item.mail }}"
      company: "{{ item.Company }}"
      upn: "{{ item.UserPrincipalName }}"
      sam_account_name: "{{ item.sAMAccountName }}"
      path: "{{ item.Path }}"
      groups:
        set:
          - Domain Users
          - "{{ item.group1 }}"
          - "{{ item.group2 }}"
      state: present
    loop: "{{ users_list.list }}"

グループ作成用のplaybookサンプル

- name: create group from csv
  hosts: AD
  tasks:
  - name: read csv
    community.general.read_csv: 
      path: files/groupList.csv
    register: group_list
    delegate_to: localhost  

  - name: createt groups
    microsoft.ad.group:
      name: "{{ item.name }}"
      path: "{{ item.Path }}"
      scope: global
      state: present
    loop: "{{ group_list.list }}"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?