AWS的多实例执行中的可变角色以旋转子网可用区域

时间:2017-06-30 13:59:30

标签: amazon-web-services ansible 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 

1 个答案:

答案 0 :(得分:0)

创建播放以将前一个子网设置为null。然后做了一个基本的条件,一旦完成第一次迭代就设置了前一个子网的事实。它现在已经解决了,谢谢大家。