Nikesh Jauhari
Nikesh Jauhari

Reputation: 245

How to check for string when not in variable

I am trying to create a ec2 volume and attache to EC2 if device is not present.

here is my code:

vars:
      region: us-east-1
      ec2_instance_id: i-xxxxxxxx
      ec2_volume_size: 5
      ec2_volume_name: chompx
      ec2_device_name: "/dev/sdf"

tasks:
     - name: List volumes for an instance
       ec2_instance_facts:
         instance_ids: "{{ ec2_instance_id }}"
         region: "{{ region }}"
       register: ec2
     - debug: msg="{{ ec2.instances[0].block_device_mappings | list }}"

     - name: Create new volume using SSD storage
       ec2_vol:
         region: "{{ region }}"
         instance: "{{ ec2_instance_id }}"
         volume_size: "{{ ec2_volume_size }}"
         volume_type: gp2
         state: present
       when: "ec2_device_name not in ec2"

Problem, Create volume code is still getting executed even when /dev/sdf is present in ec2 variable.

Upvotes: 0

Views: 572

Answers (1)

Ryan
Ryan

Reputation: 1618

register: ec2 creates an object with all the information about the task that was run. You are trying to operate on only the results of that command.

You probably need:

when: "ec2_device_name not in ec2.stdout_lines"  # ec2.stdout_lines should be a list

But I'm not 100% sure on the output of that command. Check it by adding a debug statement:

- debug: var=ec2

Upvotes: 1

Related Questions