This has been a challenge and I am stuck on what else to try. Basically, I have a text file that has an unknown amount of lines. I wish to read through each line and set facts for each line that is then used as variables for a win_domain_user module.
If there is only one line in the text file, then I can accomplish my task with no issues or errors. But, if there is more than one line, I do not know how to start the process again with the next line.
For example:
- name: "Pre-task AD | Get user account info"
win_shell: |
Get-Content C:\AD\user_info.txt
register: 'read_output'
The line of text in the user_info.txt file looks like this:
first-John;last-Doe;display_name-Doe, John (US);title-Engineer;employee_type-Employee;user-john.doe;email-john.doe@company.com;customer-Some_Business;dept_groups-Test_Group
Example of two lines:
first-John;last-Doe;display_name-Doe, John (US);title-Engineer;employee_type-Employee;user-john.doe;email-john.doe@company.com;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;email-jane.doe@company.com;customer-Some_Business;dept_groups-Test_Group
The set facts looks like:
- 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(...) }}"
The winrm Active Directory related module looks like:
- 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'
I tried using a win_shell ForEach (PowerShell) loop to action the lines in C:\AD\user_info.txt but I was not able to figure out how to use set_facts within the ForEach loop and use the win_domain_user module within it. So, instead, I looked into using with_items which from what I understand is now loop. From the info above, I was going to create 2 yml files, one that has the 'Get-Content C:\AD\user_info.txt' / register and then use the include_tasks yml with block for the tasks to run against the data from the Get-Content.
The tricky part is that I do not know how many lines are going to be in that user_info.txt file. Am I on the right track as far as using two files for this or is there a more efficient way?