81 lines
2.9 KiB
YAML
81 lines
2.9 KiB
YAML
---
|
|
- 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
|
|
|
|
vars:
|
|
installer_args: "/S"
|
|
unc_file: "{{ smb_path | regex_replace('\\\\+$', '') }}\\{{ installer_file }}"
|
|
|
|
tasks:
|
|
- 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 = 'Continue'
|
|
$res = @{ exists = $false; files = @(); error = '' }
|
|
try {
|
|
& net.exe use $Share /delete /y 2>&1 | Out-Null
|
|
$out = & net.exe use $Share "/user:$User" $Pass 2>&1 | Out-String
|
|
if ($LASTEXITCODE -ne 0) {
|
|
$res.error = "net use failed (code $LASTEXITCODE): $out"
|
|
} else {
|
|
$names = @(Get-ChildItem -LiteralPath $Share -ErrorAction Stop | Select-Object -ExpandProperty Name)
|
|
$res.files = $names
|
|
$res.exists = [bool]($names -contains $File)
|
|
& net.exe use $Share /delete /y 2>&1 | Out-Null
|
|
}
|
|
} catch {
|
|
$res.error = $_.Exception.Message
|
|
}
|
|
$Ansible.Result = $res
|
|
register: share_check
|
|
no_log: true
|
|
failed_when: false
|
|
|
|
- name: Show share check result
|
|
ansible.builtin.debug:
|
|
msg:
|
|
error: "{{ share_check.result.error | default(share_check.msg | default('нет данных')) }}"
|
|
files: "{{ share_check.result.files | default([]) }}"
|
|
|
|
- name: Fail if share unavailable or file missing
|
|
ansible.builtin.fail:
|
|
msg: >-
|
|
{{ 'Ошибка доступа к шаре: ' ~ share_check.result.error
|
|
if (share_check.result.error | default('')) != ''
|
|
else 'Файл ' ~ installer_file ~ ' не найден в ' ~ smb_path }}
|
|
when: >-
|
|
(share_check.result.error | default('x')) != '' or
|
|
not (share_check.result.exists | default(false))
|
|
|
|
- name: Install {{ installer_file }}
|
|
ansible.windows.win_package:
|
|
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) }}"
|
|
no_log: true |