Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of loop for comma separated variable in an Ansible playbook

Tags:

ansible

I am trying to run a script for multiple time using a loop.

The way I want it is

Script: /{{home}}/{{appUser}}/bin/trade_data_dnld.ksh {{ date }} {{ trade_id }}

Now my requirement is to run the same script for a hundred trade IDs and its not really possible to run the playbook hundred times.

I am looking for an option where I can create a variable called trade_id (free text) and pass the trade_id in a comma separated format.

The playbook should pick those trade IDs in the list and run it in loop for each one.

How can I achieve this?

like image 879
Sachin Khandelwal Avatar asked Sep 02 '25 09:09

Sachin Khandelwal


1 Answers

You can achieve it this way

- shell: /{{home}}/{{appUser}}/bin/trade_data_dnld.ksh {{ date }} {{ item }}
  with_items: "{{ trade_id.split(',') }}"

And pass the variable to the playbook using -e trade_id="dsdsd,sdsdsd,dsds" in your ansible-playbook command.
split will create a list of values and the shell task will iterate over this list and will run each time with the corresponding value.

like image 168
Baptiste Mille-Mathias Avatar answered Sep 04 '25 23:09

Baptiste Mille-Mathias