krishna m
krishna m

Reputation: 247

Ansible use variable inside a variable

I am trying to use variable inside a variable

vars:

env: dev  
groupname: (Dynamic variable which comes as stdout of ansible task )

task:

- name: var to trim  
  set_fact:  
    trim_var: "{{ groupname.split(\"test-{{ env }}-\") }}"  

But its not replacing env with dev. can some one please help?

Upvotes: 2

Views: 1724

Answers (1)

larsks
larsks

Reputation: 312430

You never nest {{...}} markers. You're already inside a Jinja context, so you can just write variables normally:

- name: var to trim  
  set_fact:  
    trim_var: "{{ groupname.split('test-' ~ env ~ '-') }}"

Note that the Jinja ~ operator behaves like +, but it will convert its operands to strings first (which doesn't matter in this case, but is quite useful if you're dealing with variables that are not strings).

Upvotes: 4

Related Questions