可复制:在不同的子网中创建实例 [英] Ansible: Create instances in different subnets

查看:174
本文介绍了可复制:在不同的子网中创建实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ansible创建两个实例,一个在两个子网中使用下面的播放。我使用带有标记名称的exact_count来跟踪实例。这里有两个问题:


  1. 可以最终在第一个子网中创建两个实例,并报告第二个子网的[ok]。 li>
  2. 可能似乎不关心停止的实例。它创建新的实例,而不是启动现有的实例,或至少将它们视为实例组的一部分。




$
- {{vpc_pvt_subnet_1}}
$ b $$ {pre $ $ b ec2:
group:{{kafka_sg}}
key_name:{{ec2_keypair}}
region:{{region}}
image: {{ami_id}}
wait:true
instance_type:{{kafka_inst_type}}
vpc_subnet_id:{{item}}
instance_tags:
名称:kafka-instance
所有者:data
exact_count:2
count_tag:
名称:kafka-instance
注册表:ec2


有人可以告诉我这里有什么问题吗?

解决方案

假设哟您要在每个子网中创建一个EC2实例,您提供的代码片段中的一个可见错误是 exact_count 的值应设置为1(不是2),因为 with_items 将循环在您的手册中运行 ec2 模块两次。您要每次迭代创建一个实例。



接下来,我将根据您的问题回答 -



1]您还需要指定 zone 参数以及 ec2 模块。由于 zone 根据 vpc_subnet_id 是动态的,我建议以下结构 -



在您的vars中

  subnets:
- {zone:us-east- 1a,vpc_pvt_subnet:subnet-abcdafa5}
- {zone:us-east-1b,vpc_pvt_subnet:subnet-zyxwvb51}

ec2 任务 -

   - 名称:创建kafka实例
with_items:{{subnets}}
ec2:
group:{{kafka_sg}}
key_name:{{ec2_keypair}}
region:{{region}}
image:{{ami_id}}
wait:true
instance_type:
vpc_subnet_id:{{item.vpc_pvt_subnet}}
zone:{{item.zone}}
instance_tags:
名称:kafka-实例
所有者:data
exact_count:1
count_tag:
名称:kafka-instance
注册表:ec2

2]是的,上面的方法将永远创建新的实例,即使一个实例已经存在于具有停止状态的子网中,就好像这个实例不存在一样。如果要通过标签显式启动已停止实例,可以通过将状态参数传递给新的 ec2 任务 - 您不能在同一任务中使用状态 exact_count 参数。



希望这有帮助!


I'm trying to use Ansible to create two instances, one each in two subnets using the play below. I'm using exact_count with tag Name to keep track of instances. There are two issues here:

  1. Ansible ends up creating two instances in the first subnet and reports [ok] for the second subnet.
  2. Ansible doesn't seem to care about stopped instances. It creates new instances, instead of starting existing ones, or atleast considering them as part of the group of instances.

  - name: Create kafka instances
    with_items:
      - "{{ vpc_pvt_subnet_2 }}"
      - "{{ vpc_pvt_subnet_1 }}"
    ec2:    
      group: "{{ kafka_sg }}"  
      key_name: "{{ ec2_keypair }}"  
      region: "{{ region }}"  
      image: "{{ ami_id }}"  
      wait: true   
      instance_type: "{{ kafka_inst_type }}"  
      vpc_subnet_id: "{{ item }}"  
      instance_tags:  
        Name: "kafka-instance"          
        Owner: data  
      exact_count: 2   
      count_tag:  
        Name: "kafka-instance"  
    register: ec2

Can someone please tell me what's wrong with the playbook here?

解决方案

Assuming that you want to create 1 EC2 instance in each subnet, one visible error in the snippet you provided is that the value of exact_count should be set to 1 (not 2) because with_items will loop to run ec2 module twice in your playbook. You want each iteration to create exactly 1 instance.

Next, I will answer according to your questions -

1] You need to specify zone parameter as well to ec2 module. Since zone is dynamic according to a vpc_subnet_id, I would suggest the following structure -

In your vars -

subnets:
  - { zone: "us-east-1a", vpc_pvt_subnet: "subnet-abcdafa5"}
  - { zone: "us-east-1b", vpc_pvt_subnet: "subnet-zyxwvb51"}

In the ec2 task -

- name: "Create kafka instances"
  with_items: "{{ subnets }}"
  ec2:
    group: "{{ kafka_sg }}"
    key_name: "{{ ec2_keypair }}"
    region: "{{ region }}"
    image: "{{ ami_id }}"
    wait: true
    instance_type: "{{ kafka_inst_type }}"
    vpc_subnet_id: "{{ item.vpc_pvt_subnet }}"
    zone: "{{ item.zone }}"
    instance_tags:
      Name: "kafka-instance"
      Owner: "data"
    exact_count: 1
    count_tag:
      Name: "kafka-instance"
  register: ec2

2] Yes, the above way will always create new instance even if an instance already exists in a subnet with "stopped" state as if this instance never existed. If you want to explicitly start the "stopped" instances by tags, you can achieve that by passing the state parameter to a new ec2 task - you cannot use state and exact_count parameters together in the same task.

Hope this helps!

这篇关于可复制:在不同的子网中创建实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