Playbooks grow. One day your “configure web server” playbook is 300 lines and copied into three other playbooks. Roles fix that: a role packages tasks, variables, templates and handlers into one reusable unit you can drop into any playbook.
Role structure
Create the skeleton:
cd ansible/roles
ansible-galaxy role init iis_site
roles/iis_site/
├── defaults/main.yml # default variables (easy to override)
├── tasks/main.yml # what the role does
├── handlers/main.yml # actions triggered by changes (e.g. restart)
├── templates/ # Jinja2 templates, e.g. config files
├── files/ # static files to copy
├── vars/main.yml # fixed internal variables (rarely needed)
└── meta/main.yml # metadata and role dependencies
Delete folders you don’t use.
defaults/main.yml
iis_site_name: DemoApp
iis_site_port: 80
iis_site_root: 'D:\Sites\DemoApp'
iis_app_pool: DemoAppPool
Prefix variables with the role name so they never collide with other roles.
tasks/main.yml
- name: Install IIS
ansible.windows.win_feature:
name: Web-Server
include_management_tools: true
state: present
register: iis_feature
- name: Reboot if IIS needs it
ansible.windows.win_reboot:
when: iis_feature.reboot_required
- name: Create site folder
ansible.windows.win_file:
path: "{{ iis_site_root }}"
state: directory
- name: Deploy home page
ansible.windows.win_template:
src: index.html.j2
dest: "{{ iis_site_root }}\\index.html"
notify: Recycle app pool
- name: Create app pool
community.windows.win_iis_webapppool:
name: "{{ iis_app_pool }}"
state: started
- name: Create website
community.windows.win_iis_website:
name: "{{ iis_site_name }}"
physical_path: "{{ iis_site_root }}"
application_pool: "{{ iis_app_pool }}"
port: "{{ iis_site_port }}"
state: started
The IIS modules come from the community.windows collection (ansible-galaxy collection install community.windows). Check your installed version’s documentation – some IIS modules have newer replacements in the microsoft.iis collection.
templates/index.html.j2
<h1>{{ iis_site_name }}</h1>
<p>Served by {{ inventory_hostname }}</p>
Templates fill in variables per server – ideal for config files like web.config or appsettings.json that differ by environment.
handlers/main.yml
- name: Recycle app pool
ansible.windows.win_shell: "Restart-WebAppPool -Name '{{ iis_app_pool }}'"
A handler runs once, at the end of the play, only if a task that notifies it changed something. Ten config changes → one recycle. No changes → no recycle.
Use the role
playbooks/web.yml:
- name: Configure web servers
hosts: web
gather_facts: false
roles:
- role: iis_site
vars:
iis_site_name: Intranet
iis_site_root: 'D:\Sites\Intranet'
Override defaults per play, or per group in group_vars/web.yml.
Good role habits
- One purpose per role:
iis_site,windows_baseline,monitoring_agent. - Idempotent tasks – use modules, not raw scripts, so a second run reports ok not changed.
- A short README.md in each role listing its variables.
- Test with
--check --diffon a lab group before production. - Share common roles between teams through a Git repo or a private collection.
FAQ
Roles or collections? A collection is a package that can contain many roles plus modules and plugins. Start with roles in your repo; package them as a collection when several repos need them.
What’s next
Next: Patch Windows servers with Ansible – rolling updates.
Practise this topic
Test yourself with free questions on this topic – instant answers and explanations.
Start practice questionsAnsibleRolesIISWindows ServerIntermediate