Files
semaphore/playbooks/install_software_win.yml
T

89 lines
3.3 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: Install {{ installer_file }}
ansible.windows.win_package:
path: "{{ local_file }}"
state: present
arguments: "{{ installer_args }}"
product_id: "{{ product_id | default(omit) }}"
creates_path: "{{ creates_path | default(omit) }}"
always:
- name: Remove local installer copy
ansible.windows.win_file:
path: "{{ local_dir }}"
state: absent