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:
- Create a new file named
configure_iis_web_server.yml
. - Add the
hosts
line targeting theall
group.
---- hosts: all
- Use
vars_prompts
to prompt for a username and password.
vars_prompt:- name: usernameprompt: "Enter local username"private: no- name: passwordprompt: "Enter password
- Define the connection variables for a Windows host.
vars:ansible_user: "{{ username }}"ansible_password: "{{ password }}"ansible_connection: winrmansible_winrm_transport: ntlmansible_winrm_server_cert_validation: ignore
- Add the tasks list.
tasks:
- Install the
web-server
feature and all subfeatures with thewin_feature
module.
- name: Install IISwin_feature:name: web-serverinclude_management_tools: yesinclude_sub_features: yesstate: 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.
- Create an
index.html
file.
<!DOCTYPE html><html><body><h1>become Ansible: It's like shoulder surfing a co-worker </h1></body></html>
- Copy the
index.html
to the remote Windows host.
- name: Copy index.html to wwwrootwin_copy:src: index.htmldest: C:\inetpub\wwwroot\index.htmlforce: 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 defaultindex.html
file forIIS
. - The parameter
force
is used to overwrite and replace the defaultindex.html
.
- Create a
logs
directory.
- name: Create logs directorywin_file:path: c:\logsstate: 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.
- Install
dotnetcore
with no frameworks.
- name: install net core iis hosting module with no frameworkswin_chocolatey:name: "dotnetcore-windowshosting"version: "3.1.0"install_args: "OPT_NO_RUNTIME=1 OPT_NO_SHAREDFX=1 OPT_NO_X86=1OPT_NO_SHARED_CONFIG_CHECK=1"state: presentnotify: 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.
- Use a handler to restart IIS.
handlers:- name: restart IISwin_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.
- 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>
Execute the following command in the terminal to execute the playbook.
# Replace the public IP Address by the Windows Host IPansible-playbook configure_iis_web_server.yml -i <Public Ip Address>,
- Verify the configuration by hitting the web server’s landing page.
# Replace the public IP Address by the Windows Host IPcurl <Public Ip Address>
Congratulations! You set up an IIS
server successfully!
Try it now
- 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>
- 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.
# Replace the <Public IP Address> with the# remote Linux server's IP Addressansible-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.