Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible string to variable (aka eval)

Tags:

ansible

I have a list of strings strs = [ 'foo', 'bar' ] and some dicts foo = {'a': 1, 'b': 2}, bar = {'a': 3, 'b': 4}. I'd like to use with_items to index into the named dicts

- copy
  src: {{item}}.a
  dest: {{item}}.b
  with_items: strs

But I want {{item}} to refer to the variables named foo and bar rather than the strings. In lisp or python I'd use eval for this. Is there something similar in ansible?

like image 356
seanmcl Avatar asked Jul 23 '26 14:07

seanmcl


1 Answers

Why not set a dictionary and loop through it using with_dict?

---
- hosts: localhost
  connection: local
  vars:
    strs:
      foo:
        a: 1
        b: 2
      bar:
        a: 3
        b: 4

  tasks:
  - copy: src={{ item.value.a }} dest={{ item.value.b }}
    with_dict: strs
like image 176
Strahinja Kustudic Avatar answered Jul 26 '26 04:07

Strahinja Kustudic