diff --git a/playbooks/install_software_win.yml b/playbooks/install_software_win.yml index b12c7b6..00cc381 100644 --- a/playbooks/install_software_win.yml +++ b/playbooks/install_software_win.yml @@ -1,69 +1,50 @@ ---- -- name: Prepare target host - hosts: localhost - gather_facts: no - tasks: - - name: Add target host from survey - ansible.builtin.add_host: - name: "{{ target_ip }}" - groups: dynamic_windows - ansible_connection: winrm - ansible_port: "{{ winrm_port | default(5985) }}" - ansible_winrm_transport: "{{ winrm_transport | default('ntlm') }}" - ansible_winrm_server_cert_validation: ignore - ansible_user: "{{ win_user }}" - ansible_password: "{{ win_pass }}" - - name: Install Software from SMB hosts: dynamic_windows gather_facts: no - collections: - - community.windows - - ansible.windows - vars: - drive_letter: "Z" - # target_ip, installer_file приходят из Survey - # smb_user, smb_pass, smb_path, win_user, win_pass приходят из Environment - installer_args: "/S" # можно переопределить в Survey + installer_args: "/S" + unc_file: "{{ smb_path | regex_replace('\\\\+$', '') }}\\{{ installer_file }}" tasks: - - name: Force unmount drive - community.windows.win_mapped_drive: - letter: "{{ drive_letter }}" - state: absent - ignore_errors: yes + - name: Check installer on share and list folder + ansible.windows.win_powershell: + parameters: + Share: "{{ smb_path }}" + User: "{{ smb_user }}" + Pass: "{{ smb_pass }}" + File: "{{ installer_file }}" + script: | + param($Share, $User, $Pass, $File) + $ErrorActionPreference = 'Stop' + net use $Share /delete /y 2>&1 | Out-Null + $out = net use $Share /user:$User $Pass 2>&1 + if ($LASTEXITCODE -ne 0) { throw "net use failed: $out" } + $names = Get-ChildItem -LiteralPath $Share | Select-Object -ExpandProperty Name + $Ansible.Result = @{ + exists = [bool]($names -contains $File) + files = $names + } + net use $Share /delete /y 2>&1 | Out-Null + register: share_check + no_log: true - - name: Mount network drive - community.windows.win_mapped_drive: - letter: "{{ drive_letter }}" - path: "{{ smb_path }}" - username: "{{ smb_user }}" - password: "{{ smb_pass }}" - state: present - - - name: Check installer exists - ansible.windows.win_stat: - path: "{{ drive_letter }}:\\{{ installer_file }}" - register: file_info + - name: Show files on share + ansible.builtin.debug: + var: share_check.result.files - name: Fail if installer not found ansible.builtin.fail: msg: "Файл {{ installer_file }} не найден в {{ smb_path }}" - when: not file_info.stat.exists + when: not share_check.result.exists - name: Install {{ installer_file }} ansible.windows.win_package: - path: "{{ drive_letter }}:\\{{ installer_file }}" + path: "{{ unc_file }}" + username: "{{ smb_user }}" + password: "{{ smb_pass }}" state: present arguments: "{{ installer_args }}" product_id: "{{ product_id | default(omit) }}" creates_path: "{{ creates_path | default(omit) }}" - - post_tasks: - - name: Unmount network drive - community.windows.win_mapped_drive: - letter: "{{ drive_letter }}" - state: absent - ignore_errors: yes \ No newline at end of file + no_log: true \ No newline at end of file