Trouble with ec2_vpc_subnet module - python

I'm trying to run the following play
- name: Create subnet for database servers
ec2_vpc_subnet:
state: present
region: "us-east-1"
vpc_id: "vpc-abcd1234"
cidr: "10.10.10.0/28"
resource_tags:
Name: "Subnet"
register: subnet
Every time I run the playbook the subnet recreates
Here is my -vvvv output
changed: [localhost] => {
"changed": true,
"invocation": {
"module_args": {
"aws_access_key": null,
"aws_secret_key": null,
"az": "us-east-1b",
"cidr": "10.0.10.0/28",
"ec2_url": null,
"profile": null,
"region": "us-east-1",
"resource_tags": {
"Name": "subnet"
},
"security_token": null,
"state": "present",
"tags": {
"Name": "subnet"
},
"validate_certs": true,
"vpc_id": "vpc-abcd1234"
},
"module_name": "ec2_vpc_subnet"
},
"subnet": {
"availability_zone": "us-east-1b",
"available_ip_address_count": 11,
"cidr_block": "10.0.10.0/28",
"default_for_az": "false",
"id": "subnet-cc399de1",
"map_public_ip_on_launch": "false",
"state": "available",
"tags": {
"Name": "subnet"
},
"vpc_id": "vpc-abcd1234"
}
}
Is anyone else having this issue? It would be great to build subnets outside of ec2_vpc module and have them be stable.
Thanks

I solved this issue myself. I had been using ec2_vpc module higher up the playbook to deploy AWS VPC, IGW, & subnets. I moved my Task to ec2_vpc and adjusted the downstream keys.
My ec2_vpc_subnet tasks no longer change after runs
Thanks

Related

Is there a way to set public IP to delete when Azure VM is deleted using Python REST API?

