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.
Best Answer
You can just set a var directly:
Or you can use
set_fact
: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 ifhashivault_secrets
changes, whileset_fact
will result in a static value based on the evaluation of the template at the time theset_fact
task runs. Both behaviours are useful, depending on the circumstances.