Hi
I want to generate the variable names based on another variable value. Something like this:
Vars:
environements: [“trunk”, “dev01”]
my_trunk_port: 8080
my_dev01_port: 8180
Template:
{{ my_{{ env }}_port }}
Hi
I want to generate the variable names based on another variable value. Something like this:
Vars:
environements: [“trunk”, “dev01”]
my_trunk_port: 8080
my_dev01_port: 8180
Template:
{{ my_{{ env }}_port }}
I’ve been using this pattern:
vars:
environments: [ “trunk”, “dev01” ]
my_ports:
trunk: 8080
dev01: 8081
template:
{{ my_ports[env] }}
-dave
Hi
Thx for the answer. But this is not a option here. The vars is already defined in this format:
my_trunk_port: 8080
my_dev01_port: 8180
my_dev02_port: 8280
my_dev03_port: 8480
and then I have an env var [trunk, dev01, dev02…] to construct the final var name
try something like this:
{{ hostvars[inventory_hostname][ "my_" + env + "_port"] }}
if I'm not mistaken..
vars:
port_vars:
my_trunk_port: 8080
my_dev01_port: 8180
my_dev02_port: 8280
my_dev03_port: 8480
- set_fact:
my_ports: {}
- pause: seconds=0
when: (my_ports[item[0].substring(3).substring(0,len(item[0]) - 3)] = item[1]) and false or false
with_items: port_vars.items()
- debug: msg="{{ my_ports['trunk'] }}"
Hmm, much better than my convoluted example.
Hi
I have added my vars via include_vars and not in hostvars
include_vars: /path/myvars.yml
template: src=input.j2 dest=output.j2
so {{ hostvars[inventory_hostname][ “my_” + env + “_port”] }} will not working
What will the name of the array then be?
It was a typo error from my side. The solution is working fine. Thx a lot