0
我使用Ansible角色來設置AWS EC2實例,我已經在ec2角色中創建了一系列任務,併爲每個服務器獲取執行權。有條件的使用變量進行模式匹配
- name: Provisioning EC2 instance
ec2:
region: "{{ region }}"
key_name: "{{ key }}"
instance_type: "{{ instance_type }}"
image: "{{ ami }}"
wait: yes
assign_public_ip: yes
group_id: "{{ sgs }}"
vpc_subnet_id: "{{ PublicSubnet }}"
source_dest_check: false
instance_tags: '{ "Name": "{{ server_name }}", "Environment": "{{ tag_env }}" }'
register: instance_info
- name: Storing instance information in {{ server_name }}_info file
shell: echo "{{ host_name }}:" " {{ item.public_ip }}"> group_vars/"{{ server_name }}"_info
with_items: instance_info.instances
when: 'nat in {{ server_name }}' <<=== HERE
- name: Add server to inventory
add_host: hostname={{ item.public_ip }} groupname={{ host_name }}
with_items: instance_info.instances
when: "'nat' not in {{ server_name }}"
- name: Waiting for server to come up
wait_for:
host: "{{ item.public_ip }}"
port: 22
delay: 5
timeout: 300
with_items: instance_info.instances
所以我基本上要檢查服務器是否有前綴NAT然後最後三個任務應該被忽略,因爲它沒有意義執行它們。我不能直接比較,因爲根據環境,時間和其他細節,將一些其他後綴數據添加到{{server_name}}。所以任何人都可以提供任何有關我們如何實現這一目標的信息。謝謝
謝謝我想我指定的變量應該是server_name而不是{{server_name}} –