Score:0

Save rendered Jinja to variable

cn flag
cov

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.

Score:1
th flag

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.

Score:0
in flag

You can do this using set_fact.

- set_fact:
    url: "https://{{ hashivault_secrets.value.data.USERNAME }}:{{ hashivault_secrets.value.data.TOKEN }}@hosting.com/organization"
Score:0
cn flag

Vars can be defined as expressions referencing other vars. Allows splitting an expression to a reasonable size.

The environment keyword can apply to all tasks of a play. This can reference vars defined anywhere.

module_defaults can also be used to provide a value to use when none is provided. This tends to be helpful for repetitive things like API creds in general. For pip module, gives an alternative of how to provide an --extra-index-url as the module provides a way to add arbitrary arguments.

group_vars/all/creds.yml or wherever you want to define configuration vars.

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

play.yml You did not provide a play, but this is a good place to use play scope keywords that will apply to all tasks. I'll make up a trivial play, adjust to what you are doing:

- hosts: thingapp

  environment: "{{ env }}"

  module_defaults: 
    pip: 
      extra_args: "--extra-index-url {{ url }}"

  roles:
     - webserver
     - pythonapp

roles/pythonapp/tasks/main.yml

   - pip:
       name: my-internal-package
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.