Rahul Mehrotra
Rahul Mehrotra

Reputation: 639

Ansible conditional with pattern match using variables

I am using Ansible roles to provision AWS EC2 instances, I have created a series of task in ec2 roles which gets execute for each server.

- 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

So I basically want to check if the server has a prefix nat then the last three task should be skipped as it doesn't make sense to execute them. I cannot do direct comparison as some other postfix data is added to {{ server_name }} depending upon the environment, time and other details. So can anyone provide me any information of how we can achieve this. Thank you

Upvotes: 0

Views: 5631

Answers (1)

300D7309EF17
300D7309EF17

Reputation: 24613

This should work. Basically, set a flag, no need to use Jinja in when. I'm using set_fact twice but in reality you'd just create the is_nat=false var elsewhere.

- 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

Here's an example of it in a playbook, and the execution runs with various 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   

Upvotes: 1

Related Questions