Can you use templates to compose a restful URL?

I have the following restful URL, which works when I call it.

  temporarily_disable_pihole:
    url: http://pihole.example.com/admin/api.php?disable=600&auth=ABCDEFG

However, I want to pass in a secret for the auth portion. In fact, I want to use multiple secrets to build one string. Give secret’s A B C D I’d like to be able to construct the URL with a template. How can I do this? The following doesn’t work, but it has the gist of what I’m trying to do.

rest_command:  
  temporarily_disable_pihole:
    url: >
      {% 
         set url = !secret A + !secret B + !secret C + !secret D
      %}
      {{ url }}

Most of my attempts generate an error when calling the restful command:

Failed to call service rest_command/temporarily_disable_pihole. local variable ‘request’ referenced before assignment

I believe the problem is that you can’t retrieve data from secrets.yaml from inside a template. You might have to do something like this:

rest_command:
  temporarily_disable_pihole:
    url: "http://pihole.example.com/admin/api.php?disable=600&auth={{ secretA ~ secretB ~ secretC ~ secretD }}"
script:
  temporarily_disable_pihole:
    sequence:
      service: rest_command.temporarily_disable_pihole
      data:
        secretA: !secret A
        secretB: !secret B
        secretC: !secret C
        secretD: !secret D

Then call script.temporarily_disable_piholeinstead of the rest_command directly.

2 Likes