ansible-community / molecule-plugins

Collection on molecule plugins
MIT License
109 stars 73 forks source link

Unable to create ec2 instance with default create template with error `The filter '<' is invalid` #40

Open gionn opened 2 years ago

gionn commented 2 years ago

Hello,

I tried to create a new scenario with molecule init scenario -d ec2, and then running molecule create:

PLAY [Create] **********************************************************************************************************

TASK [Validate platform configurations] ********************************************************************************
ok: [localhost] => (item=instance)

TASK [Write run config to file] ****************************************************************************************
ok: [localhost]

TASK [Generate local key pairs] ****************************************************************************************
ok: [localhost] => (item=instance)

TASK [Look up EC2 AMI(s) by owner and name (if image not set)] *********************************************************
skipping: [localhost] => (item=instance)

TASK [Look up subnets to determine VPCs (if needed)] *******************************************************************
ok: [localhost] => (item=instance)

TASK [Validate discovered information] *********************************************************************************
ok: [localhost] => (item=instance)

TASK [Create ephemeral EC2 keys (if needed)] ***************************************************************************
skipping: [localhost] => (item=instance)

TASK [Create ephemeral security groups (if needed)] ********************************************************************
ok: [localhost] => (item=instance)

TASK [Create ephemeral EC2 instance(s)] ********************************************************************************
changed: [localhost] => (item=instance)

TASK [Wait for instance creation to complete] **************************************************************************
FAILED - RETRYING: Wait for instance creation to complete (300 retries left).

I always get an AWS client exception (truncate output for readability):

return self._method(**current_kwargs)", "  File \"/Users/Giovanni.Toraldo/.pyenv/versions/3.9.0/lib/python3.9/site-packages/botocore/client.py\", line 391, in _api_call", "    return self._make_api_call(operation_name, kwargs)", "  File \"/Users/Giovanni.Toraldo/.pyenv/versions/3.9.0/lib/python3.9/site-packages/botocore/client.py\", line 719, in _make_api_call", "    raise error_class(parsed_response, operation_name)", "botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the DescribeInstances operation: The filter '<' is invalid"]}

Relevant dependencies:

ansible==2.9.21
molecule==3.5.2
molecule-docker==1.1.0
molecule-ec2==0.4
boto3==1.20.46
botocore==1.23.46

Workaround is to remove the filters attribute in the ec2_instance resource in the Create ephemeral EC2 instance(s) step in create.yml:

    - name: Create ephemeral EC2 instance(s)
      ec2_instance:
        profile: "{{ item.aws_profile | default(omit) }}"
        region: "{{ item.region | default(omit) }}"
        #filters: "{{ platform_filters }}"
        instance_type: "{{ item.instance_type }}"
        image_id: "{{ platform_image_id }}"
        vpc_subnet_id: "{{ item.vpc_subnet_id }}"
gionn commented 2 years ago

A better workaround seems to set name attribute in the ec2_instance so there is no need to fiddle with filters that documentation says it's needed just to decide if creating a new instance or update an existing one, and the default is to look for a matching Name tag value, but it needs to be specified with name otherwise it won't work if defined in tags alone.

    - name: Create ephemeral EC2 instance(s)
      ec2_instance:
        profile: "{{ item.aws_profile | default(omit) }}"
        region: "{{ item.region | default(omit) }}"
        instance_type: "{{ item.instance_type }}"
        image_id: "{{ platform_image_id }}"
        vpc_subnet_id: "{{ item.vpc_subnet_id }}"
        security_groups: "{{ platform_security_groups }}"
        network:
          assign_public_ip: "{{ item.assign_public_ip }}"
        volumes: "{{ item.volumes }}"
        key_name: "{{ (item.key_inject_method == 'ec2') | ternary(item.key_name, omit) }}"
        name: "{{ platform_tags['Name'] }}"
        tags: "{{ platform_tags }}"
        user_data: "{{ platform_user_data }}"
        wait: true