Shimon
Shimon

Reputation: 1464

Pass array/list from bash script to ansible loop

I have a simple goal in mind which I thought should be trivial, but does not seem so. I have a list of unknown names in bash, e.g. instances="i1,i2,i3" and need to iterate over them in Ansible's role, which looks like this:

- name My name
  shell: "runServer {{item}}"
  loop: "{{ my_instances }}"

I also tried to use with_items as well:

- name My name
  shell: "runServer {{item}}"
  with_items: "{{ my_instances }}"

where my_instances is a variable I am trying to construct based on the instances mentioned above. I tried so far few approaches:

  1. use --extra_vars "instances='i1,i2,i3'"
  2. use --extra_vars "instances=['i1','i2','i3']" where instances are constructed in outside Rundeck job's bash shell as a bash array
  3. use --extra_vars "{instances:['i1','i2','i3']}"
  4. use --extra_vars "instances='i1,i2,i3' and inside my main job in the var section attempted to convert it to the list like this: my_instances: "{{ instances.split(',') | list }}" In all cases I was simply declaring my_instances: "{{ instances }}"
  5. use loop: "{{ query('my_instances', 'all') }}" in the role along with 1-4 (In all other cases above I was simply declaring variable as my_instances: '{{ instances }}' in var section)

Regardless of my tries I always get the same dreadful error. Can somebody explain me what I am doing wrong and how to iterate over items in role's playbook (main.yml) where loop var is coming from outside bash shell (in my case Rundeck's bash shell) ? I am running out ideas.

Thanks in advance!

Upvotes: 0

Views: 781

Answers (1)

gary lopez
gary lopez

Reputation: 1954

--extra-vars is an argument and it's not needed use = after of it. And when you use wiht_items or loop it's necessary use a list and this is constructed as your second approach:

--extra_vars "instances=['i1','i2','i3']"

Upvotes: 0

Related Questions