windows - 如何正确循环遍历 Ansible 中 block 内任务的文件?

标签 windows powershell loops foreach ansible

这是一个挑战,我不知道还能尝试什么。基本上,我有一个文本文件,其行数未知。我希望通读每一行并为每一行设置事实,然后将其用作 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/

相关文章:

Linux Bash Shell 读取日志文件将每一行与文件的重置进行比较

python - 为什么当我导入 pynput 并使用它时它会删除我的 python 文件?

powershell - 使用 Invoke-Command 时 Try & Catch 不起作用

powershell - 如何在 powershell 中使用 certreq 显示新创建证书的证书缩略图?

windows - 如何在不显示窗口的情况下运行 PowerShell 脚本?

javascript - 删除除首先使用纯 JS 之外的所有类

mysql - 如何使用包含子查询的 mysql 查询编写循环 bash 脚本?

Javac 无法在 cmd windows 10 中工作

windows - 通过网络运行的程序的管理员权限提升

python - 如何在 Windows 上使用 Popen 隐藏控制台?