Automation 使用Ansible创建任意数量的DigitalOcean水滴

Automation 使用Ansible创建任意数量的DigitalOcean水滴,automation,ansible,digital-ocean,Automation,Ansible,Digital Ocean,当我调用ansible的剧本时,我想创建任意数量的水滴。 例如: 我需要创建10个运行python代码的水滴 $ ansible-playbook install_pyapp_commission_new.yml --extra-vars "number_of_droplets_to_create=10" 我尝试将与_sequence:count=X一起使用,但无法将其应用于角色或内部任务(据我所知)。我的剧本是这样的: - name: Digital Ocean Provisioning

当我调用ansible的剧本时,我想创建任意数量的水滴。 例如: 我需要创建10个运行python代码的水滴

$ ansible-playbook install_pyapp_commission_new.yml --extra-vars "number_of_droplets_to_create=10"
我尝试将
与_sequence:count=X一起使用,但无法将其应用于角色或内部任务(据我所知)。我的剧本是这样的:

- name: Digital Ocean Provisioning
  hosts: 127.0.0.1
  gather_facts: false
  connection: local
  roles:
    - { role: do_provision, do_droplet_number: "{{ number_of_droplets_to_create | default(01) }}" }

- name: Setting up application
  gather_facts: true
  user: root
  hosts: do_instances
  roles:
    - { role: application, wait_time: 60 }
- name: Digital Ocean Provisioning
  hosts: 127.0.0.1
  gather_facts: false
  connection: local
  roles:
    - { role: do_provision, do_droplet_number: "{{ item }}" }
  with_sequence: count={{ number_of_droplets_to_create }}
因此,我将输入的液滴数量传递给
do_provision
作为
do_droplet_number
,因为我每次运行都创建一个(这样我可以从bash并行运行10个,每个都有不同的数量,从而实现我的目标,但这是一个肮脏的解决方案)

我想做这样的事情:

- name: Digital Ocean Provisioning
  hosts: 127.0.0.1
  gather_facts: false
  connection: local
  roles:
    - { role: do_provision, do_droplet_number: "{{ number_of_droplets_to_create | default(01) }}" }

- name: Setting up application
  gather_facts: true
  user: root
  hosts: do_instances
  roles:
    - { role: application, wait_time: 60 }
- name: Digital Ocean Provisioning
  hosts: 127.0.0.1
  gather_facts: false
  connection: local
  roles:
    - { role: do_provision, do_droplet_number: "{{ item }}" }
  with_sequence: count={{ number_of_droplets_to_create }}

但这是无效的。

这应该使用循环而不是\u序列

它将循环转换为角色,因为剧本中不能包含“何时”。 当do_droplet_编号为0时,需要“when”来防止创建液滴

剧本

- name: list hosts
  hosts: all
  gather_facts: false  

  vars:
    thiscount: "{{ mycount | default('0') }}"

  roles:
    - { role: do-provision, do_droplet_number: "{{ thiscount }}" }
- name: display number
  debug:
    msg: "mycount {{ item }}"
  loop: "{{ range(0, do_droplet_number|int) |list }}"
  when: thiscount > 0
角色/do provision/task/main.yml

- name: list hosts
  hosts: all
  gather_facts: false  

  vars:
    thiscount: "{{ mycount | default('0') }}"

  roles:
    - { role: do-provision, do_droplet_number: "{{ thiscount }}" }
- name: display number
  debug:
    msg: "mycount {{ item }}"
  loop: "{{ range(0, do_droplet_number|int) |list }}"
  when: thiscount > 0