From 67a66d0e357c0487c75ec30c86efd2d5b11aa01b Mon Sep 17 00:00:00 2001 From: Bill Peck Date: Fri, 15 Nov 2024 19:25:49 -0500 Subject: [PATCH] Add validated role windows_manage_iis windows_manage_iis validated role. --- roles/windows_manage_iis/defaults/main.yml | 3 + roles/windows_manage_iis/handlers/main.yml | 7 ++ .../meta/argument_specs.yml | 36 ++++++++++ roles/windows_manage_iis/tasks/create.yml | 69 +++++++++++++++++++ roles/windows_manage_iis/tasks/delete.yml | 51 ++++++++++++++ roles/windows_manage_iis/tasks/main.yml | 5 ++ .../templates/index.html.j2 | 8 +++ .../aliases | 3 + .../defaults/main.yml | 6 ++ .../tasks/main.yml | 28 ++++++++ 10 files changed, 216 insertions(+) create mode 100644 roles/windows_manage_iis/defaults/main.yml create mode 100644 roles/windows_manage_iis/handlers/main.yml create mode 100644 roles/windows_manage_iis/meta/argument_specs.yml create mode 100644 roles/windows_manage_iis/tasks/create.yml create mode 100644 roles/windows_manage_iis/tasks/delete.yml create mode 100644 roles/windows_manage_iis/tasks/main.yml create mode 100644 roles/windows_manage_iis/templates/index.html.j2 create mode 100644 tests/integration/targets/windows_ops_test_windows_manage_iis/aliases create mode 100644 tests/integration/targets/windows_ops_test_windows_manage_iis/defaults/main.yml create mode 100644 tests/integration/targets/windows_ops_test_windows_manage_iis/tasks/main.yml diff --git a/roles/windows_manage_iis/defaults/main.yml b/roles/windows_manage_iis/defaults/main.yml new file mode 100644 index 0000000..e05cd62 --- /dev/null +++ b/roles/windows_manage_iis/defaults/main.yml @@ -0,0 +1,3 @@ +--- +# defaults file for +windows_manage_iis_operation: create diff --git a/roles/windows_manage_iis/handlers/main.yml b/roles/windows_manage_iis/handlers/main.yml new file mode 100644 index 0000000..cc50909 --- /dev/null +++ b/roles/windows_manage_iis/handlers/main.yml @@ -0,0 +1,7 @@ +--- +# handlers file for windows_managed_iis +- name: Restart iis service + ansible.windows.win_service: + name: W3Svc + state: restarted + start_mode: auto diff --git a/roles/windows_manage_iis/meta/argument_specs.yml b/roles/windows_manage_iis/meta/argument_specs.yml new file mode 100644 index 0000000..02c7bd3 --- /dev/null +++ b/roles/windows_manage_iis/meta/argument_specs.yml @@ -0,0 +1,36 @@ +--- +argument_specs: + main: + version_added: 1.0.0 + short_description: A role to Create/Delete an IIS Web server. + description: + - A role to Create/Delete an IIS Web server. + options: + windows_manage_iis_operation: + type: str + description: Operation to perform. + default: "create" + choices: ["create", "delete"] + windows_manage_iis_delete_option: + type: str + description: + - used with O(windows_manage_iis_operation=delete). + - This option specifies whether to delete all resources including the IIS service, or only the server. + default: "server" + choices: ["all", "server"] + windows_manage_iis_name: + type: str + description: The name of the Server. + required: true + windows_manage_iis_port: + type: int + description: The network port that the Server will listen on. + default: 80 + required: true + windows_manage_iis_path: + type: str + description: The path on disk where the web content will be served from. + required: true + windows_manage_iis_test_message: + type: str + description: The test message that will be used in the index.html. diff --git a/roles/windows_manage_iis/tasks/create.yml b/roles/windows_manage_iis/tasks/create.yml new file mode 100644 index 0000000..722d5f1 --- /dev/null +++ b/roles/windows_manage_iis/tasks/create.yml @@ -0,0 +1,69 @@ +--- +# tasks file for windows_manage_iis/create + +- name: Install IIS + ansible.windows.win_feature: + name: Web-Server + state: present + register: windows_manage_iis_output + +- name: Update Results w/win_feature + ansible.builtin.set_fact: + windows_manage_iis_results: "{{ windows_manage_iis_results | default({}) | combine({'win_feature': windows_manage_iis_output}) }}" + +- name: Create site directory structure + ansible.windows.win_file: + path: "{{ windows_manage_iis_path }}" + state: directory + register: windows_manage_iis_output + +- name: Update Results w/win_file + ansible.builtin.set_fact: + windows_manage_iis_results: "{{ windows_manage_iis_results | default({}) | combine({'win_file': windows_manage_iis_output}) }}" + +- name: Delete Default Site + community.windows.win_iis_website: + name: "Default Web Site" + state: absent + +- name: Create IIS site + community.windows.win_iis_website: + name: "{{ windows_manage_iis_name }}" + state: started + port: "{{ windows_manage_iis_port }}" + physical_path: "{{ windows_manage_iis_path }}" + notify: restart iis service + register: windows_manage_iis_output + +- name: Update Results w/win_iis_website + ansible.builtin.set_fact: + windows_manage_iis_results: "{{ windows_manage_iis_results | default({}) | combine({'win_iis_website': windows_manage_iis_output}) }}" + +- name: Open port for site on the firewall + community.windows.win_firewall_rule: + name: "iisport{{ windows_manage_iis_port }}" + enable: true + state: present + localport: "{{ windows_manage_iis_port }}" + action: Allow + direction: In + protocol: Tcp + register: windows_manage_iis_output + +- name: Update Results w/win_firewall_rule + ansible.builtin.set_fact: + windows_manage_iis_results: "{{ windows_manage_iis_results | default({}) | combine({'win_firewall_rule': windows_manage_iis_output}) }}" + +- name: Template simple web site to iis_site_path as index.html + ansible.windows.win_template: + src: 'index.html.j2' + dest: '{{ windows_manage_iis_path }}\index.html' + register: windows_manage_iis_output + +- name: Update Results w/win_template + ansible.builtin.set_fact: + windows_manage_iis_results: "{{ windows_manage_iis_results | default({}) | combine({'win_template': windows_manage_iis_output}) }}" + +- name: Show results + ansible.builtin.debug: + var: windows_manage_iis_results diff --git a/roles/windows_manage_iis/tasks/delete.yml b/roles/windows_manage_iis/tasks/delete.yml new file mode 100644 index 0000000..5008b1a --- /dev/null +++ b/roles/windows_manage_iis/tasks/delete.yml @@ -0,0 +1,51 @@ +--- +# tasks file for windows_manage_iis/delete + +- name: Close port for site on the firewall + community.windows.win_firewall_rule: + name: "iisport{{ windows_manage_iis_port }}" + state: absent + register: windows_manage_iis_output + +- name: Update Results w/win_firewall_rule + ansible.builtin.set_fact: + windows_manage_iis_results: "{{ windows_manage_iis_results | default({}) | combine({'win_firewall_rule': windows_manage_iis_output}) }}" + +- name: Delete IIS site + community.windows.win_iis_website: + name: "{{ windows_manage_iis_name }}" + state: absent + notify: restart iis service + register: windows_manage_iis_output + +- name: Update Results w/win_iis_website + ansible.builtin.set_fact: + windows_manage_iis_results: "{{ windows_manage_iis_results | default({}) | combine({'win_iis_website': windows_manage_iis_output}) }}" + +- name: Delete site directory structure + ansible.windows.win_file: + path: "{{ windows_manage_iis_path }}" + state: absent + register: windows_manage_iis_output + +- name: Update Results w/win_file + ansible.builtin.set_fact: + windows_manage_iis_results: "{{ windows_manage_iis_results | default({}) | combine({'win_file': windows_manage_iis_output}) }}" + +- name: Remove IIS Service + ansible.windows.win_feature: + name: Web-Server + state: absent + when: + - windows_manage_iis_delete_option == 'all' + register: windows_manage_iis_output + +- name: Update Results w/win_feature + ansible.builtin.set_fact: + windows_manage_iis_results: "{{ windows_manage_iis_results | default({}) | combine({'win_feature': windows_manage_iis_output}) }}" + when: + - windows_manage_iis_delete_option == 'all' + +- name: Show results + ansible.builtin.debug: + var: windows_manage_iis_results diff --git a/roles/windows_manage_iis/tasks/main.yml b/roles/windows_manage_iis/tasks/main.yml new file mode 100644 index 0000000..c00ad5f --- /dev/null +++ b/roles/windows_manage_iis/tasks/main.yml @@ -0,0 +1,5 @@ +--- +# tasks file for windows_managed_iis + +- name: Create or delete Windows IIS server + ansible.builtin.include_tasks: "{{ windows_manage_iis_operation }}.yml" diff --git a/roles/windows_manage_iis/templates/index.html.j2 b/roles/windows_manage_iis/templates/index.html.j2 new file mode 100644 index 0000000..3fa7245 --- /dev/null +++ b/roles/windows_manage_iis/templates/index.html.j2 @@ -0,0 +1,8 @@ + + + +

