Ansible。如何从组中选择N个主机

Ansible。如何从组中选择N个主机,ansible,ansible-playbook,Ansible,Ansible Playbook,假设一个组有10台主机。 如何从10开始在N台主机上运行playbook。N是1到10之间的任意值。 例如: 但如果我用变量代替3,它就不起作用了,就像这样 - hosts: groups['group_name'][1:N] 它可能是随机的,第一个,最后一个 谢谢。这项工作在ansible 2.1中非常好: --- - hosts: all gather_facts: no tasks: - group_by: key=limited_selection wh

假设一个组有10台主机。
如何从10开始在N台主机上运行playbook。N是1到10之间的任意值。
例如:

但如果我用变量代替3,它就不起作用了,就像这样

- hosts: groups['group_name'][1:N]  
它可能是随机的,第一个,最后一个


谢谢。

这项工作在ansible 2.1中非常好:

---
- hosts: all
  gather_facts: no
  tasks:
    - group_by: key=limited_selection
      when: play_hosts.index(inventory_hostname) < max_index | int

- hosts: limited_selection
  gather_facts: no
  tasks:
    - debug: msg="I'm in the limited selection group!"
---
-主持人:全部
收集事实:不
任务:
-分组依据:键=有限的选择
时间:play_hosts.index(目录主机名)

像这样执行:
ansible playbook-e max_index=3 playbook.yml
或在其他地方定义
max_index

我回答了类似的问题[这里]()。您可以动态地组成任何组。当:play_hosts.index(inventory_hostname)作为条件时,您可以使用
。不幸的是,它也不起作用。同样的问题。如果:play_hosts.index(inventory_hostname)<2-它可以工作。如果my_N_var-它不允许您将变量传递到剧本中?你试过了吗?是的。并尝试重新分配,比如vars:new\u var:my\u N\u var.when:play\u hosts.index(inventory\u hostname)---
- hosts: all
  gather_facts: no
  tasks:
    - group_by: key=limited_selection
      when: play_hosts.index(inventory_hostname) < max_index | int

- hosts: limited_selection
  gather_facts: no
  tasks:
    - debug: msg="I'm in the limited selection group!"