I would like to Delete a VM with its attached resources including Network Interfaces, data disks, OS Disk and Public IP when a certain URL is called with a delete method:
http://servername.com/azr/vm_name/subscription_id/resource_group
However, I'm having some issues attaching the Public IP For deletion.
I'm doing this by:
Calling Azure GET VM API to get the VM and resource details and saving the response JSON into a variable "response_json"
{
"name": "TestAZR",
"id": "/subscriptions/xx-xxx-xx/resourceGroups/xxx-xxx-xx/providers/Microsoft.Compute/virtualMachines/TestAZR",
"type": "Microsoft.Compute/virtualMachines",
"location": "eastus",
"properties": {
"vmId": "xxx-xxx-xxx",
"hardwareProfile": {
"vmSize": "Standard_DS1_v2"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2016-datacenter-gensecond",
"version": "latest"
},
"osDisk": {
"osType": "Windows",
"name": "TestAZR_disk1",
"createOption": "FromImage",
"caching": "ReadWrite",
"managedDisk": {
"storageAccountType": "xx",
"id": "/subscriptions/xx-xxx/resourceGroups/xx=xxx=x/providers/Microsoft.Compute/disks/TestAZR_disk1
},
"deleteOption": "Detach",
"diskSizeGB": 127
},
"dataDisks": [
{
"lun": 0,
"name": "TestAZR_DataDisk_0",
"createOption": "Attach",
"caching": "ReadOnly",
"writeAcceleratorEnabled": false,
"managedDisk": {
"storageAccountType": "Premium_LRS",
"id": "/subscriptions/xx-xxx-/resourceGroups/xx-xxx-x/providers/Microsoft.Compute/disks/TestAZR_DataDisk_0"
},
"deleteOption": "Detach",
"diskSizeGB": 1024,
"toBeDetached": false
},
{
"lun": 1,
"name": "TestAZR_DataDisk_1",
"createOption": "Attach",
"caching": "ReadOnly",
"writeAcceleratorEnabled": false,
"managedDisk": {
"storageAccountType": "Premium_LRS",
"id": "/subscriptions/xx-xxx-x/resourceGroups/xx-xxx-xx/providers/Microsoft.Compute/disks/TestAZR_DataDisk_1"
},
"deleteOption": "Detach",
"diskSizeGB": 1024,
"toBeDetached": false
}
]
},
"osProfile": {
"computerName": "TestAZR",
"adminUsername": "AAAA",
"windowsConfiguration": {
"provisionVMAgent": true,
"enableAutomaticUpdates": true,
"patchSettings": {
"patchMode": "AutomaticByOS",
"assessmentMode": "ImageDefault",
"enableHotpatching": false
},
"enableVMAgentPlatformUpdates": false
},
"secrets": [],
"allowExtensionOperations": true,
"requireGuestProvisionSignal": true
},
"networkProfile": {
"networkInterfaces": [
{
"id": "/subscriptions/xx-xxx-x/resourceGroups/xx-xxx-x/providers/Microsoft.Network/networkInterfaces/testazrexecute35_z1",
"properties": {
"deleteOption": "Detach"
}
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true
}
},
"provisioningState": "Succeeded",
"timeCreated": "2022-09-15T19:04:13.2910488+00:00"
},
"zones": [
"1"
]
}
Modifying the response_json setting each "DeleteOption" with "Delete" so they get deleted with the VM
Sending the response_json in a PATCH API with the request.
Deleting VM with the DELETE VM API
This works for deleting VM along with Network Interface, Data Disks and OS Disk but Public IP doesn't seem to be deleted with Network Interface automatically.
So I tried this alternatively:
I use the GET Network interface API that contains the Public IP name inside:
{
"name": "testazr845_z1",
"id": "/subscriptions/xx-xxx-xx/resourceGroups/xx-xxx-xx/providers/Microsoft.Network/networkInterfaces/testazr845_z1",
"etag": "W/\"xx-xxx-xx\"",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "xx-xxx-",
"ipConfigurations": [
{
"name": "ipconfig1",
"id": "/subscriptions/xxx-xxxx-xx/resourceGroups/xxx-xxx-xx/providers/Microsoft.Network/networkInterfaces/testazr845_z1/ipConfigurations/ipconfig1",
"etag": "W/\"xxx-xxx-xx\"",
"type": "Microsoft.Network/networkInterfaces/ipConfigurations",
"properties": {
"provisioningState": "Succeeded",
"privateIPAddress": "",
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"name": "TestAZR-ip",
"id": "/subscriptions/xxx-xxx-x/resourceGroups/xx-xxx-xx/providers/Microsoft.Network/publicIPAddresses/TestAZR-ip",
"properties": {
"provisioningState": "Succeeded",
"resourceGuid": "xx-xxxx-xx",
"publicIPAddressVersion": "IPv4",
"publicIPAllocationMethod": "Dynamic",
"idleTimeoutInMinutes": 4,
"ipTags": [],
"ipConfiguration": {
"id": "/subscriptions/xxx-xxxx-x/resourceGroups/xx-xxx-xx/providers/Microsoft.Network/networkInterfaces/testazr845_z1/ipConfigurations/ipconfig1"
},
"deleteOption": "Detach"
},
"type": "Microsoft.Network/publicIPAddresses",
"sku": {
"name": "Basic",
"tier": "Regional"
}
},
"subnet": {
"id": "/subscriptions/xx-xxxx-xx/resourceGroups/VPCxAzrDev/providers/Microsoft.Network/virtualNetworks/xx-xx/subnets/alphatest_ase_subnet"
},
"primary": true,
"privateIPAddressVersion": "IPv4"
}
}
],
"dnsSettings": {
"dnsServers": [],
"appliedDnsServers": [],
"internalDomainNameSuffix": ""
},
"macAddress": "",
"enableAcceleratedNetworking":,
"vnetEncryptionSupported":,
"enableIPForwarding":,
"primary":,
"virtualMachine": {
"id": "/subscriptions/xx-xxx-xx/resourceGroups/xxx-xxx/providers/Microsoft.Compute/virtualMachines/TestAZR"
},
"hostedWorkloads": [],
"tapConfigurations": [],
"nicType": "Standard",
"allowPort25Out": true
},
"type": "Microsoft.Network/networkInterfaces",
"location": "eastus",
"kind": "Regular"
}
I save that response into a variable json "network_interface_json", and in it I modify it with {"DeleteOption" : "Delete"} for the Public IP
I send this json in the PATCH API that's listed for deleting resources with VM
https://learn.microsoft.com/en-us/azure/virtual-machines/delete?tabs=rest2%2Ccli3%2Cportal4%2Cportal5
However I'm getting the following error message when sending this request:
{
"error": {
"code": "LinkedAuthorizationFailed",
"message": "The client '123124-12312-xx' with object id '1231-xx-xx' has permission to perform action 'Microsoft.Network/networkInterfaces/write' on scope '/subscriptions/xxx-xx-xx/resourceGroups/xx-xx-x/providers/Microsoft.Network/networkInterfaces/AZRTEST; however, it does not have permission to perform action 'Microsoft.Network/virtualNetworks/subnets/join/action' on the linked scope(s) '/subscriptions/xx-xxx-x/resourceGroups/VPCxAzureDevelopment/providers/Microsoft.Network/virtualNetworks/VPCxAzureDevelopment-eastus/subnets/xxx-xxx-subnet' or the linked scope(s) are invalid."
}
}
So it seems to be a permissions issue? Or maybe the json I'm using isn't correct to send for PATCH API? I've tried removing the subnet part but this doesn't seem to work, open to any suggestions.

