用于在 AWS 的多实例执行中轮换子网可用区的 Ansible 角色



正如标题所述,我试图阻止实例连续两次选择相同的可用区。 我目前的角色设置为根据可用 IP 轮换。 它工作正常,但是当我运行多个服务器时,它会一直转到同一个可用区。 我需要找到一种方法来防止它连续两次选择相同的 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 

创建了一个重头戏,用于在 null 时设置上一个子网。 然后执行一个基本条件,在完成第一次迭代后设置前一个子网的事实。 现在已经解决了,谢谢大家。

最新更新