Home > Software design >  Terraform Output via Jenkins
Terraform Output via Jenkins

Time:12-31

I'm unable to find vpc_id and public_subnet.0 in a terraform my-vpc output.

Every single time when I put below command:

ansible-playbook playbook1.yml -e "vpc_id=$(terraform output my-vpc.vpc_id) vpc_subnet_id=$(terraform output my-vpc.public_subnets.0)" -vvv

But it prints terraform output my-vpc

-bash-4.2$ terraform output my-vpc
{
  "azs" = tolist([
    "eu-west-1a",
    "eu-west-1b",
    "eu-west-1c",
  ])
  "cgw_arns" = []
  "cgw_ids" = []
  "database_internet_gateway_route_id" = ""
  "database_ipv6_egress_route_id" = ""
  "database_nat_gateway_route_ids" = []
  "database_network_acl_arn" = ""
  "database_network_acl_id" = ""
  "database_route_table_association_ids" = [
    "rtbassoc-0c0a897cce6cbac74",
    "rtbassoc-02c2f605eb9988418",
    "rtbassoc-0b14ef189c6c39da5",
  ]
  "database_route_table_ids" = [
    "rtb-038a86ea7eef50de1",
  ]
  "database_subnet_arns" = [
    "arn:aws:ec2:eu-west-1:008421212484:subnet/subnet-0ef73bc4e91557920",
    "arn:aws:ec2:eu-west-1:008421212484:subnet/subnet-0f044d3b21cecbca6",
    "arn:aws:ec2:eu-west-1:008421212484:subnet/subnet-042312717d7ed4fe6",
  ]
  "database_subnet_group" = "my-vpc"
  "database_subnet_group_name" = "my-vpc"
  "database_subnets" = [
    "subnet-0ef73bc4e91557920",
    "subnet-0f044d3b21cecbca6",
    "subnet-042312717d7ed4fe6",
  ]
  "database_subnets_cidr_blocks" = tolist([
    "10.0.21.0/24",
    "10.0.22.0/24",
    "10.0.23.0/24",
  ])
  "database_subnets_ipv6_cidr_blocks" = tolist([])
  "default_network_acl_id" = "acl-08f647113ea769f4f"
  "default_route_table_id" = "rtb-0b55d9919c1133316"
  "default_security_group_id" = "sg-050c394121822118f"
  "default_vpc_arn" = ""
  "default_vpc_cidr_block" = ""
  "default_vpc_default_network_acl_id" = ""
  "default_vpc_default_route_table_id" = ""
  "default_vpc_default_security_group_id" = ""
  "default_vpc_enable_dns_hostnames" = ""
  "default_vpc_enable_dns_support" = ""
  "default_vpc_id" = ""
  "default_vpc_instance_tenancy" = ""
  "default_vpc_main_route_table_id" = ""
  "dhcp_options_id" = ""
  "egress_only_internet_gateway_id" = ""
  "elasticache_network_acl_arn" = ""
  "elasticache_network_acl_id" = ""
  "elasticache_route_table_association_ids" = []
  "elasticache_route_table_ids" = [
    "rtb-0bbad3836a81d51cd",
  ]
  "elasticache_subnet_arns" = []
  "elasticache_subnet_group" = ""
  "elasticache_subnet_group_name" = ""
  "elasticache_subnets" = []
  "elasticache_subnets_cidr_blocks" = tolist([])
  "elasticache_subnets_ipv6_cidr_blocks" = tolist([])
  "igw_arn" = "arn:aws:ec2:eu-west-1:008421212484:internet-gateway/igw-006457c32d869d138"
  "igw_id" = "igw-006457c32d869d138"
  "intra_network_acl_arn" = ""
  "intra_network_acl_id" = ""
  "intra_route_table_association_ids" = []
  "intra_route_table_ids" = []
  "intra_subnet_arns" = []
  "intra_subnets" = []
  "intra_subnets_cidr_blocks" = tolist([])
  "intra_subnets_ipv6_cidr_blocks" = tolist([])
  "name" = "my-vpc"
  "nat_ids" = [
    "eipalloc-0a8b17a3d8101a5a0",
  ]
  "nat_public_ips" = tolist([
    "18.200.43.46",
  ])
  "natgw_ids" = [
    "nat-0c1f640cfa6bc1b9c",
  ]
  "outpost_network_acl_arn" = ""
  "outpost_network_acl_id" = ""
  "outpost_subnet_arns" = []
  "outpost_subnets" = []
  "outpost_subnets_cidr_blocks" = tolist([])
  "outpost_subnets_ipv6_cidr_blocks" = tolist([])
  "private_ipv6_egress_route_ids" = []
  "private_nat_gateway_route_ids" = [
    "r-rtb-0bbad3836a81d51cd1080289494",
  ]
  "private_network_acl_arn" = ""
  "private_network_acl_id" = ""
  "private_route_table_association_ids" = [
    "rtbassoc-046f37259950a7f95",
    "rtbassoc-0be769cfe2ebd6034",
    "rtbassoc-03dab517c9aa2789c",
  ]
  "private_route_table_ids" = [
    "rtb-0bbad3836a81d51cd",
  ]
  "private_subnet_arns" = [
    "arn:aws:ec2:eu-west-1:008421212484:subnet/subnet-038ffd52b102ad03d",
    "arn:aws:ec2:eu-west-1:008421212484:subnet/subnet-07fff3a676d8792b7",
    "arn:aws:ec2:eu-west-1:008421212484:subnet/subnet-09d7264428c109220",
  ]
  "private_subnets" = [
    "subnet-038ffd52b102ad03d",
    "subnet-07fff3a676d8792b7",
    "subnet-09d7264428c109220",
  ]
  "private_subnets_cidr_blocks" = tolist([
    "10.0.11.0/24",
    "10.0.12.0/24",
    "10.0.13.0/24",
  ])
  "private_subnets_ipv6_cidr_blocks" = tolist([])
  "public_internet_gateway_ipv6_route_id" = ""
  "public_internet_gateway_route_id" = "r-rtb-095c4a905b733cb521080289494"
  "public_network_acl_arn" = ""
  "public_network_acl_id" = ""
  "public_route_table_association_ids" = [
    "rtbassoc-08b42ea2c1b00f82c",
    "rtbassoc-06d913cc918b08721",
    "rtbassoc-04437e6c3b6deea45",
  ]
  "public_route_table_ids" = [
    "rtb-095c4a905b733cb52",
  ]
  "public_subnet_arns" = [
    "arn:aws:ec2:eu-west-1:008421212484:subnet/subnet-08dbadd83e2dfea89",
    "arn:aws:ec2:eu-west-1:008421212484:subnet/subnet-07b49f530d35a3899",
    "arn:aws:ec2:eu-west-1:008421212484:subnet/subnet-057153ea6183d363f",
  ]
  "public_subnets" = [
    "subnet-08dbadd83e2dfea89",
    "subnet-07b49f530d35a3899",
    "subnet-057153ea6183d363f",
  ]
  "public_subnets_cidr_blocks" = tolist([
    "10.0.1.0/24",
    "10.0.2.0/24",
    "10.0.3.0/24",
  ])
  "public_subnets_ipv6_cidr_blocks" = tolist([])
  "redshift_network_acl_arn" = ""
  "redshift_network_acl_id" = ""
  "redshift_public_route_table_association_ids" = []
  "redshift_route_table_association_ids" = []
  "redshift_route_table_ids" = tolist([
    "rtb-0bbad3836a81d51cd",
  ])
  "redshift_subnet_arns" = []
  "redshift_subnet_group" = ""
  "redshift_subnets" = []
  "redshift_subnets_cidr_blocks" = tolist([])
  "redshift_subnets_ipv6_cidr_blocks" = tolist([])
  "this_customer_gateway" = {}
  "vgw_arn" = ""
  "vgw_id" = ""
  "vpc_arn" = "arn:aws:ec2:eu-west-1:008421212484:vpc/vpc-081b13e816e14214c"
  "vpc_cidr_block" = "10.0.0.0/16"
  "vpc_enable_dns_hostnames" = true
  "vpc_enable_dns_support" = true
  "vpc_flow_log_cloudwatch_iam_role_arn" = ""
  "vpc_flow_log_destination_arn" = ""
  "vpc_flow_log_destination_type" = "cloud-watch-logs"
  "vpc_flow_log_id" = ""
  "vpc_id" = "vpc-081b13e816e14214c"
  "vpc_instance_tenancy" = "default"
  "vpc_ipv6_association_id" = ""
  "vpc_ipv6_cidr_block" = ""
  "vpc_main_route_table_id" = "rtb-0b55d9919c1133316"
  "vpc_owner_id" = "008421212484"
  "vpc_secondary_cidr_blocks" = tolist([])
}

I was trying with:

  • Different naming and paths.
  • Jenkins user privileges.
  • Destination of output.

CodePudding user response:

Outputs are meant to be human-readable:

The terraform output command by default displays in a human-readable format, which can change over time to improve clarity.

That means it's not really easy to get the values using the usual terraform syntax and you would need to use a tool like jq [1]. So in your example, that would have to be something like:

VPC_ID=$(terraform output -json my-vpc | jq -r '.vpc_id')
VPC_SUBNET_ID=$(terraform output -json my-vpc | jq -r '.public_subnets[0]')

Followed by:

ansible-playbook playbook1.yml -e vpc_id=$VPC_ID -e vpc_subnet_id=$VPC_SUBNET_ID -vvv

My Ansible skills are a bit rusty, and there might be better ways of doing it, but this should get you started.


[1] https://developer.hashicorp.com/terraform/cli/commands/output#use-in-automation

CodePudding user response:

Thaks to support of my coleuge we found a solution: jq ;)

ansible-playbook playbook1.yml -e "vpc_id=$(terraform output -json my-vpc|jq -r .vpc_id) vpc_subnet_id=$(terraform output -json my-vpc|jq -r .public_subnets[0])" -vvv
  • Related