Reputation: 11
I'm working on an Ansible playbook to automate the deployment of some dashboards in Grafana. I have a large list of items that will go into each dashboard, how can I loop over the array, but in sets of 4 elements from the array?
Here's the loop
- name: Loop through the topcs to fill in the template with the topic names (replacing the {kafka_topic_placeholder}) and append them to the working file
shell: sed 's/{kafka_topic_placeholder}/{{ item }}/g' "{{ widget_template_file_path }}" >> "{{ working_file_name }}"
loop: "{{ kafka_topic_names }}"
But I want to run these set of commands:
- name: Create the beginning of row
shell: sed 's/{dashboard_id_placeholder}/{{ dashboard_id }}/g' "{{ dashboard_template_file_part_1 }}" > "{{ final_output_file_name }}"
- name: Add the contents of our working file
shell: cat "{{ working_file_name }}" >> "{{ final_output_file_name }}"
- name: Add the ending of the row
shell: sed 's/{overwrite_placeholder}/{{ overwrite_dashboard }}/g' "{{ dashboard_template_file_part_2 }}" >> "{{ final_output_file_name }}"
Over this list:
kafka_topic_names:
- topic1
- topic2
...
- topicN
So I want to end up with as many rows as needed to have sets of 4 topics in each row. I can get it working with everything in a single row, but I'm not sure how to stop during the loop, do the commands I want, then continue the loop from the same place in the array
Upvotes: 1
Views: 248
Reputation: 2269
You can run all the commands in one task like a shell script.
- name: Create the beginning of row
shell: |
sed 's/{kafka_topic_placeholder}/{{ item }}/g' "{{ widget_template_file_path }}" >> "{{ working_file_name }}"
sed 's/{dashboard_id_placeholder}/{{ dashboard_id }}/g' "{{ dashboard_template_file_part_1 }}" > "{{ final_output_file_name }}"
cat "{{ working_file_name }}" >> "{{ final_output_file_name }}"
sed 's/{overwrite_placeholder}/{{ overwrite_dashboard }}/g' "{{ dashboard_template_file_part_2 }}" >> "{{ final_output_file_name }}"
loop: "{{ kafka_topic_names }}"
Upvotes: 1