Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2_instance keeps default value even if set in launch template #462

Closed
emanuele-leopardi opened this issue May 5, 2021 · 8 comments
Closed
Assignees
Labels
bug This issue/PR relates to a bug has_pr jira module module plugins plugin (any type) python3 traceback

Comments

@emanuele-leopardi
Copy link

emanuele-leopardi commented May 5, 2021

SUMMARY

It looks like there are still some default values that are not taken into account by this module even if passing a launch template

ISSUE TYPE
  • Bug Report
COMPONENT NAME

ec2_instance

ANSIBLE VERSION
ansible 2.10.8
  config file = /Users/ema/Documents/bin/kl_infrastructure/ansible/bda-appquality/ansible.cfg
  configured module search path = ['/Users/ema/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.9.0 (default, Oct 27 2020, 14:15:17) [Clang 12.0.0 (clang-1200.0.32.21)]
CONFIGURATION
ANSIBLE_SSH_ARGS() = -o ConnectionAttempts=10 -o ControlMaster=auto -o ControlPersist=10m -o StrictHostKeyCh
ANSIBLE_SSH_RETRIES() = 5
DEFAULT_FORCE_HANDLERS() = True
DEFAULT_STRATEGY() = linear
DEFAULT_TIMEOUT() = 120
HOST_KEY_CHECKING() = False
RETRY_FILES_ENABLED() = False
OS / ENVIRONMENT

MacOS 11.2.3

STEPS TO REPRODUCE

First of all let's define some variables

instance:
    name: kl-{{ proj_name }}-backend
    subnet: kl-{{ proj_name }}-db-a
    ami_id: ami-fooobarrrbaaaz
    instance_type: t3.micro
    state: present
    termination_protection: false
    region: "eu-south-1" #Milan
    security_groups:
      - kl-{{ proj_name }}-backend-sg
    network:
      assign_public_ip: true
      delete_on_termination: true
      description: kl-{{ proj_name }}-backend
    env: lab

then we use this to create a nice ec2_launch_template

- name: Create | Update Launch Template
  ec2_launch_template:
    state: "{{ instance.state | default('present')}}"
    template_name: "{{ instance.name | default(omit) }}-launch-template"
    image_id: "{{ instance.ami_id }}"
    instance_type: "{{ instance.instance_type }}"
    key_name: "{{ instance.key | default('ssh_key') }}"
    region: "{{ instance.region }}"
    tags:
      Name: "{{ instance.name }}-launch-template"

we register the Id and then we use it to call ec2_instance

- name: create instance from launch_template
  ec2_instance:
    launch_template: 
      id: "{{ launch_template_id }}"
      region: "{{ instance.region }}"
EXPECTED RESULTS

The expected result is that the instance gets create with instance.instance_type == t3.micro

ACTUAL RESULTS
The full traceback is:
Traceback (most recent call last):
  File "/var/folders/rt/949k8fvs20bbvn_dv1drjv9h0000gn/T/ansible_ec2_instance_payload_li974i3k/ansible_ec2_instance_payload.zip/ansible_collections/community/aws/plugins/modules/ec2_instance.py", line 1622, in ensure_present
  File "/var/folders/rt/949k8fvs20bbvn_dv1drjv9h0000gn/T/ansible_ec2_instance_payload_li974i3k/ansible_ec2_instance_payload.zip/ansible_collections/community/aws/plugins/modules/ec2_instance.py", line 1655, in run_instances
  File "/var/folders/rt/949k8fvs20bbvn_dv1drjv9h0000gn/T/ansible_ec2_instance_payload_li974i3k/ansible_ec2_instance_payload.zip/ansible_collections/amazon/aws/plugins/module_utils/core.py", line 289, in deciding_wrapper
    return unwrapped(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 676, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (Unsupported) when calling the RunInstances operation: The requested configuration is currently not supported. Please check the documentation for supported configurations.
fatal: [localhost]: FAILED! => {
    "boto3_version": "1.17.54",
    "botocore_version": "1.20.54",

.
.
.
.
"image": null,
"image_id": null, <-- correctly unset value (I guess taken from the launch template)
"instance_ids": [],
"instance_initiated_shutdown_behavior": null,
"instance_role": null,
"instance_type": "t2.micro",   <------ default value set here that seems to override the launch template
"key_name": null,
"launch_template": {
     "id": "lt-xxxxxxxxxxxx"
 },
"name": null,
"network": null,
@tremble
Copy link
Contributor

tremble commented Aug 12, 2021

This issue was fixed by ansible-collections/community.aws#413 and should be available in the latest versions of this collection.

@tremble tremble closed this as completed Aug 12, 2021
@tremble tremble reopened this Aug 12, 2021
@tremble
Copy link
Contributor

tremble commented Aug 12, 2021

Hmm, actually, night not me

@markuman
Copy link
Member

Thats #451
Wrong collection. But thanks for reporting @emanuele-leopardi
It's a must have feature but a bigger issue.

@markuman
Copy link
Member

Wrong collection.

...because ec2_instance has moved to amazon.aws collection not a long time ago.

@tremble tremble reopened this Aug 12, 2021
@tremble tremble transferred this issue from ansible-collections/community.aws Aug 12, 2021
@ansibullbot
Copy link

@ansibullbot ansibullbot added bug This issue/PR relates to a bug module module plugins plugin (any type) python3 traceback labels Aug 12, 2021
@mandar242
Copy link
Contributor

Hi @emanuele-leopardi, could you please take a look at #587? Once it is merged, it should likely resolve this issue.

@emanuele-leopardi
Copy link
Author

Hi @emanuele-leopardi, could you please take a look at #587? Once it is merged, it should likely resolve this issue.

@mandar242 It looks good to me, thank you for your hard work :)

@mandar242 mandar242 self-assigned this Dec 26, 2021
@mandar242 mandar242 added the jira label Dec 26, 2021
ansible-zuul bot pushed a commit that referenced this issue Jan 25, 2022
… default value for instance_type (#587)

ec2_instance: Fix launch template condition, handle launch template - default value for instance_type

SUMMARY

The launch_template option in ec2_instance has a broken condition.
Also the launch_template option defaults the instance_type to t2.micro if not specified and ignores the instance_type specified in the launch_template as said in the issue #451.

Fixes

#451
#462

ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME

ec2_instance
ADDITIONAL INFORMATION

The change does not break existing functionality as tested by the integration test run locally.
Related to the condition fix in community.aws: ansible-collections/community.aws#111


Ran the following playbook to verify the change.
# create a launch template called "test-launch-template" 
    - name: create test launch template
      community.aws.ec2_launch_template:
        template_name: test-launch-template
        image_id: ami-002068ed284xxxxxx
        instance_type: t3a.small
        network_interfaces:
          - associate_public_ip_address: no
            delete_on_termination: yes
            device_index: 0
            groups:
              - sg-xxxxxxxxxxxxxxxxxx
            subnet_id: subnet-xxxxxxxxxxxxxxxxxx
        region: us-east-2
        block_device_mappings:
          - device_name: /dev/sdb
            ebs:
              volume_size: 5
              volume_type: gp3
              delete_on_termination: true
              encrypted: yes
          - device_name: /dev/sdc
            ebs:
              volume_size: 2
              volume_type: gp2
              delete_on_termination: true
              encrypted: no
        tags:
          ssome: tags

# launch a ec2 instance using launch template created earlier - launches t3a.small instance as expected
    - name: test launch template usage
      ec2_instance:
        wait: yes
        name: "test-instance-mk-t3a.small"
        launch_template:
          name: test-launch-template
        vpc_subnet_id: subnet-xxxxxxxxxxxxxxxxxx

# launch ec2 instance using launch template created earlier - override instance type to be launch to t3.xlarge
    - name: test launch template usage - override instance type
      ec2_instance:
        wait: yes
        name: "test-instance-mk-t3.xlarge"
        instance_type: t3.xlarge
        launch_template:
          name: test-launch-template
        vpc_subnet_id: subnet-xxxxxxxxxxxxxxxxxx

Reviewed-by: Jill R <None>
Reviewed-by: Markus Bergholz <[email protected]>
Reviewed-by: Emanuele Leopardi <None>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: None <None>
@mandar242
Copy link
Contributor

Closing the issue as fix is merged #587.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug has_pr jira module module plugins plugin (any type) python3 traceback
Projects
None yet
Development

No branches or pull requests

5 participants