ryanwholey / terraform-provider-pihole

A Terraform provider for managing Pi-hole resources
https://registry.terraform.io/providers/ryanwholey/pihole/latest/docs
Mozilla Public License 2.0
63 stars 8 forks source link

invalid character '<' looking for beginning of value #28

Closed rishabhkalra closed 2 years ago

rishabhkalra commented 2 years ago

I was just trying to automate DNS entry creation as I create VMs.

Error

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # pihole_dns_record.record will be created
  + resource "pihole_dns_record" "record" {
      + domain = "hoku-1"
      + id     = (known after apply)
      + ip     = "192.168.86.190"
    }

╷
│ Error: invalid character '<' looking for beginning of value
│
│   with pihole_dns_record.record,
│   on homelab.tf line 79, in resource "pihole_dns_record" "record":
│   79: resource "pihole_dns_record" "record" {
│
╵

Details

❯ tf --version
Terraform v1.1.5
on darwin_amd64
+ provider registry.terraform.io/hashicorp/random v3.1.0
+ provider registry.terraform.io/ryanwholey/pihole v0.0.9
+ provider registry.terraform.io/telmate/proxmox v2.9.3
# homelab.tf
resource "pihole_dns_record" "record" {
  domain = "hoku-1"
  ip     = "192.168.86.190"
}
# main.tf
terraform {
  backend "s3" {}

  required_providers {
    pihole = {
      source = "ryanwholey/pihole"
      version = "0.0.9"
    }
    proxmox = {
      source  = "telmate/proxmox"
      version = "2.9.3"
    }
  }
}

...

provider "pihole" {
  url = var.pihole_url
  password = var.pihole_password
}
# vars.tf
# variables passed in via a .env file
variable "pihole_url" {
  type = string
  description = "URL for pihole"
  sensitive = true
}

variable "pihole_password" {
  type = string
  description = "Password for pihole"
  sensitive = true
}

I've already made sure I'm using the admin password used for logging into pihole (as was stated in #22). Not entirely sure what's going on because I think I've set everything up correctly. I am using a traefik reverse proxy in front of pihole for HTTPS so the url I pass into that variable ends with /index.php?login. That was the only way I could get it to not error out trying to connect. I see the following error if I try to omit that bit.

# when using "https://pihole.internal.mydomain.dev" instead of "https://pihole.internal.mydomain.dev/index.php?login"
╷
│ Error: login failed: session ID not found in response
│
│   with provider["registry.terraform.io/ryanwholey/pihole"],
│   on main.tf line 22, in provider "pihole":
│   22: provider "pihole" {
│
╵
ryanwholey commented 2 years ago

Thank you for the issue @rishabhkalra! Very strange indeed. What Pi-Hole version are you running? Seems to be working okay against the nightly build.

# docker run --rm -p 8080:80 -e WEBPASSWORD=password --pull always pihole/pihole:nightly

resource "pihole_dns_record" "record" {
  domain = "record.com"
  ip     = "127.0.0.1"
}

provider "pihole" {
  url      = "http://localhost:8080"
  password = "password"
}

terraform {
  required_providers {
    pihole = {
      source  = "ryanwholey/pihole"
      version = "0.0.9"
    }
  }
}

I run traefik on my cluster at home and pass provider credentials using env vars similar to these

export PIHOLE_URL=https://pihole.my-domain.com
export PIHOLE_PASSWORD=my-web-login-password

The provider URL is intended to be the protocol://domain.tld, you're saying that you got the provider to work by appending /index.php?login to your provider.url?

I've also got API token auth support (https://github.com/ryanwholey/terraform-provider-pihole/issues/27) on the horizon which should be easier to manage, but the API still seems to be changing a bit so we will see.

rishabhkalra commented 2 years ago

@ryanwholey Thanks for the prompt reply on this!

What Pi-Hole version are you running?

I'm on Pi-hole v5.8.1, FTL v5.13, and web interface v5.10.1

The provider URL is intended to be the protocol://domain.tld, you're saying that you got the provider to work by appending /index.php?login to your provider.url?

That is correct, if I leave it as https://pihole.internal.mydomain.dev I see that error I put in the bottom of the issue description. However if I use https://pihole.internal.mydomain.dev/index.php?login I don't see that error and it attempts to create the dns record (which still fails with the '>' error)

I can try updating pihole and see if that fixes it 🤔

ryanwholey commented 2 years ago

@rishabhkalra

Okay I think I've found a way to reproduce what you're seeing, I believe the domain you're supplying actually resolves to <your-pihole-url>/admin.

provider "pihole" {
  url      = "http://localhost:8080/admin"
}

│ Error: login failed: session ID not found in response
│ 
│   with provider["registry.terraform.io/ryanwholey/pihole"],
│   on main.tf line 6, in provider "pihole":
│    6: provider "pihole" {
│ 
provider "pihole" {
  url      = "http://localhost:8080/admin/index.php?login"
}

│ Error: invalid character '<' looking for beginning of value
│ 
│   with pihole_dns_record.record,
│   on main.tf line 1, in resource "pihole_dns_record" "record":
│    1: resource "pihole_dns_record" "record" {
│ 
╵
provider "pihole" {
  url      = "http://localhost:8080"
}

No changes. Your infrastructure matches the configuration.

Can you check your ingress rules and see if this is the case?

rishabhkalra commented 2 years ago

Thanks for looking into this! Turns out that did seem to be the issue. I was adding a prefix of /admin in my ingress rule 😅 I now have an entirely different issue to deal with something else being broken in my infrastructure haha. But it seemed that fixed the issue and I could see my dns record get created 😄

Thanks so much for the help @ryanwholey !

ryanwholey commented 2 years ago

happy to help, thank you for testing out the provider!