diff --git a/apps/accounts/automations/gather_account/filter.py b/apps/accounts/automations/gather_account/filter.py index e2aafd630..4b6a52562 100644 --- a/apps/accounts/automations/gather_account/filter.py +++ b/apps/accounts/automations/gather_account/filter.py @@ -1,8 +1,21 @@ from django.utils import timezone +from datetime import datetime __all__ = ['GatherAccountsFilter'] +def parse_date(date_str, default=''): + if not date_str: + return default + if date_str == 'Never': + return None + try: + dt = datetime.strptime(date_str, '%Y/%m/%d %H:%M:%S') + return timezone.make_aware(dt, timezone.get_current_timezone()) + except ValueError: + return default + + # TODO 后期会挪到 playbook 中 class GatherAccountsFilter: def __init__(self, tp): @@ -101,11 +114,26 @@ class GatherAccountsFilter: @staticmethod def windows_filter(info): - info = info[4:-2] result = {} - for i in info: - for username in i.split(): - result[username] = {} + for user_details in info['user_details']: + user_info = {} + lines = user_details['stdout_lines'] + for line in lines: + if not line.strip(): + continue + parts = line.split(' ', 1) + if len(parts) == 2: + key, value = parts + user_info[key.strip()] = value.strip() + user = { + 'username': user_info.get('User name', ''), + 'groups': user_info.get('Global Group memberships', ''), + 'date_password_change': parse_date(user_info.get('Password last set', '')), + 'date_password_expired': parse_date(user_info.get('Password expires', '')), + 'date_last_login': parse_date(user_info.get('Last logon', '')), + 'can_change_password': user_info.get('User may change password', 'Yes') + } + result[user['username']] = user return result def run(self, method_id_meta_mapper, info): diff --git a/apps/accounts/automations/gather_account/host/windows/main.yml b/apps/accounts/automations/gather_account/host/windows/main.yml index 944ae142f..6d545701a 100644 --- a/apps/accounts/automations/gather_account/host/windows/main.yml +++ b/apps/accounts/automations/gather_account/host/windows/main.yml @@ -1,14 +1,32 @@ - hosts: demo gather_facts: no tasks: - - name: Gather windows account - ansible.builtin.win_shell: net user - register: result - ignore_errors: true + - name: Run net user command to get all users + win_shell: net user + register: user_list_output - - name: Define info by set_fact - ansible.builtin.set_fact: - info: "{{ result.stdout_lines }}" + - name: Parse all users from net user command + set_fact: + all_users: >- + {%- set users = [] -%} + {%- for line in user_list_output.stdout_lines -%} + {%- if loop.index > 4 and line.strip() != "" and not line.startswith("The command completed") -%} + {%- for user in line.split() -%} + {%- set _ = users.append(user) -%} + {%- endfor -%} + {%- endif -%} + {%- endfor -%} + {{ users }} + + - name: Run net user command for each user to get details + win_shell: net user {{ item }} + loop: "{{ all_users }}" + register: user_details + ignore_errors: yes + + - set_fact: + info: + user_details: "{{ user_details.results }}" - debug: var: info