这是一个挑战,我不知道还能尝试什么。基本上,我有一个文本文件,其行数未知。我希望通读每一行并为每一行设置事实,然后将其用作 win_domain_user 模块的变量。
如果文本文件中只有一行,那么我可以毫无问题或错误地完成任务。但是,如果有多于一行,我不知道如何从下一行再次开始该过程。
例如:
- name: "Pre-task AD | Get user account info"
win_shell: |
Get-Content C:\AD\user_info.txt
register: 'read_output'
user_info.txt 文件中的文本行如下所示:
first-John;last-Doe;display_name-Doe, John (US);title-Engineer;employee_type-Employee;user-john.doe;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b1e161a12175611141315551f141e3b1814160b1a150255181416" rel="noreferrer noopener nofollow">[email protected]</a>;customer-Some_Business;dept_groups-Test_Group
两行示例:
first-John;last-Doe;display_name-Doe, John (US);title-Engineer;employee_type-Employee;user-john.doe;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e58088848c89c88f8a8d8bcb818a80a5868a8895848b9ccb868a88" rel="noreferrer noopener nofollow">[email protected]</a>;customer-Some_Business;dept_groups-Test_Group
first-Jane;last-Doe;display_name-Doe, Jane (US);title-Engineer II;employee_type-Employee;user-jane.doe;<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96f3fbf7fffabbfcf7f8f3b8f2f9f3d6f5f9fbe6f7f8efb8f5f9fb" rel="noreferrer noopener nofollow">[email protected]</a>;customer-Some_Business;dept_groups-Test_Group
设定的事实如下所示:
- set_fact:
first: "{{ read_output.stdout | regex_search(...) }}"
last: "{{ read_output.stdout | regex_search(...) }}"
user: "{{ read_output.stdout | regex_search(...) }}"
email: "{{ read_output.stdout | regex_search(...) }}"
customer: "{{ read_output.stdout | regex_search(...) }}"
title: "{{ read_output.stdout regex_search(...) }}"
display_name: "{{ read_output.stdout regex_search(...) }}"
employee_type: "{{ read_output.stdout | regex_search(...) }}"
dept_groups: "{{ read_output.stdout | regex_search(...) }}"
winrm Active Directory 相关模块如下所示:
- name: "Active Directory | Add domain user account to domain controller"
win_domain_user:
firstname: "{{ first }}"
surname: "{{ last }}"
name: "{{ first }}{{ space }}{{ last }}"
description: "{{ title }}"
password: "{{ password_var_here}}"
upn: "{{ user }}{{ domain_fqdn_var_here }}"
email: "{{ email }}"
state: 'present'
path: "{{ ou_path_here }}"
groups: "Domain Users, {{ dept_groups }}"
attributes:
displayName: "{{ first }}{{ space }}{{ last }}"
domain_username: "{{ domainusername_var_here }}"
domain_password: "{{ domainpassword_var_here }}"
domain_server: "{{ dc_var_here }}"
register: 'add_ad_user'
我尝试使用 win_shell ForEach (PowerShell) 循环来操作 C:\AD\user_info.txt 中的行,但我无法弄清楚如何在 ForEach 循环中使用 set_facts 并在其中使用 win_domain_user 模块。因此,我研究了使用 with_items,据我所知,它现在是循环。根据上面的信息,我将创建 2 个 yml 文件,其中一个包含“Get-Content C:\AD\user_info.txt”/注册,然后使用 include_tasks yml 和 block 来让任务针对来自的数据运行获取内容。
棘手的部分是我不知道 user_info.txt 文件中有多少行。就使用两个文件而言我是否走在正确的轨道上,或者是否有更有效的方法?
最佳答案
文件中的每一行都将位于注册变量read_output.stdout_lines
中。我们可以循环使用此变量并使用分号 (;
) 分割行来获取用户的各个属性。
如果我们只取第一行,下面的示例将返回 John
:
"{{ read_output.stdout_lines[0].split(';')[0].split('-')[1] }}"
类似于调试
任务:
- name: "Pre-task AD | Get user account info"
win_shell: |
Get-Content C:\AD\user_info.txt
register: read_output
- name: Show user details after splitting
debug:
msg:
- "First: {{ first }}"
- "Last: {{ last }}"
- "Display name: {{ display_name }}"
vars:
first: "{{ item.split(';')[0].split('-')[1] }}"
last: "{{ item.split(';')[1].split('-')[1] }}"
display_name: "{{ item.split(';')[2].split('-')[1] }}"
loop: "{{ read_output.stdout_lines }}"
将产生:
"msg": [
"First: John",
"Last: Doe",
"Display name: Doe, John (US)"
]
继续这个主题,我们可以使用 win_domain_user
任务(未测试):
- name: "Active Directory | Add domain user account to domain controller"
win_domain_user:
firstname: "{{ first }}"
surname: "{{ last }}"
name: "{{ first }} {{ last }}"
description: "{{ title }}"
password: "{{ password_var_here }}"
upn: "{{ user }}{{ domain_fqdn_var_here }}"
email: "{{ email }}"
state: 'present'
path: "{{ ou_path_here }}"
groups: "Domain Users, {{ dept_groups }}"
attributes:
displayName: "{{ first }} {{ last }}"
domain_username: "{{ domainusername_var_here }}"
domain_password: "{{ domainpassword_var_here }}"
domain_server: "{{ dc_var_here }}"
vars:
first: "{{ item.split(';')[0].split('-')[1] }}"
last: "{{ item.split(';')[1].split('-')[1] }}"
display_name: "{{ item.split(';')[2].split('-')[1] }}"
name: "{{ first }} {{ last }}"
title: "{{ item.split(';')[3].split('-')[1] }}"
email: "{{ item.split(';')[6].split('-')[1] }}"
user: "{{ item.split(';')[5].split('-')[1] }}"
dept_groups: "{{ item.split(';')[8].split('-')[1] }}"
loop: "{{ read_output.stdout_lines }}"
关于windows - 如何正确循环遍历 Ansible 中 block 内任务的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68875921/