---
- name: change workgroup (check mode)
  win_domain_membership:
    workgroup_name: ANSIBLETEST
    state: workgroup
    domain_admin_user: fake user
    domain_admin_password: fake password
  register: change_workgroup_check
  check_mode: yes

- name: get result of change workgroup (check mode)
  win_shell: (Get-WmiObject Win32_ComputerSystem).Workgroup
  register: change_workgroup_result_check

- name: assert result of change workgroup (check mode)
  assert:
    that:
    - change_workgroup_check|changed
    - change_workgroup_result_check.stdout == workgroup.stdout

- name: change workgroup
  win_domain_membership:
    workgroup_name: ANSIBLETEST
    state: workgroup
    domain_admin_user: fake user
    domain_admin_password: fake password
  register: change_workgroup

- name: get result of change workgroup
  win_shell: (Get-WmiObject Win32_ComputerSystem).Workgroup
  register: change_workgroup_result

- name: assert result of change workgroup
  assert:
    that:
    - change_workgroup|changed
    - change_workgroup_result.stdout_lines[0] == "ANSIBLETEST"

- name: change workgroup (idempotent)
  win_domain_membership:
    workgroup_name: ANSIBLETEST
    state: workgroup
    domain_admin_user: fake user
    domain_admin_password: fake password
  register: change_workgroup_again

- name: assert result of change workgroup (idempotent)
  assert:
    that:
    - not change_workgroup_again|changed

- name: change workgroup fail invalid name
  win_domain_membership:
    workgroup_name: ANSIBLELONGNAMEFAILURE
    state: workgroup
    domain_admin_user: fake user
    domain_admin_password: fake password
  register: fail_change_workgroup
  failed_when: "fail_change_workgroup.msg != 'failed to set workgroup through WMI, return value: 2695'"
