[英]Ansible - Adding multiple AD users to AD group
我一直在 Internet 上尋找我的一個劇本的解決方案,但我相信我可能會增加更多的困惑,而不是找出解決方案。 我希望這里的人可以讓我回到正確的道路上,而不是進一步進入雜草。
以最簡單的形式,我的劇本需要創建一個 AD 組,然后根據從請求表單中接收到的變量將成員添加到新創建的 AD 組,這似乎並不難……直到我想添加超過一個或五個來自另一個變量。 變量req_ad_user_name
始終需要是組的成員。 var req_ad_user_others
是表單上的可選字段。 因此,根據每個獨特的請求,它可能有一堆用戶名或沒有用戶名。
test_playbook.yml
---
- hosts: dc
vars:
domain_name: "{{ domain_override | default ('tst.local') }}"
ou_path: "{{ ou_override | default('OU=IT,DC=tst,DC=local') }}"
dc_name: "{{ dc_override | default ('dc1') }}"
ad_group_name: "SOME_TEST_GROUP_RW"
req_ad_user_name: "tst_user"
req_ad_user_others: "tst_user2,tst_user3"
tasks:
- name: "Create AD Group for Internal Path"
win_domain_group:
name: "{{ ad_group_name }}"
description: "Testing for internalpath"
domain_server: "{{ dc_name }}"
organizational_unit: "{{ ou_path }}"
scope: global
attributes:
info: "Testing comments for tasknumber"
- name: "Add Members to new group"
win_domain_group_membership:
name: "{{ ad_group_name }}"
domain_server: "{{ dc_name }}"
members:
- "{{ req_ad_user_name }}"
state: present
...
我最初的想法是從req_ad_user_name
創建一個列表,如果不是 null, req_ad_user_others
。 然后,遍歷“win_domain_group_membership”模塊中的成員列表,將它們添加到組中。 但是,我無法弄清楚在不同可能的情況下它的邏輯是什么。 任何幫助將非常感激。
Jinja 模板可以為您提供您想要實現的一切。
以下是分解步驟:
req_ad_user_name
變量中列出:- debug:
msg: "{{ [req_ad_user_name] }}"
split
您的變量req_ad_user_others
,
:- debug:
msg: "{{ req_ad_user_others.split(',') }}"
- debug:
msg: "{{ [req_ad_user_name] + req_ad_user_others.split(',') }}"
req_ad_user_others
可能是空字符串( count
'ing 0
個字符)的可能性,在這種情況下,我們應該只使用一個僅包含req_ad_user_name
的列表- debug:
msg: "{{ ([req_ad_user_name] + req_ad_user_others.split(',')) if req_ad_user_others | count > 0 else [req_ad_user_name] }}"
由於這總是會返回一個列表,因此您應該安全地將其直接提供給win_domain_group_membership
的members
屬性,該屬性確實希望將列表作為參數。
- name: "Add Members to new group"
win_domain_group_membership:
name: "{{ ad_group_name }}"
domain_server: "{{ dc_name }}"
members: "{{ ([req_ad_user_name] + req_ad_user_others.split(',')) if req_ad_user_others | count > 0 else [req_ad_user_name] }}"
state: present
注意:我手下沒有 Active Directory 來測試此代碼,但我非常有信心它應該可以解決問題。
至於關於變量操作的演示,這里是:
---
- hosts: localhost
connection: local
vars:
req_ad_user_name: "tst_user"
req_ad_user_others: "tst_user2,tst_user3"
req_ad_user_others_empty: ""
tasks:
- name: Test with req_ad_user_others, containing two users tst_user2, tst_user3
debug:
msg: "{{ ([req_ad_user_name] + req_ad_user_others.split(',')) if req_ad_user_others | count > 0 else [req_ad_user_name] }}"
- name: Test with req_ad_user_others_emtpy, an empty string
debug:
msg: "{{ ([req_ad_user_name] + req_ad_user_others_empty.split(',')) if req_ad_user_others_empty | count > 0 else [req_ad_user_name] }}"
這輸出
PLAY [localhost] ************************************************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [localhost]
TASK [Test with req_ad_user_others, containing two users tst_user2, tst_user3] **********************************************************************************************
ok: [localhost] => {
"msg": [
"tst_user",
"tst_user2",
"tst_user3"
]
}
TASK [Test with req_ad_user_others_emtpy, an empty string] ******************************************************************************************************************
ok: [localhost] => {
"msg": [
"tst_user"
]
}
PLAY RECAP ******************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.