+

{{ windows_manage_iis_test_message }}

+ + + diff --git a/tests/integration/targets/windows_ops_test_windows_manage_iis/aliases b/tests/integration/targets/windows_ops_test_windows_manage_iis/aliases new file mode 100644 index 0000000..535546a --- /dev/null +++ b/tests/integration/targets/windows_ops_test_windows_manage_iis/aliases @@ -0,0 +1,3 @@ +windows +infra/windows +role/windows_manage_iis diff --git a/tests/integration/targets/windows_ops_test_windows_manage_iis/defaults/main.yml b/tests/integration/targets/windows_ops_test_windows_manage_iis/defaults/main.yml new file mode 100644 index 0000000..1c68bc0 --- /dev/null +++ b/tests/integration/targets/windows_ops_test_windows_manage_iis/defaults/main.yml @@ -0,0 +1,6 @@ +--- +windows_manage_iis_name: "test-iis-server" +windows_manage_iis_port: 8080 +windows_manage_iis_path: "C:\\sites\\playbooktest" +windows_manage_iis_feature: None +windows_manage_iis_test_message: "Never gonna give you up, Never gonna let you go" diff --git a/tests/integration/targets/windows_ops_test_windows_manage_iis/tasks/main.yml b/tests/integration/targets/windows_ops_test_windows_manage_iis/tasks/main.yml new file mode 100644 index 0000000..e2db405 --- /dev/null +++ b/tests/integration/targets/windows_ops_test_windows_manage_iis/tasks/main.yml @@ -0,0 +1,28 @@ +--- +# Test: Create and Delete IIS server +- name: Create IIS Server + ansible.builtin.include_role: + name: infra.windows_ops.windows_manage_iis + vars: + windows_manage_iis_operation: create + +# Verify that IIS Server was created as expected +- name: Retrieve index.html + ansible.windows.win_uri: + url: "http://{{ ansible_host }}:{{ windows_manage_iis_port }}" + return_content: true + register: this + failed_when: this is failed or windows_manage_iis_test_message not in this.content + +- name: Delete IIS Server + ansible.builtin.include_role: + name: infra.windows_ops.windows_manage_iis + vars: + windows_manage_iis_operation: delete + +# Verify that IIS Server was created as expected +- name: Retrieve index.html + ansible.windows.win_uri: + url: "http://{{ ansible_host }}:{{ windows_manage_iis_port }}" + register: this + failed_when: this is not failed