Contents

Win_feature changes will fail the tasks behind it, and it's solution

Contents
The problem with these code is, after Server-Gui-Shell, Server-Gui-Mgmt-Infra are uninstalled, the windows will reboot immediately, leaving index.html template deployment failed. 
vagrant@acs:~/pywinrm$ cat iis.yaml
---
- hosts: s12
  tasks:
  - name: Ensure IIS seb service is installed
    win_feature:
      name: web-server
      state: present
  - name: Ensure Server GUI is not installed
    win_feature:
      name: Server-Gui-Shell
      state: absent
      restart: true
  - name: Deploy index.html file
    template:
      src: iisstart.j2

You can put that Windows Feature block to the last of this playbook, or use handlers as below. However neither is best way.

vagrant@acs:~/pywinrm$ cat iis.yaml
---
- hosts: s12
  handlers:
  - name: Reboot
    win_reboot:
  tasks:
  - name: Ensure IIS seb service is installed
    win_feature:
      name: web-server
      state: present
    when: ansible_os_family == "Windows"
  - name: Ensure Server GUI is not installed
    win_feature:
      name: Server-Gui-Shell
      state: absent
    notify:
    - Reboot
  - name: Deploy index.html file
    template:
      src: iisstart.j2
      dest: c:\inetpub\wwwroot\iisstart.htm

Here is the best way according to ansible document:-
restart
no
True
False
Restarts the computer automatically when installation is complete, if restarting is required by the roles or features installed.
DEPRECATED in Ansible 2.4, as unmanaged reboots cause numerous issues under Ansible. Check the reboot_required return value from this module to determine if a reboot is necessary, and if so, use the win_reboot action to perform it.

restart_needed
DEPRECATED in Ansible 2.4 (refer to C(reboot_required) instead). True when the target server requires a reboot to complete updates (no further updates can be installed until after a reboot)
success
boolean
True
reboot_required
True when the target server requires a reboot to complete updates (no further updates can be installed until after a reboot)
success
boolean
True