使用Ansible创建许多AWS实例,' count'不起作用

时间:2015-10-20 14:28:07

标签: amazon-web-services ansible

我有这本剧本:

---
# Run it like this:
# ansible-playbook --extra-vars '{"VAR":"var-value", "VAR":"var-value"}' playbook-name.yml
- hosts: localhost
  vars:
    instance_tag : "{{ TAG }}"
    instances_num: 2
  tasks:
  - name: Create new AWS instances
    local_action:
      module: ec2
      region: us-east-1 
      key_name: integration 
      instance_type: m3.medium 
      image: ami-61dcvfa 
      group: mysecgroup 
      instance_tags:
        Name: "{{ instance_tag }}"
    with_sequence: count = {{ instances_num | int }}

当我运行它时会抛出这个:

TASK: [Create new AWS instances] **********************************************
fatal: [localhost] => unknown error parsing with_sequence arguments: u'count = 1'

FATAL: all hosts have already failed -- aborting

我做错了什么? 我也试过了2,但是抛出了同样的错误。 我也试过了"{{instances_num}}",但没有尝试过。

1 个答案:

答案 0 :(得分:0)

ec2 module有一个count参数,您可以直接使用,而不是尝试loop the task over a sequence

你可以像这样使用它:

---
# Run it like this:
# ansible-playbook --extra-vars '{"VAR":"var-value", "VAR":"var-value"}' playbook-name.yml
- hosts: localhost
  vars:
    instance_tag : "{{ TAG }}"
    instances_num: 2
  tasks:
  - name: Create new AWS instances
    local_action:
      module: ec2
      region: us-east-1 
      key_name: integration 
      instance_type: m3.medium 
      image: ami-61dcvfa 
      group: mysecgroup 
      instance_tags:
        Name: "{{ instance_tag }}"
      count: "{{ instances_num }}"