Configure a Windows IIS Web Server

Configure a Windows host IIS (Internet Information Services) web server with an Ansible playbook.

We'll cover the following

Let’s configure a Windows host to run an IIS(Internet Information Services) web server with an Ansible playbook. You’ll perform the following steps:

  1. Create a new file named configure_iis_web_server.yml.
  2. Add the hosts line targeting the all group.
Press + to interact
---
- hosts: all
  1. Use vars_prompts to prompt for a username and password.
Press + to interact
vars_prompt:
- name: username
prompt: "Enter local username"
private: no
- name: password
prompt: "Enter password
  1. Define the connection variables for a Windows host.
Press + to interact
vars:
ansible_user: "{{ username }}"
ansible_password: "{{ password }}"
ansible_connection: winrm
ansible_winrm_transport: ntlm
ansible_winrm_server_cert_validation: ignore
  1. Add the tasks list.
Press + to interact
tasks:
  1. Install the web-server feature and all subfeatures with the win_feature module.
Press + to interact
- name: Install IIS
win_feature:
name: web-server
include_management_tools: yes
include_sub_features: yes
state: present

win_feature is an Ansible module used to install or uninstall roles or features for Windows servers. It requires the name parameter, which is passed as a web-server. web-server is the Windows feature’s name to install IIS.

include_management_tools is specified and provided a value of yes to install the management tools for IIS.

include_sub_features is also provided a value of yes. All the sub-features of the web-server are also installed. state indicates whether to install or uninstall. The value of the present installs the feature.

  1. Create an index.html file.
Press + to interact
<!DOCTYPE html>
<html>
<body>
<h1>become Ansible: It's like shoulder surfing a co-worker </h1>
</body>
</html>
  1. Copy the index.html to the remote Windows host.
Press + to interact
- name: Copy index.html to wwwroot
win_copy:
src: index.html
dest: C:\inetpub\wwwroot\index.html
force: yes

The Ansible module win_copy copies a file on the local host to a remote Windows location. Here are the parameters and their functions;

  • src is the local path to the file. When a full path isn’t provided, the relative path is used.
  • The dest parameter specifies the location of the default index.html file for IIS.
  • The parameter force is used to overwrite and replace the default index.html.
  1. Create a logs directory.
Press + to interact
- name: Create logs directory
win_file:
path: c:\logs
state: directory

win_file is used to create empty files, update file modifications of existing files, and/or to create directories.

path is a required parameter used to specify the location of the logs directory on the remote host. state is set to directory, which results in Ansible creating a directory.

  1. Install dotnetcore with no frameworks.
Press + to interact
- name: install net core iis hosting module with no frameworks
win_chocolatey:
name: "dotnetcore-windowshosting"
version: "3.1.0"
install_args: "OPT_NO_RUNTIME=1 OPT_NO_SHAREDFX=1 OPT_NO_X86=1
OPT_NO_SHARED_CONFIG_CHECK=1"
state: present
notify: restart IIS

Chocolatey is a package manager for Windows that automates software installs. win_chocolatey is the module for using Chocolatey with Ansible. This is a great example of how Ansible is more than just an automation tool. It’s an orchestration engine. Other automation technologies can also be plugged into Ansible to augment it.

You use win_chocolatey to install the dotnetcore-windowshosting package from the public Chocolatey feed. The module is given the version parameter to lock the version installed to 3.1.0. You provide several arguments as a value to the install_args to install it with no frameworks.

Installing the dotnetcore-windowshosting package requires a restart of IIS. The IIS restart should only occur after the install. IIS should not be restarted any time after the first install because it has the potential of causing an outage.

Running operations when a change occurs is accomplished by using the notify module. The notify module will trigger the restart IIS handler only when the task reports a change.

  1. Use a handler to restart IIS.
Press + to interact
handlers:
- name: restart IIS
win_shell: '& {iisreset}'

A handler is defined outside the tasks list, in its own list in the YAML file. Each handler requires

  • A name
  • An action

The handler named restart IIS calls the win_shell Ansible module to execute PowerShell on a remote Windows host. The command & {iisreset} restarts IIS with the command-line utility.

  1. Review and run the final playbook and the index.html below:
<!DOCTYPE html>
<html>
<body>
<h1>become Ansible: It's like shoulder surfing a co-worker. </h1>
</body>
</html>
Configure IIS server

Execute the following command in the terminal to execute the playbook.

Press + to interact
# Replace the public IP Address by the Windows Host IP
ansible-playbook configure_iis_web_server.yml -i <Public Ip Address>,
  1. Verify the configuration by hitting the web server’s landing page.
Press + to interact
# Replace the public IP Address by the Windows Host IP
curl <Public Ip Address>

Congratulations! You set up an IIS server successfully!

Try it now

  1. Add a task that executes any shell command in the configure_iis_web_server.yml file.
    • Find the Windows shell commands in Ansible’s win_shell module link.
<!DOCTYPE html>
<html>
<body>
<h1>become Ansible: It's like shoulder surfing a co-worker. </h1>
</body>
</html>
Add a shell task
  1. Run the playbook with verbosity set to 3.

Click on the Run button and wait for the environment to set up. Execute the playbook in the terminal.

Press + to interact
# Replace the <Public IP Address> with the
# remote Linux server's IP Address
ansible-playbook configure_iis_web_server.yml -i <Public Ip Address>, -vvv

In this lesson, you successfully configured an IIS server on the Windows remote host using Ansible playbook.

Get hands-on with 1300+ tech skills courses.