运行Ansible play时获取当前串行计数器

运行Ansible play时获取当前串行计数器,ansible,Ansible,我正在运行一个Ansible play,它使用serial属性一次运行一部分主机 我想获取当前串行计数器,以便可以使用它在add_host任务中动态设置组名 例如,我的主机文件如下所示: "172.23.130.13", "172.23.130.34", "172.23.130.99", "172.23.130.94" - name: Create Add and Remove Group Batches gather_facts: false hosts: all become:

我正在运行一个Ansible play,它使用
serial
属性一次运行一部分主机

我想获取当前串行计数器,以便可以使用它在
add_host
任务中动态设置组名

例如,我的主机文件如下所示:

"172.23.130.13",
"172.23.130.34",
"172.23.130.99",
"172.23.130.94"
- name: Create Add and Remove Group Batches
  gather_facts: false
  hosts: all
  become: true
  no_log: false
  serial: 2

  tasks:
    - add_host:
        name: "{{ item.1 }}"
        ansible_ssh_port: 2020
        group: loadbalancer-{{ <<serial counter here>> }}
        action: add
      with_indexed_items: "{{ ansible_play_batch  }}"
      delegate_to: localhost
我的剧本如下:

"172.23.130.13",
"172.23.130.34",
"172.23.130.99",
"172.23.130.94"
- name: Create Add and Remove Group Batches
  gather_facts: false
  hosts: all
  become: true
  no_log: false
  serial: 2

  tasks:
    - add_host:
        name: "{{ item.1 }}"
        ansible_ssh_port: 2020
        group: loadbalancer-{{ <<serial counter here>> }}
        action: add
      with_indexed_items: "{{ ansible_play_batch  }}"
      delegate_to: localhost
-名称:创建添加和删除组批次
收集事实:错误
主持人:全部
变成:真的
无日志:false
序列号:2
任务:
-添加\u主机:
名称:“{item.1}}”
ansible_ssh_端口:2020
组:loadbalancer-{{}
行动:添加
带有索引项:“{{ansible\u play\u batch}”
委托给:localhost
serial
设置为2时,此播放将运行两次。因此,我希望通过
add_host
任务创建两个组。一组称为
loadbalancer-1
,另一组称为
loadbalancer-2

loadbalancer-1
将包含主机文件中的前两个IP,组
loadbalancer-2
将包含下两个IP

这可能吗

感谢人们提供的任何帮助。

您愿意帮忙吗?您可以定义循环控制变量并使用该变量对主机进行分组?是否有帮助?您可以定义循环控制变量并使用它对主机进行分组?