ec2_vol parameter checking is a bit too aggressive

I am configuring a mongodb cluster on ec2. After first creating instances I create and attach an EBS volume to the instance. But I also want to be able to reattach the EBS volume to a replacement instance when needed.

desired:

`

  • name: Create mongod instance
    ec2:
    image: “{{ubuntu_image.ami}}”
    region: “{{ec2_region}}”
    vpc_subnet_id: “{{vpc_subnets.private|for_count(item)}}”
    instance_type: “{{mongod_rs_intance_type}}”
    assign_public_ip: false
    group: “{{security_groups}}”
    ebs_optimized: “{{mongod_rs_ebs_optimized}}”
    instance_tags:
    Name: “{{env_prefix}}mongod-{{item}}”
    mongod_rs_member: group
    env: “{{env}}”
    managed_by: ansible
    exact_count: 1
    count_tag:
    Name: “{{env_prefix}}mongod-{{item}}”
    key_name: “{{aws_key_name}}”
    wait: yes
    register: ec2_mongods
    with_sequence: count={{mongod_rs_instance_count}}

  • name: Attach EBS volume to mongod instance
    ec2_vol:
    instance: “{{item.1.id}}”
    name: “{{env_prefix}}mongod-{{item[0].item}}”
    volume_size: “{{mongod_rs_ebs_volume_size}}”
    volume_type: “{{mongod_rs_ebs_volume_type}}”
    device_name: “{{mongod_rs_ebs_device_name}}”
    register: ec2_vol
    with_subelements:

  • ec2_mongods.results

  • tagged_instances

`

But the ebs_vol module will error because it does not allow setting both the volume_size and name or id parameters. Looking at the code I cannot see why this need be so. There is no conflict in specifying both.

If the volume does not exist it will be created by create_volume. In that case create_volume only uses the id parameter for logging. If the volume does exist (and is not already mounted) is attached with attach_volume, which does not use the volume.

So I copied the module and commented out the offending parameter checks.

`

if not volume_size and not (id or name):

module.fail_json(msg=“You must specify an existing volume with id or name or a volume_size”)