Save rendered Jinja to variable

I'd like to split up some Jinja templating into multiple lines to keep lines under 120 characters, and register a variable for easy reuse. Is there a way to do something like the following? Is there a built-in (or community) module for this? If possible, I'd like to do it without using temporary files.

- jinja: "https://{{ username }}:{{ token }}@hosting.com/organization"
  vars:
    username: "{{ hashivault_secrets.value.data.USERNAME }}"
    token: "{{ hashivault_secrets.value.data.TOKEN }}"
  register: url

So the use/reuse could look something like

   - pip:
       name: my-internal-package
     env:
       PIP_EXTRA_INDEX_URL: "{{ url }}"

I'm currently using Ansible 2.9.


Solution 1:

You can just set a var directly:

  vars:
    url: https://{{ username }}:{{ token }}@hosting.com/organization
    username: "{{ hashivault_secrets.value.data.USERNAME }}"
    token: "{{ hashivault_secrets.value.data.TOKEN }}"

Or you can use set_fact:

- set_fact:
    url: https://{{ username }}:{{ token }}@hosting.com/organization
  vars:
    username: "{{ hashivault_secrets.value.data.USERNAME }}"
    token: "{{ hashivault_secrets.value.data.TOKEN }}"

The main practical difference (beyond setting variables not requiring an additional task) is that variables are evaluated at the time of use so the value of the first url might change over time if hashivault_secrets changes, while set_fact will result in a static value based on the evaluation of the template at the time the set_fact task runs. Both behaviours are useful, depending on the circumstances.