Get the response payload of uri GET method - azure

I am trying to fetch the response of an HTTP Get command of Azure resource creation check url. The response payload is either
{
"status": "InProgress"
}
OR
{
"status": "Succeeded"
}
What I want to do is to wait for the creation of the resource until we get "status": "Succeeded" reply. My ansible code is below:
- name: fetch the result of the resource creation command
uri:
url: "{{ new_re.azure_asyncoperation }}"
method: GET
status_code: 200
headers:
Authorization: "Bearer {{ token }}"
register: new_re_result
until: new_re_result.json.status == "Succeeded"
delay: "{{ var_delay }}"
retries: "{{ var_retries }}"
I have tried different solutions in the until property but nothing works. From this ansible code, i get the error below:
{
"msg": "The conditional check 'new_re_result.json.status == \"Succeeded\"' failed. The error was: error while evaluating conditional (new_pe_result.json.status == \"Succeeded\"): 'dict object' has no attribute 'status'",
"_ansible_no_log": false
}
Any idea as a solution?

Related

Creating a basic REST API in PolicyCenter

I am trying to create a basic HelloWorld REST API in PolicyCenter. I am getting an Internal Server Error from the GET /apis's endpoint (from the com.guidewire.pl.rest.docs.ApiListGenerator) and my new endpoint returns 404 Not Found. I am not sure what is missing and I don't see any clues in the logs. Any hints are welcome.
GET http://localhost:8180/pc/rest/apis
{
"status": 500,
"errorCode": "java.lang.IllegalArgumentException",
"cause": {
"class": "java.lang.IllegalArgumentException",
"message": "Keys in JsonObject cannot be null"
},
"stackTrace": [
"gw.api.json.JsonObject.validateKey(JsonObject.java:898)",
"gw.api.json.JsonObject.put(JsonObject.java:849)",
"com.guidewire.pl.rest.docs.ApiListGenerator.lambda$createApiListJson$2(ApiListGenerator.java:37)",
"java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)",
...
GET http://localhost:8180/pc/rest/helloworld
{
"status": 404,
"errorCode": "gw.api.rest.exceptions.NotFoundException",
"userMessage": "No resource was found at path /helloworld"
}
[modules\configuration\gsrc\myorg\pc\integration\restapi\helloworld\HelloWorldHandler.gs]
package myorg.pc.integration.restapi.helloworld
class HelloWorldHandler {
public function getHelloWorld() : String {
return 'Hello, World!'
}
}
[modules\configuration\config\integration\apis\myorg\pc\helloworld\helloworld-1.0.swagger.yaml]
swagger: "2.0"
info:
version: "1.0"
title: "Hello World API"
x-gw-apihandlers:
- myorg.pc.integration.restapi.helloworld.HelloWorldHandler
paths:
/helloworld:
get:
summary: "Says 'Hello World'"
description: "Says 'Hello World'"
operationId: getHelloWorld
produces:
- text/plain
responses:
'200':
description: |
Successful operation
schema:
type: string
[modules\configuration\config\integration\apis\published-apis.yaml]
apis:
- name: myorg.pc.helloworld.helloworld-1.0
defaultTemplate:
- name: gw.pl.framework.dev_template-1.0
After some digging I was able to find the documentation on the Swagger format, which mentioned basePath as a required property. After adding this, my API started working (with a different URL).

Azure Ansible - Doesn't fetch IPConfiguration Public IP Method

When I try to fetch the IP configuration of the network interface, I see that public_ip_allocation_method is set to NULL even though it is dynamic in Azure portal. I even tested by creating a new IP yet it shows NULL.
As a result I'm unable to modify network interface settings like Security Group, I get the error value of public_ip_allocation_method must be one of: Dynamic, Static, got: None found in ip_configurations
- name: Applying NSG to target NIC
azure_rm_networkinterface:
name: "{{ azure_vm_network_interface }}"
resource_group: "{{ resource_group }}"
subnet_name: "{{ azure_network_interface_info.networkinterfaces[0].subnet }}"
virtual_network: "{{ azure_network_interface_info.networkinterfaces[0].virtual_network.name }}"
ip_configurations: "{{ azure_network_interface_info.networkinterfaces[0].ip_configurations }}"
security_group: "/subscriptions/123456/resourceGroups/test-resource-group/providers/Microsoft.Network/networkSecurityGroups/testing_temp_8"
Output:
"ip_configurations": [
{
"application_gateway_backend_address_pools": null,
"application_security_groups": null,
"load_balancer_backend_address_pools": null,
"name": "Ubuntu915",
"primary": true,
"private_ip_address": "10.0.0.5",
"private_ip_address_version": "IPv4",
"private_ip_allocation_method": "Dynamic",
"public_ip_address": "/subscriptions/123456789/resourceGroups/test-resource-group/providers/Microsoft.Network/publicIPAddresses/Ubuntu-915-test",
"public_ip_address_name": "/subscriptions/123456789/resourceGroups/test-resource-group/providers/Microsoft.Network/publicIPAddresses/Ubuntu-915-test",
"public_ip_allocation_method": null
}
],

queryStringParameters in mock-server

I have the following url:
http://10.11.100.163:1080/v1/publish/?category=conf&product=UFED_INFIELD&serial=123&productGUIDs%5B0%5D%5Bproduct%5D=GLOBAL&productGUIDs%5B0%5D%5Bguid%5D=undefinedblabla&productGUIDs%5B1%5D%5Bproduct%5D=UFED_INFIELD&productGUIDs%5B1%5D%5Bguid%5D=undefinedblabla
As you can see there are several parameters that are formed by two names, like "productGUIDs%5B0%5D%5Bproduct%5D=GLOBAL" and this is equal to "productGUIDs[0][product]=GLOBAL"
now in the expectation file on the mock-server I am trying to create the request but without success until now.
this is what I wrote in the expectation file:
await mockServerClient(process.env.mockServerAddress , process.env.mockServerPort).setDefaultHeaders(defaultResponseHeaders, []).mockAnyResponse({
"httpRequest":{
"method": "GET",
"path": "/v1/publish",
"queryStringParameters": {
"category":["conf"],
"product":["UFED_INFIELD"],
"serial":["123"],
"productGUIDs%5B0%5D%5Bproduct%5D" : ["GLOBAL"],
"productGUIDs%5B0%5D%5Bguid%5D" : ["undefinedblabla"],
"productGUIDs%5B1%5D%5Bproduct%5D" : ["UFED_INFIELD"],
"productGUIDs%5B1%5D%5Bguid%5D" : ["undefinedblabla"],
}
},
when sending the request (GET) with POSTMAN, I get 404, means, the mock-server does not recognize the request.
any advice of how to write the query string parameters in the expectation file will be really appreaciated
The correct queryStringParameters syntax is an array of objects with name/values keys, like this:
"queryStringParameters": [
{
"name": "category",
"values": ["conf"]
},
{
"name": "product",
"values": ["UFED_INFIELD"]
},
{
"name": "productGUIDs%5B0%5D%5Bproduct%5D",
"values": ["GLOBAL"]
}
]
Here an example of an expectation in a file.yaml for a POST request with queryStringParameters. To adapt it to the GET method just delete the body and change "POST" by "GET" :
times:
remainingTimes: 1
unlimited: false
httpRequest:
method: POST
path: /accounts/login
queryStringParameters:
urlparameter1: 'value1'
body:
type: PARAMETERS
parameters:
username: myusername
password: mypassword
httpResponse:
body: {
"access_token": "e55etg9c-167e-4841-b0r3-y8fe5d1t7568",
"token_type": "bearer",
"redirectUrl": "/menu/clothes"
}
statusCode: 200
reasonPhrase: OK
The indexation is very important in the .yaml file, so be careful to have the good format for each element, otherwise it won't work.
You can find here the documentation to do expectations :
https://www.mock-server.com/mock_server/creating_expectations.html#button_match_request_by_negative_priority
Maybe you'll find your answer in the example "match request by path parameter and query parameter" in the link above.

How to get public IP from Ansible azure_rm_publicipaddress_facts module into a variable to use with add_host?

I'm trying to set up a pipeline for Azure automation with Ansible, all working, except when I try to get the public IP address into a variable to use with add_host.
Here is my example task:
---
- name: Get Public IP
azure_rm_publicipaddress_facts:
resource_group: '{{ my_resource_group }}'
name: '{{ my_name }}'
register: azure_ip
- debug: var=azure_ip verbosity=2
- name: Add new instance to host group
add_host:
hostname: '{{ item.ipAddress }}'
groupname: launched
with_items: azure_ip.azure_publicipaddresses
With this giving me the following error:
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined.
However the value is there as shown :
TASK [azure : debug] ***********************************************************
task path: mytest/roles/azure/tasks/get_ip.yml:14
ok: [localhost] => {
"azure_ip": {
"ansible_facts": {
"azure_publicipaddresses": [
{
"etag": “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"id": "/subscriptions/0000000000000/resourceGroups/myrgrp/providers/Microsoft.Network/publicIPAddresses/my_ip_001",
"location": “euwest",
"name": “my_ip_001",
"properties": {
"idleTimeoutInMinutes": 4,
"ipAddress": “20.113.125.63",
"ipConfiguration": {
"id": "/subscriptions/000000000000/resourceGroups/mygrprgrp/providers/Microsoft.Network/networkInterfaces/myrgrp-nic001/ipConfigurations/default"
},
"provisioningState": "Succeeded",
"publicIPAddressVersion": "IPv4",
"publicIPAllocationMethod": "Dynamic",
"resourceGuid": “fffff-4444444-cccccc"
},
"type": "Microsoft.Network/publicIPAddresses"
}
]
},
"changed": false
}
}
So, I guess I'm missing something, could anybody point me to the right direction?
Change:
- name: Add new instance to host group
add_host:
hostname: '{{ item.ipAddress }}'
groupname: launched
with_items: azure_ip.azure_publicipaddresses
To:
- name: Add new instance to host group
add_host:
hostname: "{{ item.properties.ipAddress }}"
groupname: launched
with_items: "{{ azure_ip.ansible_facts.azure_publicipaddresses }}"
with with_items you need to include the variable in "{{ }}" since a few Ansible versions
key names you referenced seem different from the debug output

Ansible: How to use an Azure RM template in `azure_rm_deployment` module?

I have a virtual machine ARM template built in the following way: refernce
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
"contentVersion": "1.0.0.0",
"parameters": { },
"variables": { },
"resources": [ ],
"outputs": { }
}
with parameters.json:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUserName": { "value": "mytestacct1" },
"adminPassword": { "value": "mytestpass1" }
}
}
I can successfully deploy the machine using this template in PowerShell:
New-AzureRmResourceGroupDeployment -ResourceGroupName $rgName -TemplateFile VirtualMachineTemplate.json -TemplateParameterFile Parameters.json
However, if I try to use the same template for Ansible azure_rm_deployment module in the following task:
- name: Ensure the VM is deployed to Azure
azure_rm_deployment:
state: present
resource_group_name: "{{ resource_group_name }}"
template: "{{ lookup('file', 'VirtualMachineTemplate.json') }}"
parameters: "{{ lookup('file', 'Parameters.json') }}"
I get an error:
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "failed_deployment_operations": [], "msg": "Deployment failed with status code: 400 and message: The request content was invalid and could not be deserialized: 'Error converting value \"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json\" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Data.Definitions.DeploymentParameterDefinition'. Path 'properties.parameters.$schema', line 1, position 142.'."}
The error is caused by parameters.json. If I define the parameters directly in the task:
- name: Ensure the VM is deployed to Azure
azure_rm_deployment:
state: present
resource_group_name: "{{ resource_group_name }}"
template: "{{ lookup('file', 'VirtualMachineTemplate.json') }}"
parameters:
adminUserName:
value: mytestacct1
adminPassword:
value: mytestpass1
It deploys the machine.
I'm at loss here. Is there a modification required to the template for the Ansible module?
Notes:
At the same time I can provision resources and VMs using azure_rm_storageaccount, azure_rm_virtualmachine, etc. modules, so I guess it's not a library problem; at least not the Microsoft Azure SDK for Python, which is 2.0.0rc5 per requirements.
Just to make sure I also tried with template_link and parameters_link and the error message is the same.
I see this is rather old question, but I note that there's own field parameters in the parameters file, so the right call should be:
- name: Ensure the VM is deployed to Azure
azure_rm_deployment:
state: present
resource_group_name: "{{ resource_group_name }}"
template: "{{ lookup('file', 'VirtualMachineTemplate.json') }}"
parameters: "{{ (lookup('file', 'Parameters.json') | from_json).parameters }}"

Resources