Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Ansible数组子集上的循环_Loops_Ansible - Fatal编程技术网

Loops Ansible数组子集上的循环

Loops Ansible数组子集上的循环,loops,ansible,Loops,Ansible,我正在编写Ansible playbook,以便在Grafana中自动部署一些仪表盘。我有一个很大的项目列表,这些项目将进入每个仪表板,我如何在数组中循环,但以数组中的4个元素为一组 这是循环 - 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

我正在编写Ansible playbook,以便在Grafana中自动部署一些仪表盘。我有一个很大的项目列表,这些项目将进入每个仪表板,我如何在数组中循环,但以数组中的4个元素为一组

这是循环

- 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 }}"
但是我想运行以下命令集:

- 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 }}"

在这份清单上:

kafka_topic_names:
- topic1
- topic2
...
- topicN

因此,我希望在每一行中有4个主题集,从而得到所需的尽可能多的行。我可以让它在一行中处理所有内容,但我不确定如何在循环过程中停止,执行我想要的命令,然后从数组中的同一位置继续循环

您可以像shell脚本一样在一个任务中运行所有命令

- 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 }}"