Parse JSON .txt file using RegEx in Python

I need help with understanding how to parse json text for my assignment, and I can't use JSON library as it is not permitted, hence only 're'.
This is part of the file (wrapped for legibility, originally it's all on one line):
{
"unit": {
"title": "Web Development courses",
"raw_title": "{} courses",
"source_objects": [{
"type": "subcategory",
"id": 8,
"description": "Learn web development skills to build fully functioning websites.",
"url": "/courses/development/web-development/",
"title": "Web Development"
}],
"item_type": "course",
"items": [{
"_class": "course",
"id": 625204,
"title": "The Web Developer Bootcamp 2021",
"url": "/course/the-web-developer-bootcamp/",
"is_paid": true,
"visible_instructors": [{
"image_100x100": "https://img-c.udemycdn.com/user/100x100/4466306_6fd8_3.jpg",
"id": 4466306,
"url": "/user/coltsteele/",
"initials": "CS",
"display_name": "Colt Steele",
"image_50x50": "https://img-c.udemycdn.com/user/50x50/4466306_6fd8_3.jpg",
"job_title": "Developer and Bootcamp Instructor",
"name": "Colt",
"_class": "user",
"title": "Colt Steele"
}],
"image_125_H": "https://img-c.udemycdn.com/course/125_H/625204_436a_3.jpg",
"image_240x135": "https://img-c.udemycdn.com/course/240x135/625204_436a_3.jpg",
"is_practice_test_course": false,
"image_480x270": "https://img-c.udemycdn.com/course/480x270/625204_436a_3.jpg",
"published_title": "the-web-developer-bootcamp",
"tracking_id": "2zX-e8qKRUGgpwfF5AQx3w",
"headline": "COMPLETELY REDONE - The only course you need to learn web development - HTML, CSS, JS, Node, and More!",
"num_subscribers": 713744,
"avg_rating": 4.6973553,
"avg_rating_recent": 4.6947985,
"rating": 4.6947985,
"num_reviews": 215075,
"is_wishlisted": false,
"num_published_lectures": 610,
"num_published_practice_tests": 0,
"image_50x50": "https://img-c.udemycdn.com/course/50x50/625204_436a_3.jpg",
"image_100x100": "https://img-c.udemycdn.com/course/100x100/625204_436a_3.jpg",
"image_304x171": "https://img-c.udemycdn.com/course/304x171/625204_436a_3.jpg",
"image_750x422": "https://img-c.udemycdn.com/course/750x422/625204_436a_3.jpg",
"is_in_user_subscription": false,
"locale": {
"english_title": "English (US)",
"locale": "en_US",
"_class": "locale",
"simple_english_title": "English",
"title": "English (US)"
},
"has_closed_caption": true,
"caption_languages": ["English [Auto]", "French [Auto]", "German [Auto]", "Italian [Auto]", "Polish [Auto]", "Portuguese [Auto]", "Spanish [Auto]"],
"created": "2015-09-28T21:32:19Z",
"instructional_level": "All Levels",
"instructional_level_simple": "All Levels",
"content_length_practice_test_questions": 0,
"is_user_subscribed": false,
"buyable_object_type": "course",
"published_time": "2015-11-02T21:13:27Z",
"objectives_summary": ["The ins and outs of HTML5, CSS3, and Modern JavaScript for 2021", "Make REAL web applications using cutting-edge technologies", "Create responsive, accessible, and beautiful layouts"],
"is_recently_published": false,
"last_update_date": "2021-09-09",
"preview_url": "/course/625204/preview/",
"learn_url": "/course/the-web-developer-bootcamp/learn/",
"predictive_score": null,
"relevancy_score": null,
"input_features": null,
"lecture_search_result": null,
"curriculum_lectures": [],
"order_in_results": null,
"curriculum_items": [],
"instructor_name": null,
"content_info": "63.5 total hours",
"content_info_short": "63.5 hours",
"bestseller_badge_content": null,
"badges": [],
"free_course_subscribe_url": null,
"context_info": {
"subcategory": null,
"category": {
"tracking_object_type": "cat",
"id": 288,
"url": "/courses/development/",
"title": "Development"
},
"label": {
"id": 8322,
"display_name": "Web Development",
"topic_channel_url": "/topic/web-development/",
"url": "/topic/web-development/",
"title": "Web Development",
"tracking_object_type": "cl"
}
}
},
{
"_class": "course",
"title": "etc",
}]
}
}
I have about 200 more lines like these, each filled with course/student etc etc info and I need to extract info from certain keywords from this json file.
For example, for course information, I need to extract course_id, 100x100 img, course_title, subscribers etc, and similarly for students, and instructors I need to extract some more information.
My questions are:
since the three classes (course, students, instructors) share some keywords like ID, how do I pull courseID, studentID, instructorID separately?
for course ID,
I checked another link on stackoverflow and tried this but it didn't work so please help me out with this:
import re, os
raw_data_read = open('raw_data.txt', 'r')
regex_pattern = r'.*id\":\"([^\"]+)\".*'
match = re.match(regex_pattern, str(raw_data_read))
courseId= match.group(1)
print('courseId: {}'.format(courseId))

Web3.py web3.exceptions.ContractLogicError: execution reverted on getAmountOutMin and swap functions

Essentially I am trying to use a token swap function for web3.py and upon calling the function, it is giving me a ContractLogic Error: execution reverted which occurs on both getAmountOutMin as well as the swap function. I have looked thoroughly through the docs and other posts and I am unsure as to why this code is reproducing this error. I would appreciate it if someone could find a solution to this.
Here is the code used:
uniswap_factory_abi = json.loads('''[
{
"inputs": [
{
"internalType": "address",
"name": "_tokenIn",
"type": "address"
},
{
"internalType": "address",
"name": "_tokenOut",
"type": "address"
},
{
"internalType": "uint256",
"name": "_amountIn",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_amountOutMin",
"type": "uint256"
},
{
"internalType": "address",
"name": "_to",
"type": "address"
}
],
"name": "swap",
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_tokenIn",
"type": "address"
},
{
"internalType": "address",
"name": "_tokenOut",
"type": "address"
},
{
"internalType": "uint256",
"name": "_amountIn",
"type": "uint256"
}
],
"name": "getAmountOutMin",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
}]''')
actual_min = float(min)
amountout = contract.functions.getAmountOutMin("0xd0A1E359811322d97991E03f863a0C30C2cF029C", "0xF6fF8efc1e11A17E410F248475ffE870E8FC6ebD", int(actual_min * 1e18))
txn = {
'from': account,
'gas': 650000,
'gasPrice': web3.eth.gasPrice,
'nonce': web3.eth.getTransactionCount(account)
}
amountoutmin = amountout.call(txn)
token_swap = contract.functions.swap("0xd0A1E359811322d97991E03f863a0C30C2cF029C", "0xF6fF8efc1e11A17E410F248475ffE870E8FC6ebD", int(actual_min * 1e18), amountoutmin, account)
token_swap.call(txn)
edit:
After using solcx to compile the solidity code, it has solved my issue
edit2:
Apparently using solcx to compile the solidity code did not solve the issue, however I have now found out that the solidity code that I have been using for this has been faulty, which is the source of my problems
I would try to check first if your contract deployed successfully and then see if the functions you use work fine too. Not sure what you used to deploy it, but in Remix for example you can use the web interface to test your functions and there's a nice dubug feature as well. Type in the values there, check the output and if it works then move to the code part. My guess is that your txn might be missing some parameters like chainId, value, etc. Do you sign the transaction too?

Azure Function successfully published but missing from Azure Portal

My Function app is not showing up in the Portal though it publishes successfully from local machine.
Example:
Success!:
Failure:
Tried:
Visiting https:myapp.azurewebsites.net
Visiting https://myapp.scm.azurewebsites.net/api/deployments
Last deployment looks good:
{
"id": "<redacted>",
"status": 4,
"status_text": "",
"author_email": "N/A",
"author": "<redacted>",
"deployer": "Push-Deployer",
"message": "Created via a push deployment",
"progress": "",
"received_time": "2020-10-23T20:38:47.1069702Z",
"start_time": "2020-10-23T20:38:47.3075338Z",
"end_time": "2020-10-23T20:39:19.3128742Z",
"last_success_end_time": "2020-10-23T20:39:19.3128742Z",
"complete": true,
"active": true,
"is_temp": false,
"is_readonly": true,
"url": "https://myapp.scm.azurewebsites.net/api/deployments/5e446e26fc934a75b4ab7241ce589e9f",
"log_url": "https://myapp.scm.azurewebsites.net/api/deployments/5e446e26fc934a75b4ab7241ce589e9f/log",
"site_name": "myapp"
}
Visiting https://myapp.scm.azurewebsites.net/api/deployments/5e446e26fc934a75b4ab7241ce589e9f/log
Steps looks good:
[
{
"log_time": "2020-10-23T20:38:47.2134775Z",
"id": "a9bfd9e3-dee2-4bfc-9e82-9b003d3df991",
"message": "Updating submodules.",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:38:47.2820867Z",
"id": "e980bad8-ee6a-467f-98ea-c78135e29eeb",
"message": "Preparing deployment for commit id '5e446e26fc'.",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:38:47.4606839Z",
"id": "961ce7eb-2388-4a36-9646-32a7455fce10",
"message": "Repository path is /tmp/zipdeploy/extracted",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:38:47.5141536Z",
"id": "1f1ad9b0-d5bf-41f0-ad77-537c2da9a010",
"message": "Running oryx build...",
"type": 1,
"details_url": "https://myapp.scm.azurewebsites.net/api/deployments/5e446e26fc934a75b4ab7241ce589e9f/log/1f1ad9b0-d5bf-41f0-ad77-537c2da9a010"
},
{
"log_time": "2020-10-23T20:39:14.3176646Z",
"id": "45fc756f-4873-469c-8adf-7b8968ceec95",
"message": "Writing the artifacts to a Zip file",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:39:18.8047683Z",
"id": "3f9b3434-dcbc-496e-8480-d3bff123159e",
"message": "Running post deployment command(s)...",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:39:18.9330242Z",
"id": "11e6d453-3aa0-4afd-a20e-7edc6369d774",
"message": "Triggering recycle (preview mode disabled).",
"type": 0,
"details_url": null
},
{
"log_time": "2020-10-23T20:39:19.0915157Z",
"id": "43df8884-3066-434b-91ed-6b2bbfac6236",
"message": "Deployment successful.",
"type": 0,
"details_url": null
}
]
I even created a whole new Function App (in the same resource group, using the same App Service Plan) and tried publishing the Function to it... no go.
I have 6 or 7 python Functions in prod, this is not my first deployment.
Is this a known issue that is being worked on? How do I get the Function to be visible in Portal?
I was able to determine the issue. It was my fault.
SSH into Function app via https://myFunctionName.scm.azurewebsites.net/webssh/host
ls /home/site/wwwroot/myFunctionName
The main python filename had a space " " in the filename
Was: __init__ .py
Should have been: __init__.py
Fixed this and republished

Python & JSON Phrasing issues

I am having issues phrasing the TwitchAPI JSON. I am trying to read the name which is under multiple layers (not Sure of the correct term for this).
Here is part of the API JSON:
{
"_links": {
"next": "https://api.twitch.tv/kraken/channels/test_user/follows?direction=DESC&limit=25&offset=25",
"self": "https://api.twitch.tv/kraken/channels/test_user/follows?direction=DESC&limit=25&offset=0"
},
"_total": 336,
"follows": [
{
"_links": {
"self": "https://api.twitch.tv/kraken/users/test_follower/follows/channels/test_user"
},
"created_at": "2014-07-24T20:21:10Z",
"user": {
"_id": 00000001,
"_links": {
"self": "https://api.twitch.tv/kraken/users/test_follower"
},
"bio": null,
"created_at": "2014-07-05T17:27:45Z",
"display_name": "test_follower",
"logo": null,
"name": "test_follower",
"type": "user",
"updated_at": "2014-07-24T20:20:29Z"
}
},
Etc, it continues with multiple name values I wish to collect.
How do I get the name item? This is my current attempt:
print [data['name'] for data in data['follows']['user']]
But this just gives the error:
TypeError: list indices must be integers, not str
data['follows'] is a list, you can't use ['user'] to get the element in this list.
You need a loop or using data['follows'][0] to get
{
"_links": {
"self": "https://api.twitch.tv/kraken/users/test_follower/follows/channels/test_user"
},
"created_at": "2014-07-24T20:21:10Z",
"user": {
"_id": 00000001,
"_links": {
"self": "https://api.twitch.tv/kraken/users/test_follower"
},
"bio": null,
"created_at": "2014-07-05T17:27:45Z",
"display_name": "test_follower",
"logo": null,
"name": "test_follower",
"type": "user",
"updated_at": "2014-07-24T20:20:29Z"
}
}
So, data['follows'][0]['user'] will get you
"user": {
"_id": 00000001,
"_links": {
"self": "https://api.twitch.tv/kraken/users/test_follower"
},
"bio": null,
"created_at": "2014-07-05T17:27:45Z",
"display_name": "test_follower",
"logo": null,
"name": "test_follower",
"type": "user",
"updated_at": "2014-07-24T20:20:29Z"
}
then you append [name] after it to get the name of the user.
So the answer is: print data['follows'][0]['user']['name']
or
print [data['user']['name'] for data in data['follows']]
The for loop is not correct even if you change data['follows']['user'] to data['follows'][0]['user'], since data['name'] is not valid.
====I CANNOT COMMENT ON ANSWERS=======
The other answer is not correct because there's no 'name' in data['follows']
I think that is something like:
print [data['name'] for data in data['follows']]
I hope this helps

Categories

Resources