使用变量进行模式匹配的Ansible条件

时间:2015-05-14 18:42:03

标签: amazon-ec2 ansible ansible-playbook

我使用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 个答案:

答案 0 :(得分: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