有没有办法在 Ansible 中创建条件语句,在读取字符串完成后结束进程?
Is there a way to create a conditional statement in Ansible that will end the process when reading a string has finished?
我创建了一个 ansible-playbook,旨在在 Active Directory 上创建一个组。但是,我在将新组放入活动目录时遇到了问题。这是因为属于community.windows.win_domain_group
的路径参数将域划分为CN、OU和DC。例如,我想创建一个名为“WindowsUser”的新组,它被放置在域控制器“test.active.dir”和通用名称=“Users”中,如下所示:
然后,这是我创建的名为 creategroup.yaml
的 ansible-playbook:
---
- hosts: brc.testlab.com
gather_facts: no
tasks:
- name: "Create Group"
community.windows.win_domain_group:
name: "{{group}}"
scope: global
path: "CN=Users, DC={{(domain).split('.')[0]}}, DC={{(domain).split('.')[1]}}, DC={{(domain).split('.')[2]}}"
剧本是 运行 使用以下命令:
ansible-playbook -i hosts creategroup.yaml -e group=windowsUser -e domain=test.active.dir
基于现有的 ansible 剧本,我设法将 'WindowsUser' 组放入 CN = Users,DC = test,DC = active,DC = dir。但是,如果有一个域控制器的划分多于/少于 3 个 DC(例如 domain = msg.test.active.dir
或 domain = active.dir
),那么上面的 ansible playbook 将失败。有没有办法创建一个条件语句,当字符串域的读取结束时,一定会停止将字符串放在 DC 上的过程?我尝试将以下命令添加到 yaml 但结果仍然失败:
---
- hosts: brc.testlab.com
gather_facts: no
tasks:
- name: "Create Group"
community.windows.win_domain_group:
name: "{{group}}"
scope: global
path: "CN=Users, DC={{(domain).split('.')[0]}}, DC={{(domain).split('.')[1]}}, DC={{(domain).split('.')[2]}}, DC={{(domain).split('.')[3]|default()}}"
结果如下:
任何答案都会很有帮助,谢谢。
由于domain
中的分区可以不同,最好将分区存储在一个变量中,然后在path
中指定时加入它们。
我正在使用 debug
消息来显示输出,但您可以将其指定到 win_domain_group
模块的 path
。
示例:
vars:
group: WindowsUser
domain: msg.test.active.dir
tasks:
- name: split the domain and save as variable
set_fact:
domain_split: "{{ domain.split('.') }}"
- name: show group in domain
debug:
msg: "CN={{ group }},DC={{ domain_split|join(',DC=') }}"
现在 CN={{ group }}
将以域名中的分区数作为后缀。所以它适用于 test.active.dir
和 active.dir
。对于上面的示例,它将是:
"msg": "CN=WindowsUser,DC=msg,DC=test,DC=active,DC=dir"
我创建了一个 ansible-playbook,旨在在 Active Directory 上创建一个组。但是,我在将新组放入活动目录时遇到了问题。这是因为属于community.windows.win_domain_group
的路径参数将域划分为CN、OU和DC。例如,我想创建一个名为“WindowsUser”的新组,它被放置在域控制器“test.active.dir”和通用名称=“Users”中,如下所示:
然后,这是我创建的名为 creategroup.yaml
的 ansible-playbook:
---
- hosts: brc.testlab.com
gather_facts: no
tasks:
- name: "Create Group"
community.windows.win_domain_group:
name: "{{group}}"
scope: global
path: "CN=Users, DC={{(domain).split('.')[0]}}, DC={{(domain).split('.')[1]}}, DC={{(domain).split('.')[2]}}"
剧本是 运行 使用以下命令:
ansible-playbook -i hosts creategroup.yaml -e group=windowsUser -e domain=test.active.dir
基于现有的 ansible 剧本,我设法将 'WindowsUser' 组放入 CN = Users,DC = test,DC = active,DC = dir。但是,如果有一个域控制器的划分多于/少于 3 个 DC(例如 domain = msg.test.active.dir
或 domain = active.dir
),那么上面的 ansible playbook 将失败。有没有办法创建一个条件语句,当字符串域的读取结束时,一定会停止将字符串放在 DC 上的过程?我尝试将以下命令添加到 yaml 但结果仍然失败:
---
- hosts: brc.testlab.com
gather_facts: no
tasks:
- name: "Create Group"
community.windows.win_domain_group:
name: "{{group}}"
scope: global
path: "CN=Users, DC={{(domain).split('.')[0]}}, DC={{(domain).split('.')[1]}}, DC={{(domain).split('.')[2]}}, DC={{(domain).split('.')[3]|default()}}"
结果如下:
任何答案都会很有帮助,谢谢。
由于domain
中的分区可以不同,最好将分区存储在一个变量中,然后在path
中指定时加入它们。
我正在使用 debug
消息来显示输出,但您可以将其指定到 win_domain_group
模块的 path
。
示例:
vars:
group: WindowsUser
domain: msg.test.active.dir
tasks:
- name: split the domain and save as variable
set_fact:
domain_split: "{{ domain.split('.') }}"
- name: show group in domain
debug:
msg: "CN={{ group }},DC={{ domain_split|join(',DC=') }}"
现在 CN={{ group }}
将以域名中的分区数作为后缀。所以它适用于 test.active.dir
和 active.dir
。对于上面的示例,它将是:
"msg": "CN=WindowsUser,DC=msg,DC=test,DC=active,DC=dir"