Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add win_service task to the role #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ If you change the version, the `windows_exporter` binary will be replaced with t

windows_exporter_bin_path: 'C:\Windows\temp\windows_exporter-{{ windows_exporter_version }}-{{ windows_exporter_arch }}.msi'


The path where the `windows_exporter` binary will be downloaded and installed from.

windows_exporter_options: ''

Any additional options to pass to `windows_exporter` when it starts, e.g. `--no-collector.wifi` if you want to ignore any WiFi data.
Need to use MSI Parametes as stated in https://github.com/prometheus-community/windows_exporter#installation

windows_exporter_state: restarted
windows_exporter_enabled: true
windows_exporter_state: started
windows_exporter_start_mode: delayed

Controls for the `windows_exporter` service.
Controls for the `windows_exporter` service. `windows_exporter_start_mode` controls the [start_mode](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_service_module.html#parameter-start_mode) of the service. `windows_exporter_state` controls the [state](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_service_module.html#parameter-state) of the service on the host.

## Dependencies

Expand Down
4 changes: 2 additions & 2 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ windows_exporter_arch: 'amd64'
windows_exporter_download_url: https://github.com/prometheus-community/windows_exporter/releases/download/v{{ windows_exporter_version }}/windows_exporter-{{ windows_exporter_version }}-{{ windows_exporter_arch }}.msi
windows_exporter_options: ''
windows_exporter_bin_path: '"C:\Program Files\windows_exporter\windows_exporter.exe"'
windows_exporter_state: restarted
windows_exporter_enabled: true
windows_exporter_state: started
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched the default state to started so that the role becomes idempotent. With restarted every time I run a play with this role, my services get restarted which is not desirable during normal operation imo.

windows_exporter_start_mode: delayed
17 changes: 16 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---
- name: Check that the service variable values are sane
assert:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this assert to try to provide some user validation if they pass in strange combinations. I am sure this will not cover all possible combinations that could break the win_service task further on, but imo is a solid start.

that:
- windows_exporter_state != 'started' and windows_exporter_start_mode != 'disabled'
fail_msg: "You cannot manage the service with the windows_exporter_state and windows_exporter_start_mode settings"
- name: Check current windows_exporter version.
win_command: "{{ windows_exporter_bin_path }} --version"
failed_when: false
Expand Down Expand Up @@ -27,10 +32,20 @@
state: present
enabled: yes
notify: restart windows_exporter

- name: windows_exporter service settings
ansible.windows.win_service:
name: windows_exporter
start_mode: "{{ windows_exporter_start_mode }}"
state: "{{ windows_exporter_state }}"
- name: Verify windows_exporter is responding to requests.
win_uri:
url: http://localhost:9182/
return_content: true
register: metrics_output
failed_when: "'Metrics' not in metrics_output.content"
when: >
windows_exporter_start_mode <> 'disabled' or
windows_exporter_start_mode <> 'manual' and (
windows_exporter_state == 'started' OR WINDOWS_EXPORTER_STATE == 'restarted'
)