Files
2026-10-21 13:17:25 -05:00

92 lines
3.0 KiB
YAML

- name: Windows updates and Telegraf maintenance
hosts: win-pc
gather_facts: true
vars:
telegraf_version: "1.39.2"
influxdb_url: "http://docker2.tail99133.ts.net:8086"
influxdb_org: "homelab"
influxdb_bucket: "telegraf"
windows_install_dir: "C:\\Program Files\\Telegraf"
windows_conf: "{{ windows_install_dir }}\\telegraf.conf"
update_categories:
- SecurityUpdates
- CriticalUpdates
- UpdateRollups
- WindowsDefender
secret_search_paths:
- "C:\\Program Files\\Telegraf\\secrets"
- "C:\\secrets\\influxdb"
- "\\\\nas\\public\\Hermes_Data\\telegraf-windows\\secrets"
influxdb_token: ""
tasks:
- name: Configure power settings
block:
- name: Disable sleep
win_shell: powercfg /change standby-timeout-ac 0; powercfg /change monitor-timeout-ac 30; powercfg /change hibernate-timeout-ac 0; powercfg /change standby-timeout-dc 0; powercfg /change monitor-timeout-dc 30; powercfg /change hibernate-timeout-dc 0
- name: Set active power scheme to always on
win_shell: powercfg /setactive 8c5e7fda-e8bf-45a6-a7cc-4e54e757e0d4
ignore_errors: true
- name: Install Windows updates
win_updates:
category_names: "{{ update_categories }}"
state: installed
reboot: no
- name: Ensure temp directory exists
win_file:
path: C:\Temp
state: directory
- name: Download Telegraf {{ telegraf_version }} zip
win_get_url:
url: "https://dl.influxdata.com/telegraf/releases/telegraf-{{ telegraf_version }}_windows_amd64.zip"
dest: C:\Temp\telegraf-{{ telegraf_version }}.zip
- name: Extract Telegraf
win_unzip:
src: C:\Temp\telegraf-{{ telegraf_version }}.zip
dest: C:\Temp\telegraf
creates: C:\Temp\telegraf\telegraf-{{ telegraf_version }}\telegraf.exe
- name: Copy Telegraf to Program Files
win_command: cmd /c xcopy /E /I /Y "C:\Temp\telegraf\telegraf-{{ telegraf_version }}" "{{ windows_install_dir }}"
- name: Deploy Windows Telegraf config
win_copy:
src: ../templates/telegraf-windows.conf.j2
dest: "{{ windows_conf }}"
force: yes
notify: Restart Windows Telegraf
- name: Install Telegraf service if missing
win_shell: |
if (Get-Service -Name telegraf -ErrorAction SilentlyContinue) {
Write-Output "telegraf service already exists"
} else {
& '{{ windows_install_dir }}\telegraf.exe' --service install --config '{{ windows_conf }}' --non-strict-env-handling
}
args:
executable: powershell
- name: Configure Telegraf service auto-restart
win_shell: |
sc.exe failure telegraf reset= 86400 actions= restart/5000
sc.exe start telegraf
args:
executable: cmd
- name: Ensure Telegraf service is running
win_service:
name: telegraf
state: started
start_mode: auto
handlers:
- name: Restart Windows Telegraf
win_service:
name: telegraf
state: restarted