Amazon ec2 Ansible对循环中的项执行when语句
我正在尝试根据变量中的数字创建一些AWS弹性IP-到目前为止一切都很好。但是我需要检查这个EIP是否已经存在于我从文件中加载的一组变量中。如果EIP存在,我想跳过它的创建, 例如:Amazon ec2 Ansible对循环中的项执行when语句,amazon-ec2,ansible,eip,Amazon Ec2,Ansible,Eip,我正在尝试根据变量中的数字创建一些AWS弹性IP-到目前为止一切都很好。但是我需要检查这个EIP是否已经存在于我从文件中加载的一组变量中。如果EIP存在,我想跳过它的创建, 例如: - set_fact: "number_of_ips=3 - name: load the local config file include_vars: profiles/{{ ec2_tag_environment }}/file_with_variables - name: allocate a new
- set_fact: "number_of_ips=3
- name: load the local config file
include_vars: profiles/{{ ec2_tag_environment }}/file_with_variables
- name: allocate a new elastic IP for server {{ item }} if not exists already
ec2_eip:
profile: "{{ boto_profile }}"
region: "{{ ec2_region }}"
state: present
register: reg_eip_server_{{ item }}
when: server_eip_{{ item }} is not defined
with_sequence: start=1 end={{ number_of_ips}}
(请不要介意缩进-缩进有效,但可能是复制/粘贴问题)
在“何时”行中,我收到一条警告,如果我尝试使用reg\u eip\u server\u 1.public\u ip
,它当然会失败,因为它不存在
以下是问题:
我设法弄明白了。 使用这些项本身,警告只是一个警告,但实际上是有效的,因此如果文件do中的变量存在,它将忽略循环实例并继续。
所以:1。是的,有可能。2.从结果项中获取公共IP(使用结果项编号作为索引)。3.尽管警告本身仍然有效。忘了提及,我们的想法是今天的数字是3,但明天将是20。我在创建EIP后将其保存在配置文件中,以便下次运行时不会再次创建它。