在 AWS 的多实例执行中轮换子网可用区的 Ansible 角色
Ansible role to Rotate Subnets Availability zone in multiple instance execution for AWS
如标题所述,我试图阻止实例连续两次选择相同的 AZ。我当前的角色设置为根据可用的 ips 轮换。它工作正常,但是当我 运行 多台服务器时,它会一直转到同一个 AZ。我需要找到一种方法来防止它连续两次选择相同的 AZ。
这是在我的整个服务器构建期间调用的角色
#gather vpc and subnet facts to determine where to build the server
- name: gather subnet facts
ec2_vpc_subnet_facts:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
aws_region: "{{ aws_region }}"
register: ec2_subnets
- name: initialize build subnets
set_fact:
build_subnet: ""
free_ips: 0
previous_subnet_az: ""
- name: pick usable ec2_subnets
set_fact:
# Item.id is sets the current builds subnet Id
build_subnet: "{{ item.id }}"
free_ips: "{{ item.available_ip_address_count|int }}"
# Just for debugging and does not work
previous_subnet_az: " {{ item.availability_zone }}"
when: ("{{ item.available_ip_address_count|int }}" > free_ips) and ("ansible_subnet" in "{{ item.tags }}") and ("{{ previous_subnet_az|string }}" != "{{ item.availability_zone|string }}")
# Each subnet in the list
with_items: '{{ec2_subnets.subnets}}'
register: build_subnets
- debug: var=build_subnets var=build_subnet var=previous_subnet_az
var=selected_subnet
创建了一个游戏来设置前一个子网为空。然后做了一个基本条件,一旦完成第一次迭代就设置前一个子网的事实。已经解决了,谢谢大家
如标题所述,我试图阻止实例连续两次选择相同的 AZ。我当前的角色设置为根据可用的 ips 轮换。它工作正常,但是当我 运行 多台服务器时,它会一直转到同一个 AZ。我需要找到一种方法来防止它连续两次选择相同的 AZ。
这是在我的整个服务器构建期间调用的角色
#gather vpc and subnet facts to determine where to build the server
- name: gather subnet facts
ec2_vpc_subnet_facts:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
aws_region: "{{ aws_region }}"
register: ec2_subnets
- name: initialize build subnets
set_fact:
build_subnet: ""
free_ips: 0
previous_subnet_az: ""
- name: pick usable ec2_subnets
set_fact:
# Item.id is sets the current builds subnet Id
build_subnet: "{{ item.id }}"
free_ips: "{{ item.available_ip_address_count|int }}"
# Just for debugging and does not work
previous_subnet_az: " {{ item.availability_zone }}"
when: ("{{ item.available_ip_address_count|int }}" > free_ips) and ("ansible_subnet" in "{{ item.tags }}") and ("{{ previous_subnet_az|string }}" != "{{ item.availability_zone|string }}")
# Each subnet in the list
with_items: '{{ec2_subnets.subnets}}'
register: build_subnets
- debug: var=build_subnets var=build_subnet var=previous_subnet_az
var=selected_subnet
创建了一个游戏来设置前一个子网为空。然后做了一个基本条件,一旦完成第一次迭代就设置前一个子网的事实。已经解决了,谢谢大家