106 lines
4.1 KiB
YAML
106 lines
4.1 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"
|
||
local_dir: 'C:\Windows\Temp\ansible_install'
|
||
local_file: "{{ local_dir }}\\{{ installer_file }}"
|
||
|
||
tasks:
|
||
- name: Copy installer from share to local temp
|
||
ansible.windows.win_powershell:
|
||
parameters:
|
||
Share: "{{ smb_path }}"
|
||
User: "{{ smb_user }}"
|
||
Pass: "{{ smb_pass }}"
|
||
File: "{{ installer_file }}"
|
||
Dest: "{{ local_dir }}"
|
||
script: |
|
||
param($Share, $User, $Pass, $File, $Dest)
|
||
$ErrorActionPreference = 'Continue'
|
||
$res = @{ copied = $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 {
|
||
$res.files = @(Get-ChildItem -LiteralPath $Share -ErrorAction Stop | Select-Object -ExpandProperty Name)
|
||
$src = Join-Path $Share $File
|
||
if (-not (Test-Path -LiteralPath $src)) {
|
||
$res.error = "Файл $File не найден на шаре"
|
||
} else {
|
||
New-Item -ItemType Directory -Path $Dest -Force | Out-Null
|
||
Copy-Item -LiteralPath $src -Destination $Dest -Force -ErrorAction Stop
|
||
$res.copied = $true
|
||
}
|
||
& net.exe use $Share /delete /y 2>&1 | Out-Null
|
||
}
|
||
} catch {
|
||
$res.error = $_.Exception.Message
|
||
}
|
||
$Ansible.Result = $res
|
||
register: copy_result
|
||
no_log: true
|
||
changed_when: false
|
||
failed_when: false
|
||
|
||
- name: Show copy result
|
||
ansible.builtin.debug:
|
||
msg:
|
||
error: "{{ copy_result.result.error | default(copy_result.msg | default('нет данных')) }}"
|
||
files: "{{ copy_result.result.files | default([]) }}"
|
||
|
||
- name: Fail if copy did not succeed
|
||
ansible.builtin.fail:
|
||
msg: "Не удалось скопировать {{ installer_file }}: {{ copy_result.result.error | default('неизвестная ошибка') }}"
|
||
when: not (copy_result.result.copied | default(false))
|
||
|
||
- name: Install and clean up
|
||
block:
|
||
- name: Check running FreeCAD
|
||
ansible.windows.win_shell: |
|
||
Get-Process FreeCAD -IncludeUserName -ErrorAction SilentlyContinue |
|
||
ForEach-Object { "$($_.UserName) (session $($_.SessionId))" }
|
||
register: running
|
||
changed_when: false
|
||
|
||
- name: Fail if FreeCAD is running
|
||
ansible.builtin.fail:
|
||
msg: "FreeCAD запущен у: {{ running.stdout_lines | join(', ') }}. Закройте его или включите force_close."
|
||
when: running.stdout_lines | length > 0 and not (force_close | default(false) | bool)
|
||
|
||
- name: Force close FreeCAD
|
||
ansible.windows.win_shell: Stop-Process -Name FreeCAD -Force
|
||
when: running.stdout_lines | length > 0 and (force_close | default(false) | bool)
|
||
|
||
- name: Install and clean up
|
||
block:
|
||
- name: Install {{ installer_file }}
|
||
ansible.windows.win_command: '"{{ local_file }}" /S /D=C:\Program Files\FreeCAD 1.1'
|
||
args:
|
||
creates: 'C:\Program Files\FreeCAD 1.1\bin\FreeCAD.exe'
|
||
register: inst
|
||
failed_when: inst.rc not in [0, 3010]
|
||
always:
|
||
- name: Remove local installer copy
|
||
ansible.windows.win_file:
|
||
path: "{{ local_dir }}"
|
||
state: absent |