JBone
JBone

Reputation: 1794

passing a variable value in with_items that is used in uri call

I am not sure if this is possible but here is what I am trying to do. I appreciate if any inputs.

I have a jinja2 template that have one variable value

say template.json.j2

{
  "index_patterns": "{{ pattern }}"
 }

and in my playbook, I have this API call

uri:
  url: "https://{{ ansible_default_ipv4.address }}:8100/_index_template/{{ item.name }}
  method: PUT
  body: "{{ lookup('template', item.path) }}
with_items:
  - { path: 'template.json.j2', name: 'test1' }
  - { path: 'template.json.j2', name: 'test2' }
  - { path: 'template.json.j2', name: 'test3' }
 

for test1, I want to have a different index_patterns value and so is for test2 ..how can I pass my {{ pattern }} value dynamically in my template.json.j2 based on the name in with_items?

may be something like this `{ path: 'template.json.j2', name: 'test1', pattern: 'abcd' }

Upvotes: 0

Views: 983

Answers (1)

seshadri_c
seshadri_c

Reputation: 7340

This is possible. As you mentioned you can pass { path: 'template.json.j2', name: 'test1', pattern: 'abcd' } in with_items. Then add the pattern variable as vars to the task which would reference item.pattern.

Like below:

  - uri:
      url: "https://{{ ansible_default_ipv4.address }}:8100/_index_template/{{ item.name }}"
      method: PUT
      body: "{{ lookup('template', item.path) }}"
    with_items:
      - { path: 'template.json.j2', name: 'test1', pattern: 'foo' }
      - { path: 'template.json.j2', name: 'test2', pattern: 'bar' }
      - { path: 'template.json.j2', name: 'test3', pattern: 'baz' }
    vars:
      pattern: "{{ item.pattern }}"

Upvotes: 1

Related Questions