Create an ansible playbook to install IIS on windows systems.Please make sure to include handlers to make sure that if any configuration made to IS configuration file, Handler must automatically restart the specific IIS server
Hi,
following the ansible-community best practices, i would rather suggest to set your focus on building a role instead of throwing everything into a playbook, use the playbook only to run that role.
Initiate your ansible-related poject with the below commands to ensure a proper scaffold of that role:
~$ mkdir ansible-iis
~$ cd ansible-iis/
~$ ansible-galaxy init IIS
- Role IIS was created successfully
~$ tree
.
└── IIS
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
So the handler directive will be go into IIS/handlers/main.yml
- name: Restart IIS service
ansible.windows.win_service:
name: iis
state: restarted
Your actual task to install IIS will be go into IIS/tasks/main.yml
- name: Install IIS (Web-Server only)
ansible.windows.win_feature:
name: Web-Server
state: present
restart: yes
This task will simply install IIS. And will be restarted after an sucessfully installation. So no need to use our previouly defined handler. So it would be wise to add something like a index file to proof the concept:
First create a example index file and put it into IIS/files:
<html>
<head>
<title>Ansible - IIS Demo</title>
</head>
<body>
<h1 style="background-color:DodgerBlue;"> Use Ansible to install and configure IIS</h1>
</body>
</html>
Then extend IIS/tasks/main.yml to actual copy the file and include a “Notify” statement with the reference of the previous created Handler “Restart IIS service”:
- name: Copy index test page
ansible.windows.win_copy:
src: files/index.html
dest: C:\\inetpub\\wwwroot\index.html
notify:
- Restart IIS service
Now we only have to create a playbook which runs the role and we are all set up with a proper ansible structure:
Create the playbook file:
~$ touch iis-install.yaml
Edit the playbook file iis-install.yaml to something like that:
- name: Install IIS
hosts: all
roles:
- role: "IIS"
Ensure you have a proper inventory file, then run the playbook:
ansible-playbook -i inventory.yaml iis-install.yaml
That should do the trick.
Hey Simon,
Thank you for your detailed answer. I am a beginner in ansible so so not have much knowledge about roles and using them. will try to explore more.
I have made this -
---
- name: Install IIS
hosts: windows
gather_facts: true
tasks:
-
name: Install IIS (Web-Server only)
ansible.windows.win_feature:
name: Web-Server
state: present
restart: yes
-
name: Copy index test page
ansible.windows.win_copy:
src: files/index.html
dest: C:\\inetpub\\wwwroot\index.html
notify: Restart IIS service
handlers:
-
name: Restart IIS service
ansible.windows.win_service:
name: iis
state: restarted
A Handler will only be triggered if the declarative state differs from the actual state, or in simple words. If there is an actual change.
Your screenshot shows, that ansible have not changed anything on the current play.
Can you edit the content of the index file and rerun the playbook?
For example just add another section heading to you index.html like:
<html>
<head>
<title>Ansible - IIS Demo</title>
</head>
<body>
<h1 style="background-color:DodgerBlue;"> Use Ansible to install and configure IIS</h1>
<h2>Hi, i am the change!</h2>
</body>
</html>
That should actually trigger the handler via the notify statement in the “Copy index test page” task.
Your code looks fine at the first glance compared to the official documentation - so i guess that is the missing part