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

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

问题描述

我正在尝试使用 Ansible 创建两个实例,使用以下播放在两个子网中各一个.我使用带有标签名称的精确计数来跟踪实例.这里有两个问题:

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

<块引用>

 - 名称:创建 kafka 实例with_items:- "{{ vpc_pvt_subnet_2 }}"- "{{ vpc_pvt_subnet_1 }}"ec2:组:{{ kafka_sg }}"key_name: "{{ ec2_keypair }}"区域:{{ 区域}}"图像:{{ ami_id }}"等待:真的实例类型:{{ kafka_inst_type }}"vpc_subnet_id: "{{ item }}"实例标签:名称:kafka 实例"所有者:数据精确计数:2计数标签:名称:kafka 实例"注册:ec2

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

解决方案

假设您要在每个子网中创建 1 个 EC2 实例,您提供的代码段中的一个可见错误是 exact_count 应该设置为 1(而不是 2),因为 with_items 将在你的剧本中循环运行 ec2 模块两次.您希望每次迭代只创建 1 个实例.

接下来,我会根据你的问题来回答-

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

在你的变量中 -

子网:- { 区域:us-east-1a",vpc_pvt_subnet:subnet-abcdafa5"}- { 区域:us-east-1b",vpc_pvt_subnet:subnet-zyxwvb51"}

ec2任务中-

- name: "创建 kafka 实例"with_items: "{{ 子网 }}"ec2:组:{{ kafka_sg }}"key_name: "{{ ec2_keypair }}"区域:{{ 区域}}"图像:{{ ami_id }}"等待:真的实例类型:{{ kafka_inst_type }}"vpc_subnet_id: "{{ item.vpc_pvt_subnet }}"区域:{{ item.zone }}"实例标签:名称:kafka 实例"所有者:数据"精确计数:1计数标签:名称:kafka 实例"注册:ec2

2] 是的,即使一个实例已经存在于具有停止"状态的子网中,上述方法也将始终创建新实例,就好像该实例从未存在过一样.如果您想通过标签显式启动已停止"的实例,您可以通过将 state 参数传递给新的 ec2 任务来实现 - 您不能使用 stateexact_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!

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

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