amazon web services - Ansible: Create instances in different subnets -
i'm trying use ansible create 2 instances, 1 each in 2 subnets using play below. i'm using exact_count tag name keep track of instances. there 2 issues here:
- ansible ends creating 2 instances in first subnet , reports [ok] second subnet.
- ansible doesn't seem care stopped instances. creates new instances, instead of starting existing ones, or atleast considering them part of 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 please tell me what's wrong playbook here?
assuming want create 1 ec2 instance in each subnet, 1 visible error in snippet provided value of exact_count
should set 1 (not 2) because with_items
loop run ec2
module twice in playbook. want each iteration create 1 instance.
next, answer according questions -
1] need specify zone
parameter ec2
module. since zone
dynamic according vpc_subnet_id
, suggest following structure -
in vars -
subnets: - { zone: "us-east-1a", vpc_pvt_subnet: "subnet-abcdafa5"} - { zone: "us-east-1b", vpc_pvt_subnet: "subnet-zyxwvb51"}
in 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, above way create new instance if instance exists in subnet "stopped" state if instance never existed. if want explicitly start "stopped" instances tags, can achieve passing state
parameter new ec2
task - cannot use state
, exact_count
parameters in same task.
hope helps!
Comments
Post a Comment