ASP.NET role

Now we have IIS up-and-running, let's look at enabling ASP.NET support. Again, let's start by creating the role:

$ ansible-galaxy init roles/asp

Starting with the variables in roles/asp/defaults/main.yml, you can see that they look similar to the HTML ones except that we have prefixed them with .aspx so that they do not clash with the variables from the iis role:

aspx_document_root: 'C:inetpubwwwrootansible'
aspx_file: default.aspx

aspx_heading: "Success !!!"
aspx_body: |
This HTML page has been deployed using Ansible to a <b>{{ ansible_distribution }}</b> host.<br><br>
The weboot is <b>{{ aspx_document_root }}</b> this file is called <b>{{ aspx_file }}</b>.<br><br>
The output below is from ASP.NET<br><br>
Hello from <%= Environment.MachineName %> at <%= DateTime.UtcNow %><br><br>

As you can see from the bottom of the page, we are including a function that prints the machine name, which in our case should be Vagrant, and also the date and time.

Next up, we have the template in roles/asp/templates/default.aspx.j2. Apart from the updated variables and filename, the content remains more or less the same as the one used in the iis role:

<!--{{ ansible_managed }}-->
<!doctype html>
<title>{{ html_heading }}</title>
<style>
body { text-align: center; padding: 150px; }
h1 { font-size: 50px; }
body { font: 20px Helvetica, sans-serif; color: #333; }
article { display: block; text-align: left; width: 650px; margin: 0 auto; }
</style>
<article>
<h1>{{ aspx_heading }}</h1>
<div>
<p>{{ aspx_body }}</p>
</div>
</article>

Next, we have the tasks that should be placed in roles/asp/tasks/main.yml. First of all, we are using the win_feature module to enable the components needed to get our basic page up-and-running:

- name: enable .net
win_feature:
name:
- "Net-Framework-Features"
- "Web-Asp-Net45"
- "Web-Net-Ext45"
state: "present"
notify: restart iis

Next, we need to create a folder to serve our page from and copy the rendered template:

- name: create the folder for our asp.net app
win_file:
path: "{{ aspx_document_root }}"
state: "directory"

- name: create an aspx file from a template
win_template:
src: "default.aspx.j2"
dest: "{{ aspx_document_root }}{{ aspx_file }}"

As you can see, we are using the win_template module again. As well as using the win_file module, the syntax for the file module is extremely close to that of the file module we have been using in other chapters. The final task checks that the site is configured correctly in IIS:

- name: ensure the default web application exists
win_iis_webapplication:
name: "Default"
state: "present"
physical_path: "{{ aspx_document_root }}"
application_pool: "DefaultAppPool"
site: "Default Web Site"

The win_iis_webapplication module is used for, as the name says, configuring web applications within IIS. This is not strictly required for our example, but it gives you an idea of what is possible.

You may have noticed that, when we enabled the additional features, we sent a notification to restart IIS. This means we have to add a task to the roles/asp/handlers/main.yml file. This task uses the win_service module to restart the webserver:

- name: restart iis
win_service:
name: w3svc
state: restarted

Now that we have our completed role, we can look at running the playbook again. First, we need to add the new role to the site.yml file:

---

- hosts: windows
gather_facts: true

vars_files:
- group_vars/common.yml

roles:
- roles/iis
- roles/asp

Then, we can run the playbook using the following command:

$ ansible-playbook -i production site.yml

This should give you something along the lines of the following output:

Opening your browser and going to http://localhost:8080/ansible/ should present you with something that looks similar to the following web page:

Let's remove our Vagrant box and look at a few more modules. To remove the box, run:

$ vagrant destroy

We can now look at creating a user using Ansible, and also install a few desktop applications using Chocolatey on a server host in AWS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.13.173