search json file with python script

Milister asked:

I have following tfstate file (it has JSON structure)

{
    "version": 3,
    "terraform_version": "0.11.1",
    "serial": 1,
    "lineage": "4d050905-8f3d-46df-8bbb-8859c708abe7",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {
                "id": {
                    "sensitive": false,
                    "type": "list",
                    "value": [
                        "34.244.138.72",
                        "34.249.95.222"
                    ]
                }
            },
            "resources": {
                "aws_instance.win-example.0": {
                    "type": "aws_instance",
                    "depends_on": [
                        "aws_security_group.DJukes"
                    ],
                    "primary": {
                        "id": "i-0f8c0fcb36f58947d",
                        "attributes": {
                            "ami": "ami-cc821eb5",
                            "associate_public_ip_address": "true",
                            "availability_zone": "eu-west-1a",
                            "disable_api_termination": "false",
                            "ebs_block_device.#": "0",
                            "ebs_optimized": "false",
                            "ephemeral_block_device.#": "0",
                            "iam_instance_profile": "",
                            "id": "i-0f8c0fcb36f58947d",
                            "instance_state": "running",
                            "instance_type": "t2.medium",
                            "ipv6_addresses.#": "0",
                            "key_name": "",
                            "monitoring": "false",
                            "network_interface.#": "0",
                            "network_interface_id": "eni-8dbf17be",
                            "placement_group": "",
                            "primary_network_interface_id": "eni-8dbf17be",
                            "private_dns": "ip-172-31-30-138.eu-west-1.compute.internal",
                            "private_ip": "172.31.30.138",
                            "public_dns": "ec2-34-244-138-72.eu-west-1.compute.amazonaws.com",
                            "public_ip": "34.244.138.72",
                            "root_block_device.#": "1",
                            "root_block_device.0.delete_on_termination": "true",
                            "root_block_device.0.iops": "100",
                            "root_block_device.0.volume_id": "vol-0dcfa45e001323eac",
                            "root_block_device.0.volume_size": "30",
                            "root_block_device.0.volume_type": "gp2",
                            "security_groups.#": "1",
                            "security_groups.4272775738": "DJukes",
                            "source_dest_check": "true",
                            "subnet_id": "subnet-5b50a512",
                            "tags.%": "1",
                            "tags.Name": "DJukes-1",
                            "tenancy": "default",
                            "user_data": "fad67bb12b32d15c3f5156bf7bdd830d4e084c6f",
                            "volume_tags.%": "0",
                            "vpc_security_group_ids.#": "1",
                            "vpc_security_group_ids.2187737336": "sg-fd5adf87"
                        },
                        "meta": {
                            "e2bfb730-ecaa-11e6-8f88-34363bc7c4c0": {
                                "create": 600000000000,
                                "delete": 600000000000,
                                "update": 600000000000
                            },
                            "schema_version": "1"
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                },
                "aws_instance.win-example.1": {
                    "type": "aws_instance",
                    "depends_on": [
                        "aws_security_group.DJukes"
                    ],
                    "primary": {
                        "id": "i-0bb714a7ddd376599",
                        "attributes": {
                            "ami": "ami-cc821eb5",
                            "associate_public_ip_address": "true",
                            "availability_zone": "eu-west-1a",
                            "disable_api_termination": "false",
                            "ebs_block_device.#": "0",
                            "ebs_optimized": "false",
                            "ephemeral_block_device.#": "0",
                            "iam_instance_profile": "",
                            "id": "i-0bb714a7ddd376599",
                            "instance_state": "running",
                            "instance_type": "t2.medium",
                            "ipv6_addresses.#": "0",
                            "key_name": "",
                            "monitoring": "false",
                            "network_interface.#": "0",
                            "network_interface_id": "eni-dab018e9",
                            "placement_group": "",
                            "primary_network_interface_id": "eni-dab018e9",
                            "private_dns": "ip-172-31-24-177.eu-west-1.compute.internal",
                            "private_ip": "172.31.24.177",
                            "public_dns": "ec2-34-249-95-222.eu-west-1.compute.amazonaws.com",
                            "public_ip": "34.249.95.222",
                            "root_block_device.#": "1",
                            "root_block_device.0.delete_on_termination": "true",
                            "root_block_device.0.iops": "100",
                            "root_block_device.0.volume_id": "vol-079b3491295b06249",
                            "root_block_device.0.volume_size": "30",
                            "root_block_device.0.volume_type": "gp2",
                            "security_groups.#": "1",
                            "security_groups.4272775738": "DJukes",
                            "source_dest_check": "true",
                            "subnet_id": "subnet-5b50a512",
                            "tags.%": "1",
                            "tags.Name": "DJukes-2",
                            "tenancy": "default",
                            "user_data": "fad67bb12b32d15c3f5156bf7bdd830d4e084c6f",
                            "volume_tags.%": "0",
                            "vpc_security_group_ids.#": "1",
                            "vpc_security_group_ids.2187737336": "sg-fd5adf87"
                        },
                        "meta": {
                            "e2bfb730-ecaa-11e6-8f88-34363bc7c4c0": {
                                "create": 600000000000,
                                "delete": 600000000000,
                                "update": 600000000000
                            },
                            "schema_version": "1"
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                },
                "aws_security_group.DJukes": {
                    "type": "aws_security_group",
                    "depends_on": [],
                    "primary": {
                        "id": "sg-fd5adf87",
                        "attributes": {
                            "description": "Managed by Terraform",
                            "egress.#": "1",
                            "egress.482069346.cidr_blocks.#": "1",
                            "egress.482069346.cidr_blocks.0": "0.0.0.0/0",
                            "egress.482069346.description": "",
                            "egress.482069346.from_port": "0",
                            "egress.482069346.ipv6_cidr_blocks.#": "0",
                            "egress.482069346.prefix_list_ids.#": "0",
                            "egress.482069346.protocol": "-1",
                            "egress.482069346.security_groups.#": "0",
                            "egress.482069346.self": "false",
                            "egress.482069346.to_port": "0",
                            "id": "sg-fd5adf87",
                            "ingress.#": "1",
                            "ingress.2601213444.cidr_blocks.#": "1",
                            "ingress.2601213444.cidr_blocks.0": "0.0.0.0/0",
                            "ingress.2601213444.description": "",
                            "ingress.2601213444.from_port": "0",
                            "ingress.2601213444.ipv6_cidr_blocks.#": "0",
                            "ingress.2601213444.protocol": "tcp",
                            "ingress.2601213444.security_groups.#": "0",
                            "ingress.2601213444.self": "false",
                            "ingress.2601213444.to_port": "6556",
                            "name": "DJukes",
                            "owner_id": "520150089049",
                            "revoke_rules_on_delete": "false",
                            "tags.%": "1",
                            "tags.Name": "allow-RDP",
                            "vpc_id": "vpc-8b23ccec"
                        },
                        "meta": {
                            "schema_version": "1"
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                }
            },
            "depends_on": []
        }
    ]

I want to extract public IP addresses to variables using python

"value": [
                        "34.244.138.72",
                        "34.249.95.222"
                    ]

So far, i have following code:

#!/bin/python
import json
import os.path
import shutil
from os import mkdir
from pprint import pprint
from python_terraform import *


json_data=open('./terraform.json')
data = json.load(json_data)

json_data.close()



for i in range (0, len (data['modules'])):
    print data['modules']['path']['outputs'][1]

And getting following error:

 File "./1.py", line 17, in <module>
    for i in range (0, len (data['modules']['path']['outputs'])):
TypeError: list indices must be integers, not str

also tried:

for dat in data:

  print dat['outputs']

My answer:


We can see from your JSON that modules is a list, which maps to an array with numeric indices, not a dictionary.

So you need to iterate over it with i.

See what you are actually receiving with something like:

for i in range (0, len (data['modules'])):
    pprint(data['modules'][i])

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.