Search code examples
ansibleansible-inventoryaws-ec2-instance-connect

The error was: Invalid type for parameter TagSpecifications, value: None, type: <class 'NoneType'>, valid types: <class 'list'>, <class 'tuple'>


Below is my .yml file when i execute the playbook getting error on Tagsepcifications not able to figure out what it exactly referring to here in my yml code

- name: provisioning Ec2 instance
  hosts: localhost
  connection: local
  tasks:
  - name: launching
    ec2_instance:
      key_name: stackkeypair
      image_id: ami-01216e7612243e0ef
      instance_type: t2.micro
      aws_region: ap-south-1
      wait: yes
      count: 1

      aws_access_key:  <Key given>
      aws_secret_key: < Key given>   


The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_ec2_instance_payload_kihlndx5/ansible_ec2_instance_payload.zip/ansible_collections/amazon/aws/plugins/modules/ec2_instance.py", line 1834, in ensure_present
  File "/tmp/ansible_ec2_instance_payload_kihlndx5/ansible_ec2_instance_payload.zip/ansible_collections/amazon/aws/plugins/modules/ec2_instance.py", line 1883, in run_instances
  File "/tmp/ansible_ec2_instance_payload_kihlndx5/ansible_ec2_instance_payload.zip/ansible_collections/amazon/aws/plugins/module_utils/core.py", line 336, in deciding_wrapper
    return retrying_wrapper(*args, **kwargs)
  File "/tmp/ansible_ec2_instance_payload_kihlndx5/ansible_ec2_instance_payload.zip/ansible_collections/amazon/aws/plugins/module_utils/cloud.py", line 118, in _retry_wrapper
    return _retry_func(
  File "/tmp/ansible_ec2_instance_payload_kihlndx5/ansible_ec2_instance_payload.zip/ansible_collections/amazon/aws/plugins/module_utils/cloud.py", line 68, in _retry_func
    return func()
  File "/home/ubuntu/.local/lib/python3.10/site-packages/botocore/client.py", line 514, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/ubuntu/.local/lib/python3.10/site-packages/botocore/client.py", line 901, in _make_api_call
    request_dict = self._convert_to_request_dict(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/botocore/client.py", line 962, in _convert_to_request_dict
    request_dict = self._serializer.serialize_to_request(
  File "/home/ubuntu/.local/lib/python3.10/site-packages/botocore/validate.py", line 381, in serialize_to_request
    raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter TagSpecifications, value: None, type: <class 'NoneType'>, valid types: <class 'list'>, <class 'tuple'>
fatal: [local]: FAILED! => {
    "boto3_version": "1.24.89",
    "botocore_version": "1.27.89",
    "changed": false,
    "invocation": {
        "module_args": {
            "availability_zone": null,
            "aws_access_key": "<key>",
            "aws_ca_bundle": null,
            "aws_config": null,
            "aws_region": "ap-south-1",
            "aws_secret_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "count": 1,
            "cpu_credit_specification": null,
            "cpu_options": null,
            "debug_botocore_endpoint_logs": false,
            "detailed_monitoring": null,
            "ebs_optimized": null,
            "ec2_url": null,
            "exact_count": null,
            "filters": {
                "image-id": [
                    "ami-01216e7612243e0ef"
                ],
                "instance-state-name": [
                    "pending",
                    "running",
                    "stopping",
                    "stopped"
                ],
                "subnet-id": "subnet-0d919639655cf3bd0"
            },
            "image": null,
            "image_id": "ami-01216e7612243e0ef",
            "instance_ids": [],
            "instance_initiated_shutdown_behavior": null,
            "instance_role": null,
            "instance_type": "t2.micro",
            "key_name": "stackkeypair",
            "launch_template": null,
            "metadata_options": null,
            "name": null,
            "network": null,
            "placement_group": null,
            "profile": null,
            "purge_tags": false,
            "region": "ap-south-1",
            "security_group": null,
            "security_groups": [],
            "security_token": null,
            "state": "present",
            "tags": null,
            "tenancy": null,
            "termination_protection": null,
            "tower_callback": null,
            "user_data": null,
            "validate_certs": true,
            "volumes": null,
            "vpc_subnet_id": null,
            "wait": true,
            "wait_timeout": 600
        }
    },
    "msg": "Failed to create new EC2 instance: Parameter validation failed:\nInvalid type for parameter TagSpecifications, value: None, type: <class 'NoneType'>, valid types: <class 'list'>, <class 'tuple'>"
}

The above is the trace am not getting what exactly TagSepcification here

it is working when i give ec2: instead of ec2_instance and image instead of image_id but getting warning that ec2 is depcreated

my version of anisble

==Versions i have used ansible [core 2.13.4]

python version = 3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0] jinja version = 3.0.3 "boto3_version": "1.24.89", "botocore_version": "1.27.89" amazon.aws 3.4.0


Solution

  • It seems there are a couple of answers to this, and what appears to be a bug in either the amazon.aws collection, or botocore itself. We'll start with getting you unstuck: you can add a name: to that one instance and/or add an empty tags: {} dict to the task params

    option 1: add a name: to that one instance:

        ec2_instance:
          name: my-instance  # <-- it doesn't matter what, just something
          key_name: stackkeypair
          image_id: ami-01216e7612243e0ef
          instance_type: t2.micro
          aws_region: ap-south-1
          wait: yes
          count: 1
    

    by doing that, it special-cases tag creation by adding Key=Name,Value=my-instance and it doesn't fail validation anymore

    option 2: add an empty tags: {} dict to the task params

        ec2_instance:
          key_name: stackkeypair
          image_id: ami-01216e7612243e0ef
          instance_type: t2.micro
          aws_region: ap-south-1
          wait: yes
          count: 1
          tags: {}
    

    this is probably the easiest, because it seems to side-step whatever bug exists in the amazon.aws collection

    One will observe that all of their creation examples have tags: on them, so that's likely why this bug slipped through for so long

    amazon.aws bug

    1. TagSpecifications is set from build_instance_tags
    2. but it just returns boto3_tag_specifications
    3. but it can return None, which makes botocore mad

    thus, they should None-check that result before blindly assigning it to TagSpecifications