From a979624b8813aa27c995ef732e0e8f4bb9806c49 Mon Sep 17 00:00:00 2001 From: Corwin Brown Date: Tue, 13 Oct 2015 09:47:56 -0500 Subject: [PATCH] Adding win_uri module --- .../modules/extras/windows/win_uri.ps1 | 76 +++++++++++++++ lib/ansible/modules/extras/windows/win_uri.py | 93 +++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 lib/ansible/modules/extras/windows/win_uri.ps1 create mode 100644 lib/ansible/modules/extras/windows/win_uri.py diff --git a/lib/ansible/modules/extras/windows/win_uri.ps1 b/lib/ansible/modules/extras/windows/win_uri.ps1 new file mode 100644 index 00000000000..9c2ddededae --- /dev/null +++ b/lib/ansible/modules/extras/windows/win_uri.ps1 @@ -0,0 +1,76 @@ +#!powershell +# This file is part of Ansible +# +# Copyright 2015, Corwin Brown +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# WANT_JSON +# POWERSHELL_COMMON + +$params = Parse-Args $args; + +$result = New-Object psobject @{ + win_uri = New-Object psobject +} + +# Build Arguments +$webrequest_opts = @{} +if (Get-Member -InputObject $params -Name url) { + $url = $params.url.ToString() + $webrequest_opts.Uri = $url +} else { + Fail-Json $result "Missing required argument: url" +} + +if (Get-Member -InputObject $params -Name method) { + $method = $params.method.ToString() + $webrequest_opts.Method = $method +} + +if (Get-Member -InputObject $params -Name content_type) { + $content_type = $params.method.content_type.ToString() + $webrequest_opts.ContentType = $content_type +} + +if (Get-Member -InputObject $params -Name body) { + $body = $params.method.body.ToString() + $webrequest_opts.Body = $body +} + +if (Get-Member -InputObject $params -Name headers) { + $headers = $params.headers + Set-Attr $result.win_uri "headers" $headers + + $req_headers = @{} + ForEach ($header in $headers.psobject.properties) { + $req_headers.Add($header.Name, $header.Value) + } + + $webrequest_opts.Headers = $req_headers +} + +try { + $response = Invoke-WebRequest @webrequest_opts +} catch { + $ErrorMessage = $_.Exception.Message + Fail-Json $result $ErrorMessage +} + +ForEach ($prop in $response.psobject.properties) { + Set-Attr $result $prop.Name $prop.Value +} + +Exit-Json $result + diff --git a/lib/ansible/modules/extras/windows/win_uri.py b/lib/ansible/modules/extras/windows/win_uri.py new file mode 100644 index 00000000000..451c965836d --- /dev/null +++ b/lib/ansible/modules/extras/windows/win_uri.py @@ -0,0 +1,93 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2015, Corwin Brown +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# this is a windows documentation stub. actual code lives in the .ps1 +# file of the same name + +DOCUMENTATION = """ +--- +module: win_uri +version_added: "" +short_description: Interacts with webservices. +description: + - Interacts with HTTP and HTTPS services. +options: + url: + description: + - HTTP or HTTPS URL in the form of (http|https)://host.domain:port/path + method: + description: + - The HTTP Method of the request or response. + default: GET + choices: + - GET + - POST + - PUT + - HEAD + - DELETE + - OPTIONS + - PATCH + - TRACE + - CONNECT + - REFRESH + content_type: + description: + - Sets the "Content-Type" header. + body: + description: + - The body of the HTTP request/response to the web service. + headers: + description: + - Key Value pairs for headers. Example "Host: www.somesite.com" +author: Corwin Brown +""" + +Examples= """ +# Send a GET request and store the output: +--- +- name: Perform a GET and Store Output + win_uri: + url: http://www.somesite.com/myendpoint + register: http_output + +# Set a HOST header to hit an internal webserver: +--- +- name: Hit a Specific Host on the Server + win_uri: + url: http://my.internal.server.com + method: GET + headers: + host: "www.somesite.com + +# Do a HEAD request on an endpoint +--- +- name: Perform a HEAD on an Endpoint + win_uri: + url: http://www.somesite.com + method: HEAD + +# Post a body to an endpoint +--- +- name: POST a Body to an Endpoint + win_uri: + url: http://www.somesite.com + method: POST + body: "{ 'some': 'json' }" +"""