2015-05-14 33 views
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}}。所以任何人都可以提供任何有關我們如何實現這一目標的信息。謝謝

回答

1

這應該工作。基本上,設置一個標誌,不需要在when中使用Jinja。我使用了兩次set_fact,但實際上你只是在其他地方創建了is_nat = false var。

- set_fact: is_nat=false 
- set_fact: is_nat=true 
    when: "'nat' in server_name" 

- name: Add server to inventory 
    add_host: hostname={{ item.public_ip }} groupname={{ host_name }} 
    with_items: instance_info.instances 
    when: not is_nat 

下面是它在劇本中的一個例子,並且執行時使用各種server_names運行。

$ cat compare.yml 
--- 
- hosts: localhost 
    connection: local 
    vars: 
    is_nat: false 
    tasks: 
    - set_fact: is_nat=true 
     when: "'nat' in server_name" 

    - debug: msg="run this when not nat" 
     when: not is_nat 

$ ansible-playbook -i "localhost," compare.yml -e "server_name=gnat" 

PLAY [localhost] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [localhost] 

TASK: [set_fact is_nat=true] ************************************************** 
ok: [localhost] 

TASK: [debug msg="run this when not nat"] ************************************* 
skipping: [localhost] 

PLAY RECAP ******************************************************************** 
localhost     : ok=2 changed=0 unreachable=0 failed=0 

$ ansible-playbook -i "localhost," compare.yml -e "server_name=mosquito" 

PLAY [localhost] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [localhost] 

TASK: [set_fact is_nat=true] ************************************************** 
skipping: [localhost] 

TASK: [debug msg="run this when not nat"] ************************************* 
ok: [localhost] => { 
    "msg": "run this when not nat" 
} 

PLAY RECAP ******************************************************************** 
localhost     : ok=2 changed=0 unreachable=0 failed=0 
+0

謝謝我想我指定的變量應該是server_name而不是{{server_name}} –