
    Vh                    t   d Z dZdZddlZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ 	 ddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddlm"Z" ddlm#Z# ddlm$Z$ ddlm%Z% ddlm&Z& ddlm'Z' dd lm(Z) dd!lm*Z* dd"lm+Z+ dd#lm,Z, dd$l-m.Z. dd%l-m/Z/ dd&l-m0Z0 dd'l1m2Z2 dd(l3m4Z4 dd)l5m6Z6 dd*l5m7Z7 dd+l8m9Z9 dd,l:m;Z; d-e	e<ef   d.e
e	e<ef      fd/Z=d0e2d1e	e<ef   d2e<d.e>fd3Z?d0e2d.dfd4Z@d0e2d5eAd.dfd6ZBd0e2d7e	e<ef   d8ee<   d9eee
e<   e<f      d.e	e<ef   f
d:ZCd0e2d.e
e	e<ef      fd;ZDd0e2d1e	e<ef   d.dfd<ZEd0e2d1e	e<ef   d.dfd=ZF	 	 dfd0e2d9ee<e
e<   f   d>ee<   d8ee<   d.e
e<   f
d?ZGd@ ZHd0e2d.e	e<ef   fdAZId-e	e<ef   d.e
e	e<ef      fdBZJdgd0e2dCeAd.e	e<ef   fdDZK	 dhd0e2dEe
e<   dFe<dGe>d.df
dHZL	 did0e2d1e	e<ef   dIee
e<      d.e
e	e<ef      fdJZMd0e2d1e	e<e<f   d.e>fdKZNd0e2d1e	e<ef   d.e>fdLZO	 dfd0e2dEee
e<      dMee	e<ef      d.e
e	e<ef      fdNZPd0e2d.ee	e<ef      fdOZQ	 did0e2dPe	e<ef   d.ee	e<ef      fdQZRd0e2dFe<dMee	e<ef      d.e	e<ef   fdRZSd0e2dMee	e<ef      dFe<d.eee<   e
e<   ee
e	e<ef         e<f   fdSZTdT ZUd0e2dUe<dVe<dWe	e<e	e<e<f   f   d.df
dXZVd0e2dUe<dWe
e	e<ef      d.dfdYZWd0e2dZe
e	e<ef      dUe<dMee	e<ef      d.e	e<ef   f
d[ZXd0e2dZe
e	e<ef      dFe<dMee	e<ef      d.e	e<ef   f
d\ZY	 did0e2dZee
e	e<ef         dFe<dCeeA   d.e	e<ef   f
d]ZZd^e	e<ef   d.e	e<ef   fd_Z(djd0e2d`e>d.e<fdaZ[d0e2d.e	e<e
e<   f   fdbZ\d0e2d.e	e<ef   fdcZ]dd Z^e_dek(  r e^        yy# e$ r Y rw xY w)kaR  
---
module: ec2_instance
version_added: 1.0.0
short_description: Create & manage EC2 instances
description:
  - Create and manage AWS EC2 instances.
  - This module does not support creating
    L(EC2 Spot instances,https://aws.amazon.com/ec2/spot/).
  - The M(amazon.aws.ec2_spot_instance) module can create and manage spot instances.
author:
  - Ryan Scott Brown (@ryansb)
options:
  instance_ids:
    description:
      - If you specify one or more instance IDs, only instances that have the specified IDs are returned.
      - Mutually exclusive with O(exact_count).
    type: list
    elements: str
    default: []
  state:
    description:
      - Goal state for the instances.
      - "O(state=present): ensures instances exist, but does not guarantee any state (e.g. running). Newly-launched instances will be run by EC2."
      - "O(state=running): O(state=present) + ensures the instances are running."
      - "O(state=started): O(state=running) + waits for EC2 status checks to report OK if O(wait=true)."
      - "O(state=stopped): ensures an existing instance is stopped."
      - "O(state=rebooted): convenience alias for O(state=stopped) immediately followed by O(state=running)."
      - "O(state=restarted): convenience alias for O(state=stopped) immediately followed by O(state=started)."
      - "O(state=terminated): ensures an existing instance is terminated."
      - "O(state=absent): alias for O(state=terminated)."
    choices: [present, terminated, running, started, stopped, restarted, rebooted, absent]
    default: present
    type: str
  wait:
    description:
      - Whether or not to wait for the desired O(state) (use O(wait_timeout) to customize this).
    default: true
    type: bool
  wait_timeout:
    description:
      - How long to wait (in seconds) for the instance to finish booting/terminating.
    default: 600
    type: int
  instance_type:
    description:
      - Instance type to use for the instance, see
        U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html).
      - At least one of O(instance_type) or O(launch_template) must be specificed when launching an
        instance.
      - When the instance is present and the O(instance_type) specified value is different from the current value,
        the instance will be stopped and the instance type will be updated.
    type: str
  count:
    description:
      - Number of instances to launch.
      - Setting this value will result in always launching new instances.
      - Mutually exclusive with O(exact_count).
    type: int
    version_added: 2.2.0
  exact_count:
    description:
      - An integer value which indicates how many instances that match the O(filters) parameter should be running.
      - Instances are either created or terminated based on this value.
      - If termination takes place, least recently created instances will be terminated based on Launch Time.
      - Mutually exclusive with O(count), O(instance_ids).
    type: int
    version_added: 2.2.0
  user_data:
    description:
      - Opaque blob of data which is made available to the EC2 instance.
    type: str
  aap_callback:
    description:
      - Preconfigured user-data to enable an instance to perform an Ansible Automation Platform
        callback (Linux only).
      - For Windows instances, to enable remote access via Ansible set O(aap_callback.windows) to V(true), and
        optionally set an admin password.
      - If using O(aap_callback.windows) and O(aap_callback.set_password), callback to Ansible Automation Platform will not
        be performed but the instance will be ready to receive winrm connections from Ansible.
      - Mutually exclusive with O(user_data).
    type: dict
    aliases: ['tower_callback']
    suboptions:
      windows:
        description:
          - Set O(aap_callback.windows=True) to use powershell instead of bash for the callback script.
        type: bool
        default: False
      set_password:
        description:
          - Optional admin password to use if O(aap_callback.windows=True).
        type: str
      tower_address:
        description:
          - IP address or DNS name of Tower server. Must be accessible via this address from the
            VPC that this instance will be launched in.
          - Required if O(aap_callback.windows=False).
        type: str
      job_template_id:
        description:
          - Either the integer ID of the Tower Job Template, or the name.
            Using a name for the job template is not supported by Ansible Tower prior to version
            3.2.
          - Required if O(aap_callback.windows=False).
        type: str
      host_config_key:
        description:
          - Host configuration secret key generated by the Tower job template.
          - Required if O(aap_callback.windows=False).
        type: str
  image:
    description:
      - An image to use for the instance. The M(amazon.aws.ec2_ami_info) module may be used to retrieve images.
        One of O(image) or O(image_id) are required when instance is not already present.
    type: dict
    suboptions:
      id:
        description:
        - The AMI ID.
        type: str
      ramdisk:
        description:
        - Overrides the AMI's default ramdisk ID.
        type: str
      kernel:
        description:
        - a string AKI to override the AMI kernel.
        type: str
  image_id:
    description:
       - I(ami) ID to use for the instance. One of O(image) or O(image_id) are required when instance is not already present.
       - This is an alias for O(image.id).
    type: str
  security_groups:
    description:
      - A list of security group IDs or names (strings).
      - Mutually exclusive with O(security_group).
      - Mutually exclusive with O(network_interfaces_ids).
    type: list
    elements: str
    default: []
  security_group:
    description:
      - A security group ID or name.
      - Mutually exclusive with O(security_groups).
      - Mutually exclusive with O(network_interfaces_ids).
    type: str
  name:
    description:
      - The Name tag for the instance.
    type: str
  vpc_subnet_id:
    description:
      - The subnet ID in which to launch the instance (VPC).
      - If none is provided, M(amazon.aws.ec2_instance) will chose the default zone of the default VPC.
      - If used along with O(network), O(vpc_subnet_id) is used as a fallback to prevent errors when O(network.subnet_id) is not specified.
    aliases: ['subnet_id']
    type: str
  network:
    description:
      - Either a dictionary containing the key C(interfaces) corresponding to a list of network interface IDs or
        containing specifications for a single network interface.
      - Use the M(amazon.aws.ec2_eni) module to create ENIs with special settings.
      - This field is deprecated and will be removed in a release after 2026-12-01, use O(network_interfaces) or O(network_interfaces_ids) instead.
      - Mutually exclusive with O(network_interfaces).
      - Mutually exclusive with O(network_interfaces_ids).
      - If used along with O(vpc_subnet_id), O(vpc_subnet_id)  is used as a fallback to prevent errors when O(network.subnet_id) is not specified.
    type: dict
    suboptions:
      interfaces:
        description:
          - A list of ENI IDs (strings) or a list of objects containing the key id.
        type: list
        elements: str
      assign_public_ip:
        description:
          - When C(true) assigns a public IP address to the interface.
        type: bool
      private_ip_address:
        description:
          - An IPv4 address to assign to the interface.
        type: str
      ipv6_addresses:
        description:
          - A list of IPv6 addresses to assign to the network interface.
        type: list
        elements: str
      source_dest_check:
        description:
          - Controls whether source/destination checking is enabled on the interface.
          - This field with be ignored when O(source_dest_check) is provided.
        type: bool
      description:
        description:
          - A description for the network interface.
        type: str
      private_ip_addresses:
        description:
          - A list of IPv4 addresses to assign to the network interface.
        type: list
        elements: str
      subnet_id:
        description:
          - The subnet to connect the network interface to.
        type: str
      delete_on_termination:
        description:
          - Delete the interface when the instance it is attached to is
            terminated.
        type: bool
      device_index:
        description:
          - The index of the interface to modify.
        type: int
      groups:
        description:
          - A list of security group IDs to attach to the interface.
        type: list
        elements: str
  source_dest_check:
    description:
      - Controls whether source/destination checking is enabled on the interface.
    type: bool
    version_added: 8.2.0
  network_interfaces:
    description:
      - A list of dictionaries containing specifications for network interfaces.
      - Use the M(amazon.aws.ec2_eni) module to create ENIs with special settings.
      - Mutually exclusive with O(network).
    type: list
    elements: dict
    version_added: 8.2.0
    suboptions:
      assign_public_ip:
        description:
          - When V(true) assigns a public IP address to the interface.
        type: bool
      private_ip_address:
        description:
          - An IPv4 address to assign to the interface.
        type: str
      ipv6_addresses:
        description:
          - A list of IPv6 addresses to assign to the network interface.
        type: list
        elements: str
      description:
        description:
          - A description for the network interface.
        type: str
      subnet_id:
        description:
          - The subnet to connect the network interface to.
        type: str
      delete_on_termination:
        description:
          - Delete the interface when the instance it is attached to is terminated.
        type: bool
        default: True
      device_index:
        description:
          - The position of the network interface in the attachment order.
          - Use device index V(0) for a primary network interface.
        type: int
        default: 0
      groups:
        description:
          - A list of security group IDs or names to attach to the interface.
        type: list
        elements: str
      private_ip_addresses:
        description:
          - A list of private IPv4 addresses to assign to the network interface.
          - Only one private IPv4 address can be designated as primary.
          - You cannot specify this option if you're launching more than one instance.
        type: list
        elements: dict
        suboptions:
          private_ip_address:
            description:
              - The private IPv4 address.
            type: str
            required: true
          primary:
            description:
              - Indicates whether the private IPv4 address is the primary private IPv4 address.
              - Only one IPv4 address can be designated as primary.
            type: bool
  network_interfaces_ids:
    description:
      - A list of ENI ids to attach to the instance.
      - Mutually exclusive with O(network).
      - Mutually exclusive with O(security_group).
      - Mutually exclusive with O(security_groups).
    type: list
    elements: dict
    version_added: 8.2.0
    suboptions:
      id:
        description:
          - The ID of the network interface.
        type: str
        required: True
      device_index:
        description:
          - The position of the network interface in the attachment order.
        type: int
        default: 0
  volumes:
    description:
      - A list of block device mappings, by default this will always use the AMI root device so the volumes option is primarily for adding more storage.
      - A mapping contains the (optional) keys V(device_name), V(virtual_name), V(ebs.volume_type), V(ebs.volume_size), V(ebs.kms_key_id),
        V(ebs.snapshot_id), V(ebs.iops), and V(ebs.delete_on_termination).
      - For more information about each parameter, see U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_BlockDeviceMapping.html).
    type: list
    elements: dict
  launch_template:
    description:
      - The EC2 launch template to base instance configuration on.
      - At least one of O(instance_type) or O(launch_template) must be specificed when launching an
        instance.
    type: dict
    suboptions:
      id:
        description:
          - The ID of the launch template (optional if name is specified).
        type: str
      name:
        description:
          - The pretty name of the launch template (optional if id is specified).
        type: str
      version:
        description:
          - The specific version of the launch template to use. If unspecified, the template default is chosen.
  key_name:
    description:
      - Name of the SSH access key to assign to the instance - must exist in the region the instance is created.
      - Use M(amazon.aws.ec2_key) to manage SSH keys.
    type: str
  availability_zone:
    description:
      - Specify an availability zone to use the default subnet it. Useful if not specifying the O(vpc_subnet_id) parameter.
      - If no subnet, ENI, or availability zone is provided, the default subnet in the default VPC will be used in the first AZ (alphabetically sorted).
    type: str
  instance_initiated_shutdown_behavior:
    description:
      - Whether to stop or terminate an instance upon shutdown.
    choices: ['stop', 'terminate']
    type: str
  tenancy:
    description:
      - What type of tenancy to allow an instance to use. Default is V(shared) tenancy. Dedicated tenancy will incur additional charges.
      - This field is deprecated and will be removed in a release after 2025-12-01, use O(placement) instead.
    choices: ['dedicated', 'default']
    type: str
  termination_protection:
    description:
      - Whether to enable termination protection.
      - This module will not terminate an instance with termination protection active, it must be turned off first.
    type: bool
  hibernation_options:
    description:
      - Indicates whether an instance is enabled for hibernation.
        Refer U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.html)
        for Hibernation prerequisits.
    type: bool
    default: False
    version_added: 5.0.0
  cpu_credit_specification:
    description:
      - For T series instances, choose whether to allow increased charges to buy CPU credits if the default pool is depleted.
      - Choose V(unlimited) to enable buying additional CPU credits.
    choices: ['unlimited', 'standard']
    type: str
  cpu_options:
    description:
      - Reduce the number of vCPU exposed to the instance.
      - Those parameters can only be set at instance launch. The two suboptions O(cpu_options.threads_per_core) and  O(cpu_options.core_count) are mandatory.
      - See U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-optimize-cpu.html) for combinations available.
    type: dict
    suboptions:
      threads_per_core:
        description:
          - Select the number of threads per core to enable. Disable or Enable Intel HT.
        choices: [1, 2]
        required: true
        type: int
      core_count:
        description:
          - Set the number of core to enable.
        required: true
        type: int
  detailed_monitoring:
    description:
      - Whether to allow detailed CloudWatch metrics to be collected, enabling more detailed alerting.
    type: bool
  ebs_optimized:
    description:
      - Whether instance is should use optimized EBS volumes, see U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSOptimized.html).
    type: bool
  filters:
    description:
      - A dict of filters to apply when deciding whether existing instances match and should be altered. Each dict item
        consists of a filter key and a filter value. See
        U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html).
        for possible filters. Filter names and values are case sensitive.
      - By default, instances are filtered for counting by their "Name" tag, base AMI, state (running, by default), and
        subnet ID. Any queryable filter can be used. Good candidates are specific tags, SSH keys, or security groups.
    type: dict
  iam_instance_profile:
    description:
      - The ARN or name of an EC2-enabled IAM instance profile to be used.
      - If a name is not provided in ARN format then the ListInstanceProfiles permission must also be granted.
        U(https://docs.aws.amazon.com/IAM/latest/APIReference/API_ListInstanceProfiles.html)
      - If no full ARN is provided, the role with a matching name will be used from the active AWS account.
    type: str
    aliases: ['instance_role']
  placement_group:
    description:
      - The placement group that needs to be assigned to the instance.
      - This field is deprecated and will be removed in a release after 2025-12-01, use O(placement) instead.
    type: str
  placement:
    description:
      - The location where the instance launched, if applicable.
    type: dict
    version_added: 7.0.0
    suboptions:
      affinity:
        description: The affinity setting for the instance on the Dedicated Host.
        type: str
        required: false
      availability_zone:
        description: The Availability Zone of the instance.
        type: str
        required: false
      group_name:
        description: The name of the placement group the instance is in.
        type: str
        required: false
      host_id:
        description: The ID of the Dedicated Host on which the instance resides.
        type: str
        required: false
      host_resource_group_arn:
        description: The ARN of the host resource group in which to launch the instances.
        type: str
        required: false
      partition_number:
        description: The number of the partition the instance is in.
        type: int
        required: false
      tenancy:
        description:
          - Type of tenancy to allow an instance to use. Default is shared tenancy. Dedicated tenancy will incur additional charges.
          - Support for O(tenancy=host) was added in amazon.aws 7.6.0.
        type: str
        required: false
        choices: ['dedicated', 'default', 'host']
  license_specifications:
    description:
      - The license specifications to be used for the instance.
    type: list
    elements: dict
    suboptions:
      license_configuration_arn:
        description: The Amazon Resource Name (ARN) of the license configuration.
        type: str
        required: true
  additional_info:
    description:
      - Reserved for Amazon's internal use.
    type: str
    version_added: 7.1.0
  metadata_options:
    description:
      - Modify the metadata options for the instance.
      - See U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) for more information.
      - The two suboptions O(metadata_options.http_endpoint) and O(metadata_options.http_tokens) are supported.
    type: dict
    version_added: 2.0.0
    suboptions:
      http_endpoint:
        description:
          - Enables or disables the HTTP metadata endpoint on instances.
          - If specified a value of disabled, metadata of the instance will not be accessible.
        choices: [enabled, disabled]
        default: enabled
        type: str
      http_tokens:
        description:
          - Set the state of token usage for instance metadata requests.
          - If the state is optional (v1 and v2), instance metadata can be retrieved with or without a signed token header on request.
          - If the state is required (v2), a signed token header must be sent with any instance metadata retrieval requests.
        choices: [optional, required]
        default: optional
        type: str
      http_put_response_hop_limit:
        version_added: 4.0.0
        type: int
        description:
          - The desired HTTP PUT response hop limit for instance metadata requests.
          - The larger the number, the further instance metadata requests can travel.
        default: 1
      http_protocol_ipv6:
        version_added: 4.0.0
        type: str
        description:
          - Whether the instance metadata endpoint is available via IPv6 (V(enabled)) or not (V(disabled)).
        choices: [enabled, disabled]
        default: 'disabled'
      instance_metadata_tags:
        version_added: 4.0.0
        type: str
        description:
          - Whether the instance tags are availble (V(enabled)) via metadata endpoint or not (V(disabled)).
        choices: [enabled, disabled]
        default: 'disabled'

extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.tags
  - amazon.aws.boto3
a2  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- name: Terminate every running instance in a region. Use with EXTREME caution.
  amazon.aws.ec2_instance:
    state: absent
    filters:
      instance-state-name: running

- name: restart a particular instance by its ID
  amazon.aws.ec2_instance:
    state: restarted
    instance_ids:
      - i-12345678

- name: start an instance with a public IP address
  amazon.aws.ec2_instance:
    name: "public-compute-instance"
    key_name: "prod-ssh-key"
    vpc_subnet_id: subnet-5ca1ab1e
    instance_type: c5.large
    security_group: default
    network_interfaces:
      - assign_public_ip: true
    image_id: ami-123456
    tags:
      Environment: Testing

- name: start an instance and Add EBS
  amazon.aws.ec2_instance:
    name: "public-withebs-instance"
    vpc_subnet_id: subnet-5ca1ab1e
    instance_type: t2.micro
    key_name: "prod-ssh-key"
    security_group: default
    volumes:
      - device_name: /dev/sda1
        ebs:
          volume_size: 16
          delete_on_termination: true

- name: start an instance and Add EBS volume from a snapshot
  amazon.aws.ec2_instance:
    name: "public-withebs-instance"
    instance_type: t2.micro
    image_id: ami-1234567890
    vpc_subnet_id: subnet-5ca1ab1e
    volumes:
      - device_name: /dev/sda2
        ebs:
          snapshot_id: snap-1234567890

- name: Create EC2 instance with termination protection turned on
  amazon.aws.ec2_instance:
    name: "my-ec2-instance"
    vpc_subnet_id: subnet-5ca1ab1e
    instance_type: t3.small
    image_id: ami-123456
    termination_protection: true
    wait: true

- name: start an instance with a cpu_options
  amazon.aws.ec2_instance:
    name: "public-cpuoption-instance"
    vpc_subnet_id: subnet-5ca1ab1e
    tags:
      Environment: Testing
    instance_type: c4.large
    volumes:
      - device_name: /dev/sda1
        ebs:
          delete_on_termination: true
    cpu_options:
      core_count: 1
      threads_per_core: 1

- name: start an instance and have it begin a Tower callback on boot
  amazon.aws.ec2_instance:
    name: "tower-callback-test"
    key_name: "prod-ssh-key"
    vpc_subnet_id: subnet-5ca1ab1e
    security_group: default
    tower_callback:
      # IP or hostname of tower server
      tower_address: 1.2.3.4
      job_template_id: 876
      host_config_key: '[secret config key goes here]'
    network_interfaces:
      - assign_public_ip: true
    image_id: ami-123456
    cpu_credit_specification: unlimited
    tags:
      SomeThing: "A value"

- name: start an instance with ENI (An existing ENI ID is required)
  amazon.aws.ec2_instance:
    name: "public-eni-instance"
    key_name: "prod-ssh-key"
    vpc_subnet_id: subnet-5ca1ab1e
    network_interfaces_ids:
      - id: "eni-12345"
        device_index: 0
    tags:
      Env: "eni_on"
    volumes:
      - device_name: /dev/sda1
        ebs:
          delete_on_termination: true
    instance_type: t2.micro
    image_id: ami-123456

- name: add second ENI interface
  amazon.aws.ec2_instance:
    name: "public-eni-instance"
    network_interfaces_ids:
      - id: "eni-12345"
        device_index: 0
      - id: "eni-67890"
        device_index: 1
    image_id: ami-123456
    tags:
      Env: "eni_on"
    instance_type: t2.micro

- name: start an instance with metadata options
  amazon.aws.ec2_instance:
    name: "public-metadataoptions-instance"
    vpc_subnet_id: subnet-5calable
    instance_type: t3.small
    image_id: ami-123456
    tags:
      Environment: Testing
    metadata_options:
      http_endpoint: enabled
      http_tokens: optional

# ensure number of instances running with a tag matches exact_count
- name: start multiple instances
  amazon.aws.ec2_instance:
    instance_type: t3.small
    image_id: ami-123456
    exact_count: 5
    region: us-east-2
    vpc_subnet_id: subnet-0123456
    network_interfaces:
      - assign_public_ip: true
        groups:
          - default
    tags:
      foo: bar

# launches multiple instances - specific number of instances
- name: start specific number of multiple instances
  amazon.aws.ec2_instance:
    instance_type: t3.small
    image_id: ami-123456
    count: 3
    region: us-east-2
    network_interfaces:
      - assign_public_ip: true
        groups:
          - default
        subnet_id: subnet-0123456
    state: present
    tags:
      foo: bar

# launches an instance with a primary and a secondary network interfaces
- name: start an instance with a primary and secondary network interfaces
  amazon.aws.ec2_instance:
    instance_type: t2.large
    image_id: ami-123456
    region: us-east-2
    network_interfaces:
      - assign_public_ip: true
        groups:
          - default
        subnet_id: subnet-0123456
        private_ip_addresses:
          - primary: true
            private_ip_address: 168.50.4.239
          - primary: false
            private_ip_address: 168.50.4.237
    state: present
    tags:
      foo: bar

# launches a mac instance with HostResourceGroupArn and LicenseSpecifications
- name: start a mac instance with a host resource group and license specifications
  amazon.aws.ec2_instance:
    name: "mac-compute-instance"
    key_name: "prod-ssh-key"
    vpc_subnet_id: subnet-5ca1ab1e
    instance_type: mac1.metal
    security_group: default
    placement:
      host_resource_group_arn: arn:aws:resource-groups:us-east-1:123456789012:group/MyResourceGroup
    license_specifications:
      - license_configuration_arn: arn:aws:license-manager:us-east-1:123456789012:license-configuration:lic-0123456789
    image_id: ami-123456
    tags:
      Environment: Testing
ab  
instance_ids:
    description: A list of EC2 instance IDs matching the provided specification and filters.
    returned: always
    type: list
    sample: ["i-0123456789abcdef0", "i-0123456789abcdef1"]
    version_added: 5.3.0
changed_ids:
    description: A list of the set of EC2 instance IDs changed by the module action.
    returned: when instances that must be present are launched
    type: list
    sample: ["i-0123456789abcdef0"]
    version_added: 5.3.0
terminated_ids:
    description: A list of the set of EC2 instance IDs terminated by the module action.
    returned: when instances that must be absent are terminated
    type: list
    sample: ["i-0123456789abcdef1"]
    version_added: 5.3.0
instances:
    description: A list of EC2 instances.
    returned: when O(wait=true) or when matching instances already exist
    type: complex
    contains:
        ami_launch_index:
            description: The AMI launch index, which can be used to find this instance in the launch group.
            returned: always
            type: int
            sample: 0
        architecture:
            description: The architecture of the image.
            returned: always
            type: str
            sample: x86_64
        block_device_mappings:
            description: Any block device mapping entries for the instance.
            returned: always
            type: complex
            contains:
                device_name:
                    description: The device name exposed to the instance (for example, /dev/sdh or xvdh).
                    returned: always
                    type: str
                    sample: /dev/sdh
                ebs:
                    description: Parameters used to automatically set up EBS volumes when the instance is launched.
                    returned: always
                    type: complex
                    contains:
                        attach_time:
                            description: The time stamp when the attachment initiated.
                            returned: always
                            type: str
                            sample: "2017-03-23T22:51:24+00:00"
                        delete_on_termination:
                            description: Indicates whether the volume is deleted on instance termination.
                            returned: always
                            type: bool
                            sample: true
                        status:
                            description: The attachment state.
                            returned: always
                            type: str
                            sample: attached
                        volume_id:
                            description: The ID of the EBS volume.
                            returned: always
                            type: str
                            sample: vol-12345678
        capacity_reservation_specification:
            description: Information about the Capacity Reservation targeting option.
            type: complex
            contains:
                capacity_reservation_preference:
                    description: Describes the Capacity Reservation preferences.
                    type: str
                    sample: open
        client_token:
            description: The idempotency token you provided when you launched the instance, if applicable.
            returned: always
            type: str
            sample: mytoken
        cpu_options:
            description: The CPU options for the instance.
            type: complex
            contains:
                core_count:
                    description: The number of CPU cores for the instance.
                    type: int
                    sample: 1
                threads_per_core:
                    description: The number of threads per CPU core.
                    type: int
                    sample: 2
                amd_sev_snp:
                    description: Indicates whether the instance is enabled for AMD SEV-SNP.
                    type: str
                    sample: enabled
        current_instance_boot_mode:
            description: The boot mode that is used to boot the instance at launch or start.
            type: str
            sample: legacy-bios
        ebs_optimized:
            description: Indicates whether the instance is optimized for EBS I/O.
            returned: always
            type: bool
            sample: false
        ena_support:
            description: Specifies whether enhanced networking with ENA is enabled.
            returned: always
            type: bool
            sample: true
        enclave_options:
            description: Indicates whether the instance is enabled for Amazon Web Services Nitro Enclaves.
            type: dict
            contains:
                enabled:
                    description: If this parameter is set to true, the instance is enabled for Amazon Web Services Nitro Enclaves.
                    returned: always
                    type: bool
                    sample: false
        hibernation_options:
            description: Indicates whether the instance is enabled for hibernation.
            type: dict
            contains:
                configured:
                    description: If true, your instance is enabled for hibernation; otherwise, it is not enabled for hibernation.
                    returned: always
                    type: bool
                    sample: false
        hypervisor:
            description: The hypervisor type of the instance.
            returned: always
            type: str
            sample: xen
        iam_instance_profile:
            description: The IAM instance profile associated with the instance, if applicable.
            returned: always
            type: complex
            contains:
                arn:
                    description: The Amazon Resource Name (ARN) of the instance profile.
                    returned: always
                    type: str
                    sample: "arn:aws:iam::123456789012:instance-profile/myprofile"
                id:
                    description: The ID of the instance profile.
                    returned: always
                    type: str
                    sample: JFJ397FDG400FG9FD1N
        image_id:
            description: The ID of the AMI used to launch the instance.
            returned: always
            type: str
            sample: ami-0011223344
        instance_id:
            description: The ID of the instance.
            returned: always
            type: str
            sample: i-012345678
        instance_type:
            description: The instance type size of the running instance.
            returned: always
            type: str
            sample: t2.micro
        key_name:
            description: The name of the key pair, if this instance was launched with an associated key pair.
            returned: always
            type: str
            sample: my-key
        launch_time:
            description: The time the instance was launched.
            returned: always
            type: str
            sample: "2017-03-23T22:51:24+00:00"
        licenses:
            description: The license configurations for the instance.
            returned: When license specifications are provided.
            type: list
            elements: dict
            contains:
                license_configuration_arn:
                    description: The Amazon Resource Name (ARN) of the license configuration.
                    returned: always
                    type: str
                    sample: arn:aws:license-manager:us-east-1:123456789012:license-configuration:lic-0123456789
        metadata_options:
            description: The metadata options for the instance.
            returned: always
            type: complex
            contains:
                http_endpoint:
                    description: Indicates whether the HTTP metadata endpoint on your instances is enabled or disabled.
                    type: str
                    sample: enabled
                http_protocol_ipv6:
                    description: Indicates whether the IPv6 endpoint for the instance metadata service is enabled or disabled.
                    type: str
                    sample: disabled
                http_put_response_hop_limit:
                    description: The maximum number of hops that the metadata token can travel.
                    type: int
                    sample: 1
                http_tokens:
                    description: Indicates whether IMDSv2 is required.
                    type: str
                    sample: optional
                instance_metadata_tags:
                    description: Indicates whether access to instance tags from the instance metadata is enabled or disabled.
                    type: str
                    sample: disabled
                state:
                    description: The state of the metadata option changes.
                    type: str
                    sample: applied
        monitoring:
            description: The monitoring for the instance.
            returned: always
            type: complex
            contains:
                state:
                    description: Indicates whether detailed monitoring is enabled. Otherwise, basic monitoring is enabled.
                    returned: always
                    type: str
                    sample: disabled
        network_interfaces:
            description: One or more network interfaces for the instance.
            returned: always
            type: list
            elements: dict
            contains:
                association:
                    description: The association information for an Elastic IPv4 associated with the network interface.
                    returned: always
                    type: complex
                    contains:
                        ip_owner_id:
                            description: The ID of the owner of the Elastic IP address.
                            returned: always
                            type: str
                            sample: amazon
                        public_dns_name:
                            description: The public DNS name.
                            returned: always
                            type: str
                            sample: ""
                        public_ip:
                            description: The public IP address or Elastic IP address bound to the network interface.
                            returned: always
                            type: str
                            sample: 1.2.3.4
                attachment:
                    description: The network interface attachment.
                    returned: always
                    type: complex
                    contains:
                        attach_time:
                            description: The time stamp when the attachment initiated.
                            returned: always
                            type: str
                            sample: "2017-03-23T22:51:24+00:00"
                        attachment_id:
                            description: The ID of the network interface attachment.
                            returned: always
                            type: str
                            sample: eni-attach-3aff3f
                        delete_on_termination:
                            description: Indicates whether the network interface is deleted when the instance is terminated.
                            returned: always
                            type: bool
                            sample: true
                        device_index:
                            description: The index of the device on the instance for the network interface attachment.
                            returned: always
                            type: int
                            sample: 0
                        network_card_index:
                            description: The index of the network card.
                            returned: always
                            type: int
                            sample: 0
                        status:
                            description: The attachment state.
                            returned: always
                            type: str
                            sample: attached
                description:
                    description: The description.
                    returned: always
                    type: str
                    sample: My interface
                groups:
                    description: One or more security groups.
                    returned: always
                    type: list
                    elements: dict
                    contains:
                        group_id:
                            description: The ID of the security group.
                            returned: always
                            type: str
                            sample: sg-abcdef12
                        group_name:
                            description: The name of the security group.
                            returned: always
                            type: str
                            sample: mygroup
                interface_type:
                    description: The type of network interface.
                    returned: always
                    type: str
                    sample: interface
                ipv6_addresses:
                    description: One or more IPv6 addresses associated with the network interface.
                    returned: always
                    type: list
                    elements: dict
                    contains:
                        ipv6_address:
                            description: The IPv6 address.
                            returned: always
                            type: str
                            sample: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
                mac_address:
                    description: The MAC address.
                    returned: always
                    type: str
                    sample: "00:11:22:33:44:55"
                network_interface_id:
                    description: The ID of the network interface.
                    returned: always
                    type: str
                    sample: eni-01234567
                owner_id:
                    description: The AWS account ID of the owner of the network interface.
                    returned: always
                    type: str
                    sample: 01234567890
                private_dns_name:
                    description: The private DNS hostname name assigned to the instance.
                    type: str
                    returned: always
                    sample: ip-10-1-0-156.ec2.internal
                private_ip_address:
                    description: The IPv4 address of the network interface within the subnet.
                    returned: always
                    type: str
                    sample: 10.0.0.1
                private_ip_addresses:
                    description: The private IPv4 addresses associated with the network interface.
                    returned: always
                    type: list
                    elements: dict
                    contains:
                        association:
                            description: The association information for an Elastic IP address (IPv4) associated with the network interface.
                            type: complex
                            contains:
                                ip_owner_id:
                                    description: The ID of the owner of the Elastic IP address.
                                    returned: always
                                    type: str
                                    sample: amazon
                                public_dns_name:
                                    description: The public DNS name.
                                    returned: always
                                    type: str
                                    sample: ""
                                public_ip:
                                    description: The public IP address or Elastic IP address bound to the network interface.
                                    returned: always
                                    type: str
                                    sample: 1.2.3.4
                        primary:
                            description: Indicates whether this IPv4 address is the primary private IP address of the network interface.
                            returned: always
                            type: bool
                            sample: true
                        private_dns_name:
                            description: The private DNS hostname name assigned to the instance.
                            type: str
                            returned: always
                            sample: ip-10-1-0-156.ec2.internal
                        private_ip_address:
                            description: The private IPv4 address of the network interface.
                            returned: always
                            type: str
                            sample: 10.0.0.1
                source_dest_check:
                    description: Indicates whether source/destination checking is enabled.
                    returned: always
                    type: bool
                    sample: true
                status:
                    description: The status of the network interface.
                    returned: always
                    type: str
                    sample: in-use
                subnet_id:
                    description: The ID of the subnet for the network interface.
                    returned: always
                    type: str
                    sample: subnet-0123456
                vpc_id:
                    description: The ID of the VPC for the network interface.
                    returned: always
                    type: str
                    sample: vpc-0123456
        placement:
            description: The location where the instance launched, if applicable.
            returned: always
            type: complex
            contains:
                availability_zone:
                    description: The Availability Zone of the instance.
                    returned: always
                    type: str
                    sample: ap-southeast-2a
                affinity:
                    description: The affinity setting for the instance on the Dedicated Host.
                    returned: When a placement group is specified.
                    type: str
                group_id:
                    description: The ID of the placement group the instance is in (for cluster compute instances).
                    type: str
                    sample: "pg-01234566"
                group_name:
                    description: The name of the placement group the instance is in (for cluster compute instances).
                    returned: always
                    type: str
                    sample: "my-placement-group"
                host_id:
                    description: The ID of the Dedicated Host on which the instance resides.
                    type: str
                host_resource_group_arn:
                    description:  The ARN of the host resource group in which the instance is in.
                    type: str
                    sample: "arn:aws:resource-groups:us-east-1:123456789012:group/MyResourceGroup"
                partition_number:
                    description: The number of the partition the instance is in.
                    type: int
                    sample: 1
                tenancy:
                    description: Type of tenancy to allow an instance to use. Default is shared tenancy. Dedicated tenancy will incur additional charges.
                    returned: always
                    type: str
                    sample: default
        additional_info:
            description: Reserved for Amazon's internal use.
            returned: always
            type: str
            version_added: 7.1.0
            sample:
        platform_details:
            description: The platform details value for the instance.
            returned: always
            type: str
            sample: Linux/UNIX
        private_dns_name:
            description: The private DNS name.
            returned: always
            type: str
            sample: ip-10-0-0-1.ap-southeast-2.compute.internal
        private_dns_name_options:
            description: The options for the instance hostname.
            type: dict
            contains:
                enable_resource_name_dns_a_record:
                    description: Indicates whether to respond to DNS queries for instance hostnames with DNS A records.
                    type: bool
                    sample: false
                enable_resource_name_dns_aaaa_record:
                    description: Indicates whether to respond to DNS queries for instance hostnames with DNS AAAA records.
                    type: bool
                    sample: false
                hostname_type:
                    description: The type of hostname to assign to an instance.
                    type: str
                    sample: ip-name
        private_ip_address:
            description: The IPv4 address of the network interface within the subnet.
            returned: always
            type: str
            sample: 10.0.0.1
        product_codes:
            description: One or more product codes.
            returned: always
            type: list
            elements: dict
            contains:
                product_code_id:
                    description: The product code.
                    returned: always
                    type: str
                    sample: aw0evgkw8ef3n2498gndfgasdfsd5cce
                product_code_type:
                    description: The type of product code.
                    returned: always
                    type: str
                    sample: marketplace
        public_dns_name:
            description: The public DNS name assigned to the instance.
            returned: always
            type: str
            sample:
        public_ip_address:
            description: The public IPv4 address assigned to the instance
            returned: always
            type: str
            sample: 52.0.0.1
        root_device_name:
            description: The device name of the root device
            returned: always
            type: str
            sample: /dev/sda1
        root_device_type:
            description: The type of root device used by the AMI.
            returned: always
            type: str
            sample: ebs
        security_groups:
            description: One or more security groups for the instance.
            returned: always
            type: list
            elements: dict
            contains:
                group_id:
                    description: The ID of the security group.
                    returned: always
                    type: str
                    sample: sg-0123456
                group_name:
                    description: The name of the security group.
                    returned: always
                    type: str
                    sample: my-security-group
        source_dest_check:
            description: Indicates whether source/destination checking is enabled.
            returned: always
            type: bool
            sample: true
        state:
            description: The current state of the instance.
            returned: always
            type: complex
            contains:
                code:
                    description: The low byte represents the state.
                    returned: always
                    type: int
                    sample: 16
                name:
                    description: The name of the state.
                    returned: always
                    type: str
                    sample: running
        state_transition_reason:
            description: The reason for the most recent state transition.
            returned: always
            type: str
            sample:
        subnet_id:
            description: The ID of the subnet in which the instance is running.
            returned: always
            type: str
            sample: subnet-00abcdef
        tags:
            description: Any tags assigned to the instance.
            returned: always
            type: dict
            sample:
        virtualization_type:
            description: The type of virtualization of the AMI.
            returned: always
            type: str
            sample: hvm
        vpc_id:
            description: The ID of the VPC the instance is in.
            returned: always
            type: dict
            sample: vpc-0011223344
    N)
namedtuple)Any)Dict)List)Optional)Set)Tuple)Union)	to_native)camel_dict_to_snake_dict)snake_dict_to_camel_dict)string_types)AnsibleEC2Error)associate_iam_instance_profile)attach_network_interface)*describe_iam_instance_profile_associations)describe_instance_attribute)describe_instance_status)describe_instances)describe_subnets)describe_vpcs)determine_iam_arn_from_name)ensure_ec2_tags)%get_ec2_security_group_ids_from_names)modify_instance_attribute) modify_instance_metadata_options)(replace_iam_instance_profile_association)run_instances)start_instances)stop_instances)terminate_instances)AnsibleAWSError)is_ansible_aws_error_code)is_ansible_aws_error_message)AnsibleAWSModule)AWSRetry)boto3_tag_list_to_ansible_dict)boto3_tag_specifications)tower_callback_script)!ansible_dict_to_boto3_filter_listparamsreturnc                 ~   | j                  d      xs g }|D ]  }d|v sdD ]!  }||d   v st        |d   |         |d   |<   # d|d   v s6|d   d   dk(  sB|d   j                  d      sd|d   d<   d|d   v rt        |d   d         |d   d<   }d	|d   d<    |D cg c]  }t        |d
       c}S c c}w )Nvolumesebs)volume_sizeiopsvolume_typegp3r1   i  
throughput}   T)capitalize_first)getintr   )r+   r.   volume	int_valuevs        k/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/ec2_instance.pybuild_volume_specr=   ]  s    jj#)rG 6F?4 M	u-/26%=3K/LF5M),M u-&-2NRW2We}((0,0F5M&)6%=025fUmL6Q2RF5M,/25F5M,/6 IPP1$Q>PPPs   #B:moduleinstancedesired_profile_namec           	         |j                  d      }d }|r|r||j                  d      |j                  d      fv ry|j                  d      }t        ||      }|j                  d      |k(  ry	 t        | d|d   gdg	      }	 |xs |j                  d      }t        | d   d   dt        ||      i       y|s6|r4	 |xs |j                  d      }t        | dt        ||      i|d          yy# t        $ r}|j                  |d
       Y d }~d }~ww xY w# t        $ r}|j                  |d       Y d }~d }~ww xY w# t        $ r}|j                  |d       Y d }~yd }~ww xY w)NIamInstanceProfileNameArnFiaminstance-id
InstanceId)rC   ValuesFiltersz+Could not find instance profile associationr   AssociationId)association_idiam_instance_profileTz$Could not associate instance profile)rM   instance_idz(Could not associate new instance profile)r7   clientr   r   r   fail_json_awsr   r   )	rO   r>   r?   r@   instance_profile_setting
iam_clientdesired_arnassociationes	            r<   add_or_update_instance_profilerV   o  s     (||,@AJ$8$<$@$@$HJbJfJfglJm#nnu-J5jBVWK'++E2kA	SD-H\DZC[!\ ]K		L#;v}}U';J4*1~o>&+-HUi-j%k
  $(<		P#;v}}U';J*&+-HUi-j%k$\2
  7  	S  $QRR	S  	L  $JKK	L  	P  $NOO	PsH   0C5 5D 2E 5	D>DD	E&D==E	E*E%%E*c                     | j                   j                  d      xs g }| j                   j                  d      xs g }t        ||z         dkD  r&t        d |D              r| j	                  d       yyy)z|
    Validate Network interface public IP configuration
        Parameters:
            module: The ansible AWS module.
    network_interfacesnetwork_interfaces_ids   c              3   b   K   | ]'  }|j                  d       |j                  d        ) yw)assign_public_ipNr7   .0is     r<   	<genexpr>z,validate_assign_public_ip.<locals>.<genexpr>  s0      D&'GYAZAf !Ds   //zUThe option 'assign_public_ip' cannot be set to true with multiple network interfaces.msgN)r+   r7   lenany	fail_json)r>   rX   rY   s      r<   validate_assign_public_iprg     s      **+?@FB#]]../GHNB
 667!; D+=D A 	tuA;    nb_instancesc           	         t        |        | j                  j                  d      }|r|D ]V  }t        |j                  d      xs g D cg c]  }|j                  d      s| c}      dkD  sE| j	                  d       X |dkD  r3dD ]-  t        fd|D              s| j	                  d	 d
       / yyyc c}w )a  
    This function is used to validate network specifications with the following constraints
        - 'assign_public_ip' cannot set to True when multiple network interfaces are specified
        - 'private_ip_addresses' only one IP can be set as primary
        - 'private_ip_addresses' or 'private_ip_address' can't be specified if launching more than one instance
        Parameters:
            module: The ansible AWS module.
            nb_instances: number of instance to create.
    rX   private_ip_addressesprimaryrZ   zUOnly one primary private IP address can be specified when creating network interface.rb   )private_ip_addressrk   ipv6_addressesc              3   F   K   | ]  }|j                        sd   yw)TNr]   )r_   intyopts     r<   ra   z*validate_network_params.<locals>.<genexpr>  s     LdhhsmtLs   !!zThe option 'z<' cannot be specified when launching more than one instance.N)rg   r+   r7   rd   rf   re   )r>   ri   rX   rp   r`   rq   s        @r<   validate_network_paramsrr     s     f%**+?@& 	Dtxx(>?E2Z!yIYAZ[^__  o ! 	 !W L(:LL$$*3%/kl %    [s   B?
"B?
	interface	subnet_idgroupsc                     d|j                  dd      i}|j                  d      r|d   |d<   |j                  d      r	|d   |d<   n|r||d<   nt        | |      |d<   |j                  d      r|d   D cg c]  }d	|i c}|d
<   |j                  d      r|d   |d<   |j                  d      r}g |d<   |d   D ]p  }d|i}t        |t              rFd|j                  d      i}|j                  d      "|j	                  d|j                  d      i       |d   j                  |       r |j                  dd      |d<   |j                  d      xs |}	|	rt        | ||	|d         |d<   |j                  d      |d   |d<   |S c c}w )a  
    Converts Ansible network interface specifications into AWS network interface spec
        Parameters:
            interface: Ansible network interface specification
            subnet_id: Subnet Id
            security_groups: Optional list of security groups.
            security_group: Optional security group.
        Returns:
            AWS network interface specification.
    DeviceIndexdevice_indexr   r\   AssociatePublicIpAddressrt   SubnetIdrn   Ipv6AddressIpv6Addressesrm   PrivateIpAddressrk   PrivateIpAddressesrl   Primarydelete_on_terminationTDeleteOnTerminationru   ru   rt   GroupsdescriptionDescription)r7   describe_default_subnet
isinstancedictupdateappenddiscover_security_groups)
rO   r>   rs   rt   ru   specaaddrdinterface_groupss
             r<   "ansible_to_boto3_eni_specificationr     s   $ 	y}}^Q7D }}'(+45G+H'(}}[!$[1Z	$Z266BZ}}%&=FGW=X Y-!3 Y_}})*#,-A#B }}+,%'!"45 	1D#T*A$%'2F)GH88I&2HHi))<=>%&--a0	1 #,--0G"ND	 }}X.8&1&&IYeijteuvX}}]#/'6]K- !Zs   7Fc                 l   g }|j                   }|j                  d      }|r0|D cg c]%  }|j                  d      |j                  d      d' }}|j                  d      }|j                  d      xs |j                  d      }|j                  d      }|j                  d	      }	|r|j                  d
      r|	rD|r|j                  d
      s|g}	|j                  |	D 
cg c]  }
t        | ||
||       c}
       |S |s=|s;|j                   j                  d      s |j	                  t        | |i ||             |S |ra|j                  t        |j                  d
g             D 
cg c]+  \  }}
|t        |
t              r|
n|
j                  d      d- c}
}       |S c c}w c c}
w c c}
}w )z
    Returns network interface specifications for instance to be created.
        Parameters:
            module: The ansible AWS module.
        Returns:
            network specs (list): List of network interfaces specifications
    rY   idrx   )NetworkInterfaceIdrw   networksecurity_groupssecurity_groupvpc_subnet_idrX   
interfaceslaunch_template)rw   r   )r+   r7   extendr   r   	enumerater   r   )rO   r>   r   r+   rY   enir   ru   r   rX   rp   idxs               r<   build_network_specr     s    J]]F#ZZ(@A .
 $'774=AXY

 

 jj#GZZ)*Jfjj9I.JFJJ/M$89L16H7;;|4") / 3664X^_	
$  3FMM<M<MN_<` 	<VVRQ^`fgh  
 "+7;;|R+H!IC !$:dT`Ca4gkgogoptguv	
 ?
s   *F&#F+,0F0
c                 J   | j                   j                  d      xs i j                  d      }t        |        | j                   j                  d      }|r|d   j                  d      }|y |j                  d      }|r|r|r|s| j                  d| d|d    d	       y y y )
Nr   r\   rX   r   PublicDnsNamez)Unable to modify public ip assignment to  for instance rG   zN. Whether or not to assign a public IP is determined during instance creation.)r+   r7   rg   warn)r>   r?   r\   rX   public_dns_names        r<   $warn_if_public_ip_assignment_changedr   6  s    )))4:??@RSf%**+?@ .a0445GH ll?3O 06F78H7IX`amXnWo p\ \	
 P_6Frh   c                 J   | j                   j                  d      }|y |d   j                  d      }|j                  d      }|d   j                  d      }|j                  d      }||k7  r| j                  d| d| d	       ||k7  r| j                  d
| d| d       y y )Ncpu_options
CpuOptions	CoreCount
core_countThreadsPerCorethreads_per_corez!Unable to modify core_count from z to zD. Assigning a number of core is determinted during instance creationz'Unable to modify threads_per_core from zP. Assigning a number of threads per core is determined during instance creation.)r+   r7   r   )r>   r?   r   core_count_currr   threads_per_core_currr   s          r<   warn_if_cpu_options_changedr   L  s    --##M2K |,00=O.J$\2667GH"'9:*$//@ZL Q4 4	

  0056K5LDQaPb cK K	
 1rh   parent_vpc_idc                     |	 t        | |g      }d   d	   }t        || |
      S # t        d      $ r |j                  d| d       Y ;t        $ r!}|j	                  |d| d       Y d }~_d }~ww xY w)N)	SubnetIdszInvalidGroup.NotFoundzCould not find subnet z] to associate security groups. Please check the vpc_subnet_id and security_groups parameters.z!Error while searching for subnet z parent VPC.rb   r   VpcId)vpc_id)r   r#   rf   r"   rP   r   )rO   r>   ru   r   rt   subrU   s          r<   r   r   d  s     	e"6i[AC Aw0VV ))@A 	( 4/ /  	e  *KI;Vb(c dd	es   ( #A6A6A11A6c           	         | j                  d      dt        | j                  d            iS | j                  d      rt        | j                  d      j                  d      | j                  d      j                  d      | j                  d      j                  d      | j                  d      j                  d      | j                  d      j                  d      	      }d|iS i S )
N	user_dataUserDataaap_callbacktower_addressjob_template_idhost_config_keywindowsset_password)r   r   r   r   passwd)r7   r   r)   )r+   userdatas     r<   build_userdatar   z  s    zz+*Ifjj&=>??zz.!( **^488I"JJ~6::;LM"JJ~6::;LMJJ~.229=::n-11.A
 H%%Irh   c           	      4   i }| j                   }|j                  d      r	|d   |d<   nh|j                  d      rW|j                  d      }|j                  d      |d<   |j                  d      r|d   |d<   |j                  d      r|d   |d<   |j                  d      s#|j                  d	      s| j                  d
       |j                  d      |j                  d      |d<   |j                  t	        |             |j                  d	      2i |d<   |j                  d	      j                  d      s2|j                  d	      j                  d      s| j                  d       |j                  d	      j                  d      &|j                  d	      j                  d      |d   d<   |j                  d	      j                  d      &|j                  d	      j                  d      |d   d<   |j                  d	      j                  d      /t        |j                  d	      j                  d            |d   d<   |j                  dd      rddi|d<   |j                  d      d|j                  d      i|d<   |j                  d      d|j                  d      i|d<   |j                  d       rQd|v r!t        |j                  d             |d   d!<   n,|j                  dd!t        |j                  d             i       |j                  d"      i |d<   |j                  d"      j                  d#      &|j                  d"      j                  d#      |d   d$<   |j                  d"      j                  d%      &|j                  d"      j                  d%      |d   d&<   |j                  d"      j                  d'      &|j                  d"      j                  d'      |d   d!<   |j                  d"      j                  d(      &|j                  d"      j                  d(      |d   d)<   |j                  d"      j                  d*      &|j                  d"      j                  d*      |d   d+<   |j                  d"      j                  d,      &|j                  d"      j                  d,      |d   d-<   |j                  d"      j                  d      &|j                  d"      j                  d      |d   d<   |j                  d.      |j                  d.      |d/<   |j                  d0      r|j                  d0      |d1<   |j                  d2      |j                  d2      |d3<   |j                  d4      rZ|j                  d5      rI|d5   D ]A  }|j                  d6      r|d6   j                  d7      rd8di|d9<   0| j                  d:       C |j                  d;      Qi |d<<   |j                  d;      j                  d=      |d<   d><   |j                  d;      j                  d?      |d<   d@<   |j                  dA      ri |dB<   |j                  dA      j                  dC      |dB   dD<   |j                  dA      j                  dE      |dB   dF<   |j                  dA      j                  dG      |dB   dH<   |j                  dA      j                  dI      |dB   dJ<   |j                  dA      j                  dK      |dB   dL<   |j                  dM      r|j                  dM      |dN<   |j                  dO      r@g |dP<   |j                  dO      D ]'  }|dP   j                  dQ|j                  dR      i       ) |S )SNimage_idImageIdimager   ramdisk	RamdiskIdkernelKernelIdr   zcYou must include an image_id or image.id parameter to create an instance, or use a launch_template.rb   key_nameKeyNameLaunchTemplatenamezyCould not create instance with launch template. Either launch_template.name or launch_template.id parameters are requiredLaunchTemplateIdLaunchTemplateNameversionVersiondetailed_monitoringFEnabledT
Monitoringcpu_credit_specification
CpuCreditsCreditSpecificationtenancyTenancy	Placementplacement_group	GroupName	placementavailability_zoneAvailabilityZoneaffinityAffinity
group_namehost_idHostIdhost_resource_group_arnHostResourceGroupArnpartition_numberPartitionNumberebs_optimizedEbsOptimized$instance_initiated_shutdown_behavior!InstanceInitiatedShutdownBehaviortermination_protectionDisableApiTerminationhibernation_optionsr.   r/   	encrypted
ConfiguredHibernationOptionszHibernation prerequisites not satisfied. Refer to https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.htmlr   r   r   r   r   r   metadata_optionsMetadataOptionshttp_endpointHttpEndpointhttp_tokens
HttpTokenshttp_put_response_hop_limitHttpPutResponseHopLimithttp_protocol_ipv6HttpProtocolIpv6instance_metadata_tagsInstanceMetadataTagsadditional_infoAdditionalInfolicense_specificationsLicenseSpecificationsLicenseConfigurationArnlicense_configuration_arn)	r+   r7   rf   r   r   r   str
setdefaultr   )r>   r   r+   r   vollicense_configurations         r<   build_top_level_optionsr	    sH   D]]Fzz* ,Y	G	

7#))D/Y99Y %i 0D99X$XD88Ivzz2C'Du 	 	
 zz*) **Z0YKKv&'zz#$0!#zz+,006vzzJ[?\?`?`ag?h/   ::'(,,T2>9?DU9V9Z9Z[_9`D!"#56::'(,,V4@;A::FW;X;\;\]c;dD!"#78::'(,,Y7C09&**EV:W:[:[\e:f0gD!"9-zz'/'.\zz,-9'3VZZ@Z5[&\"#zz)(&

9(=>[zz#$$-0<M1N-ODk*OOK+s6::FW;X7Y)Z[zz+*[::k"&&':;G4:JJ{4K4O4OPc4dD01::k"&&z2>,2JJ{,C,G,G
,SDj)::k"&&|4@-3ZZ-D-H-H-VDk*::k"&&y1=*0**[*A*E*Ei*PDh'::k"&&'@AM8>

;8O8S8STm8nD45::k"&&'9:F39::k3J3N3NOa3bD/0::k"&&y1=+1::k+B+F+Fy+QDi(zz/".%zz/:^zz894:JJ?e4f01zz*+7(.

3K(L$%zz'(VZZ	-B)$ 		Cwwu~#e*.."=.:D-A)*  n ! 			 zz- ,\/5zz-/H/L/LM_/`\+,*0**]*C*G*G*U\;'zz$%"$28**=O2P2T2TUd2e/06

;M0N0R0RS`0a-=CZZHZ=[=_=_)>
 9: 7=jjAS6T6X6XYm6n 23:@**EW:X:\:\]u:v 67zz#$!',=!>zz*+(*$%%+ZZ0H%I 	!()00*,A,E,EFa,bc	 Krh   c                     | j                  d      xs i }| j                  d      | j                  d      |d<   t        |ddg      }|S )Ntagsr   rC   r9   r?   )r7   r(   )r+   r  specss      r<   build_instance_tagsr    sL    ::f#Dzz&%zz&)V$THj+ABELrh   current_countc                    t        t        j                         j                        }|j                  } |j
                  di t        |       |j                  d      xs d}|j                  d      r|j                  d      |z
  }||d<   ||d<   t        ||       t        | |      }|r||d<   t        |      }|r||d<   t        |      }|||d	<   |j                  d
      r7t        t        |j                  d      |j                  d
                  |d<   |j                  d      r|d   |d<   |j                  d      s|j                  d      st        d      |S )N)ClientTokencountrZ   exact_countMinCountMaxCountNetworkInterfacesBlockDeviceMappingsTagSpecificationsrM   rE   )rD   rB   instance_typeInstanceTyper   z^At least one of 'instance_type' and 'launch_template' must be passed when launching instances. )r   uuiduuid4hexr+   r   r	  r7   rr   r   r=   r  r   rO   r   )	rO   r>   r  r   r+   ri   network_specsvolume_specstag_specs	            r<   build_run_instance_specr!    sd   JJL$$D ]]FDKK2)&12::g&+!Lzz- zz-0=@#D#D FL1&vv6M$1 !$V,L&2"#"6*H$, ! zz()%)+FMM%,@&**McBde&
!" zz/"%o6^JJ'6::6G+Hl
 	
 Krh   idsdesired_module_state
force_waitc                    |j                   j                  dd      s|sy |j                  ry ddddddddd}||vr|j                  d	| d
       ||   }| j	                  |      }	 |j                  |d|j                   j                  dd      dz  d       y # t        j                  j                  $ r>}dj                  |      }	|j                  t        |       d|	 d|        Y d }~y d }~wt        j                  j                  $ r>}dj                  |      }	|j                  d|	 d| dt        |              Y d }~y d }~ww xY w)NwaitTinstance_existsinstance_status_okinstance_runninginstance_stoppedinstance_terminatedpresentstartedrunningstopped	restartedrebooted
terminatedabsentzCannot wait for state z, invalid staterb      wait_timeoutX  )DelayMaxAttempts)InstanceIdsWaiterConfigz, z. Error waiting for instances z to reach state z
Instances z took too long to reach state z. )r+   r7   
check_moderf   
get_waiterr&  botocore
exceptionsWaiterConfigErrorjoinr   WaiterErrorr   )
rO   r>   r"  r#  r$  state_to_boto3_waiterboto3_waiter_typewaiterrU   instance_idss
             r<   await_instancesrG  *  s~    ==VT*: %'%%)&+'	 #8856J5K?[\-.BC01Fr%}}00EK 	 	
 00 
yy~Q<. >|nL\]n\op 	 	
 	
 ** ryy~j.LM^L__abklmbnaopqqrs$   )3B E:4C33 E4EEskipc           	         |g }g }|d   }t        dg d      }d } |ddd|       |d	d
d|       |ddd|      g}|D ]  }	|j                  j                  |	j                        )|	j                  |v r8	 t        | ||	j                        }

|	j                     d   |j                  j                  |	j                        k7  st        |d         }|	j                  |j                  j                  |	j                              ||	j                  <   |j                  |        |j                  j                  d      }|sI|j                  j                  d      r.|j                  d   j                  d      s|j                  d   g}|s7|j                  j                  d      s|j                  j                  d      rt        |xs g       dkD  st        |d         dkD  r|j                  d       nS	 t        | |d      }
d}d}|ra|d   j                  d       }|d   j                  d!      }|s||j                  j                  d"      ra|j                  j                  d"      }nE|j                  j                  d"      r|j                  j                  d"      }nt        | |d#$      }|xs8 |j                  j                  d      xs |j                  j                  d      }|rZt        | |||%      }
d&   D cg c]  }|d'   	 }}t!        |      t!        |      k7  r|j                  t        ||d   (             |j                  j                  d)      }|;|j                  j                  d      xs i j                  d)      }|t#        |      }|)|d*   |k7  r!|j                  t        |d   d|i+             |S # t        $ r.}|j                  |d|	j                   d|        Y d}~{d}~ww xY w# t        $ r!}|j                  |d|        Y d}~(d}~ww xY wc c}w ),z!boto3 instance obj, module paramsNrG   ParamMapper)	param_keyinstance_keyattribute_name	add_valuec                 
    d| iS )NValuer  )r;   s    r<   value_wrapperz/diff_instance_and_params.<locals>.value_wrapperc  s    |rh   r   r   ebsOptimizedr   r   disableApiTerminationr  r  instanceType)rN   	attributezCould not describe attribute r   rb   rP  )rG   rX   r   r   r   r   rZ   r  zQSkipping group modification because instance contains mutiple network interfaces.groupSetz3Could not describe attribute groupSet for instance r   rt   ru   r   F)use_availability_zoner   r   GroupId)r   rG   source_dest_checkSourceDestCheck)rG   rZ  )r   r+   r7   rK  rL  r   rM  r   rP   r   rN  r   rd   r   r   r   setbool)rO   r>   r?   rH  changes_to_applyid_rJ  rQ  param_mappingsmappingvaluerU   	argumentsrX   rt   ru   ginstance_groupsrY  s                      r<   diff_instance_and_paramsre  V  s=   
 |
<
 C],hiK 	O^^]S,.EG^`mnO^^]SN " /==W../74'	u/CSZSiSijE %%&w/6==3D3DWEVEV3WW#L1I /6.?.?@Q@QRYRcRc@d.eIg**+##I.!/$  **+?@&--"3"3I">v}}U^G_GcGcdpGq$mmI67V]]../@AV]]EVEVWgEh!'R(1,H=P4Q0RUV0VKKkli3FWab
 IF!.q155kB	+A.228< V]]%6%6%G & 1 1/ BI""?3"MM--o>	3FFZ_`	 jv}}001BCjv}}GXGXYiGjF1&&[de9>x"IA1Y<"I"I'3v;6$++D8T`Ka,bc))*=> #]]..y9?RDDEXY( $%6 7$2C)DHY)Y#L1!(*; <	
 s  	u  *GH^H^G__mnqmr(s tt	u& # i$$Q.abeaf,g$hhi( #Js6   8OP Q 	P"#PP	P=P88P=c                    |j                   j                  d      }|yt        |j                  d            }t        |      t        |      z  D ci c]  }||   ||   k7  r|||    }}|sy|j                  d      xs |j                  d      |j                  d      xs |j                  d      |j                  d      xs |j                  d      |j                  d      xs |j                  d      |j                  d      xs |j                  d      d	}d
}|j                  s	 t        | |d   fi | |S |S c c}w # t        $ r$}	|j                  |	d|d           Y d }	~	|S d }	~	ww xY w)Nr   Fr   r   r   r   r   r   )r   r   r   r   r   TrG   z<Failed to update instance metadata options for instance ID: rb   )r+   r7   r   r[  r<  r   r   rP   )
rO   r>   r?   metadata_options_to_applyexisting_metadata_optionskeyr]  request_argschangedrU   s
             r<    change_instance_metadata_optionsrl    s    & 1 12D E ( 8FW9X Y 01C8Q4RR$S)-Fs-KK 	&s++   '**=9i=V=Z=Z[h=i#3#7#78U#V $H$(()FG(,,_=oAZA^A^_nAo,001EF ?$(()=> 0 4 45M N !C$(()AB	L G	,VXl5K\|\
 N7N90  	  UV^_kVlUmn !   N		s   E0E	 		E6E11E6c           
         |j                   j                  d      xs2 |j                   j                  d      xs i j                  d      xs g D cg c]  }|j                  d       }}|syt        d |      }|d   D cg c]  }|d   	 }}t        |      t        |      z
  }|j                  set        |      D ]W  \  }	}|}
|	}t        |t              r&|j                  d      }
|j                  d	      xs |	}|
|v sE	 t        | ||d
   |
       Y t        |      S c c}w c c}w # t        $ r&}|j                  |d|
 d|d
           Y d}~d}~ww xY w)a"  
    Attach Network interfaces to the instance
        Parameters:
            client: AWS API client.
            module: The ansible AWS module.
            instance: A dictionnary describing the instance.
        Returns:
            A boolean set to True if changes have been done.
    rY   r   r   r   Fc                 H    t        | t              r| j                  d      S | S )Nr   )r   r   r7   )xs    r<   <lambda>z,change_network_attachments.<locals>.<lambda>  s    :a+>AEE$K A rh   r  r   rx   rG   )rw   rG   r   zCould not attach interface z to instance rb   N)r+   r7   mapr[  r<  r   r   r   r   r   rP   r\  )rO   r>   r?   r   new_enisnew_idsrp   old_ids	to_attachr   eni_idrx   rU   s                r<   change_network_attachmentsrw    s    ==$$%=> MMi(.B33LA	 	H  ExPG6>?R6STdt()TGTGs7|+I!(+ 	HCFL#t$"ww~6=#"
,$0#+L#9+1		$ 	?? U" ' ((!<VHMRZ[gRhQij )  s$   D$D)D..	E7EEfiltersc                 z   t               }|rt        |      }nz||j                  d       net        |j                               D ]4  }|j	                  d      s||   ||j                  dd      <   -||   ||<   6 t        t        |            }g }t        | fi D ]  }|j                  |d           |S )	N)r:  z+No filters provided when they were requiredrb   ztag:_-rI   	Instances)	r   rf   listkeys
startswithreplacer*   r   r   )	rO   r>   r"  rx  sanitized_filtersr+   ri  	instancesreservations	            r<   find_instancesr    s     
#&	JK' 	6C>>&);B3<!#++c3"78)0!#&		6
 ?@QRSI)&;F; 3[123rh   c                     	 t        | t        ddi            }r|d   S d S # t        $ r}|j                  |d       Y d }~*d }~ww xY w)N	isDefaulttruerI   zCould not describe default VPCrb   r   )r   r*   r   rP   )rO   r>   vpcsrU   s       r<   get_default_vpcr    s_    FV-NP[]cOd-ef 47$$  FQ$DEEFs   # 	A	AA	vpcc           	         	 t        | t        |d   ddd            }d }r0|t	        d |D              }||v r||   S t        |d	 
      }|d   }|S # t        $ r#}|j                  |d|d           Y d }~]d }~ww xY w)Nr   	availabler  )zvpc-idstatezdefault-for-azrI   z+Could not describe default subnets for VPC rb   c              3   *   K   | ]  }|d    |f  yw)r   Nr  )r_   subnets     r<   ra   z%get_default_subnet.<locals>.<genexpr>7  s     Yvv&896BYs   c                     | d   S )Nr   r  )ss    r<   rp  z$get_default_subnet.<locals>.<lambda>=  s    a0B.C rh   ri  r   )r   r*   r   rP   r   sorted)	rO   r>   r  r   subnetsrU   result
subs_by_azby_azs	            r<   get_default_subnetr  $  s    b"5!'l(&,	
 F(YQXYYJ J.!"344 w$CDqM  bQ&QRUV]R^Q_$`aabs   A 	BA<<Bc                     t               }d}|dv rt        | |||      \  }}}}	|t        t        |            z  }|r |j	                  d|	 t        |      |       t        dt        |      g t        |      g ||D 
cg c]  }
t        |
       c}
      }|S |dv rt        | ||d	      \  }}}}	|r |j	                  d
|	 t        |      |       |t        t        |            z  }t        | |||      \  }}}}	|t        t        |            z  }|r |j	                  d|	 t        |      |       t        dt        |      |g |D 
cg c]  }
t        |
       c}
      }|S |dv rt        | |||      \  }}}}	|t        t        |            z  }|r |j	                  d
|	 t        |      |       t        dt        |      |g |D 
cg c]  }
t        |
       c}
      }|S |dv rxt        | |||      \  }}}}	|r |j	                  d|	 t        |      |       t        dt        |      t        t        |            g |D 
cg c]  }
t        |
       c}
      }|S c c}
w c c}
w c c}
w c c}
w )zB
    Sets return keys depending on the desired instance state
    F)r/  r.  )rx  r#  zUnable to start instances: )rc   reboot_successreboot_failedzInstances started)rc   start_successstart_failedr  r  rk  r  )r1  r2  r0  zUnable to stop instances: )rc   stop_successstop_failedzUnable to restart instances: zInstances restarted)rc   r  rk  r  r  )r0  zInstances stopped)rc   r  rk  r  r  r4  r3  zUnable to terminate instances: )rc   terminate_successterminate_failedzInstances terminated)rc   r  rk  r  r  )r   change_instance_stater\  rd   rf   r}  pretty_instance)rO   r>   r#  rx  resultsrk  _changedfailedr  failure_reasonr`   r3  r  s                r<   ensure_instance_stater  B  s    fGG556KFGBV7
3&)^ 	4H&&1.1AB#H~$   #x.>3<=aq)=	
~ Nk 
!:	: 7L!*	7
3&)^ 00@A!(^"   	4H&&6K!5	7
3&)^ 	4H&&3N3CD#H~$   %>3<=aq)=
f NY 
	-6K!5	7
3&)^ 	4H&&00@A!(^"   #h3<=aq)=
8 N+ 
!9	9BW!5	C
?
$i 5n5EF"&z"2!1  
 &":.Z)3<=aq)=
 No >R >. >* >s   ?I,I1I6I;c           	      R   ddddddddd}||   t               }t        | ||      }t        fd|D              }t               }d}	|D ]  }
	 dk(  r|
d   d	   d
k(  rt        | ||
d   gdd       n|
d   d	   dk(  rt        | ||
d   gdd       |j                  r|j	                  |
d          jt        | |
d   g      }|j                  |D cg c]  }|d   	 c}       dk(  r|
d   d	   dk(  rt        | ||
d   gdd       n|
d   d	   dv r|j	                  |
d          |j                  r|j	                  |
d          t        | |
d   g      }|j                  |D cg c]  }|d   	 c}       dk(  r|
d   d	   dv r|j	                  |
d          [|
d   d	   d
k(  rt        | ||
d   gdd       |j                  r|j	                  |
d          t        | |
d   g      }|j                  |D cg c]  }|d   	 c}        |r$t        | |t        |      t        |      z   |       t        ||z
        }|rt        | |t        d |D                    }||||	fS c c}w c c}w c c}w # t        $ r}t        |      }	Y d }~Ud }~ww xY w)Nr/  r0  r3  r,  rx  c              3   @   K   | ]  }|d    d   k7  s|d     yw)StaterC   rG   Nr  )r_   r`   desired_ec2_states     r<   ra   z(change_instance_state.<locals>.<genexpr>  s&     b!G*V:LPa:aAlObs   
 r  rC   stoppingrG   Tr#  r$  pending)rF  )r  r0  )r  r/  )r"  r#  c              3   &   K   | ]	  }|d      yw)rG   Nr  r^   s     r<   ra   z(change_instance_state.<locals>.<genexpr>	  s     ;_PQAlO;_s   r"  )r[  r  rG  r<  addr!   r   r    r   r   r  r}  )rO   r>   rx  r#  ec2_instance_statesrk  r  	to_change	unchangedr  instrespr`   rU   change_failedr  s                  @r<   r  r    s   
 "	 ,,@AeGvvw?IbYbbIIN 7$6	$ L0 =(J6#l);(<S\im ']6*i7#l);(<S\im $$KK\ 23 +6lAS@TU>A,>? I- =(I5#l);(<S\im ']6*.EEMM$|"45$$KK\ 23%fD<N;OP>A,>? I-=(,BBMM$|"45=(J6#l);(<S\im $$KK\ 23&vT,=O<PQ>A,>?k7$r DMDO,KbvwW,-M"66t;_U^;_7_`	M9n<<W  ?"  ?  ? 	$ VN	$sb   A$J6 JI8
"A
J- J J/I=
;*J'?J( JJ
J8J	J&J!!J&c                 ^    t        | dg      }t        | j                  di             |d<   |S )NTags)ignore_listr  )r   r'   r7   )r`   r?   s     r<   r  r  	  s/    'x@H5aeeFB6GHHVOrh   r  rN   changesc                     d|gi}t        | |d|       t        | ||gdd       t        | fd|i| |dk(  rdn|}t        | |||       y )NrF   r0  T)r"  r#  r$  rN   r-  r/  )r  rG  r   )rO   r>   r  rN   r  rx  r#  s          r<   modify_instance_typer  	  sk     	}G &&)W= FFIbfg fI+II ).(:9&&*>Hrh   c           	          |j                   s<|D ]6  }|j                  d      }	 d|v rt        | ||||       nt        | fd|i| 8 y y # t        $ r*}|j                  |dt        |       d       Y d }~id }~ww xY w)NrG   r  rN   zCould not apply change z to existing instance.rb   )r<  popr  r   r   rP   r  )rO   r>   r  r  crN   rU   s          r<   modify_ec2_instance_attributer  /	  s     	fA%%-Kf!Q&(QO-fS+SQRS	f  # f$$Q.Ec!fXMc,d$eefs   "A

	A= A88A=existing_matchesc           
      d   |j                   j                  d      }|j                   j                  d      }|j                   j                  d      }|r|d}i }|j                  d|i       d}t               }	|D ]  }
|t	        | ||
d   ||      z  }|t        | ||
      z  }t        | ||
      }t        | |||       |	j                  |       |t        |      z  }|t        | ||
|j                   j                  d            z  }|t        | ||
      z  } t        | ||D cg c]  }|d   	 c}	      }t        ||D cg c]  }t        |       c}|D cg c]  }|d   	 c}
      }t        | |||      }|dxx   |j!                  dd      z  cc<   i ||}|S c c}w c c}w c c}w )Nr  
purge_tagsr   FrC   rG   )r  r  rM   r  )rk  r  rF  r  rk  )r+   r7   r   r}  r   rl  re  r  r   r\  rV   rw  r  r   r  r  r  )rO   r>   r  r  rx  r  r  r   rk  all_changesr?   r  r`   alteredalter_config_resultstate_resultsr  s                    r<   handle_existingr  <	  s    ==V$D""<0J==V$D
 <JDVTN#G&K$ H?668L3IPTakll3FFHMM*668D%ffeWE7#4= 1&&(FMML]L]^tLuvv-ffhGGH VVK[1\a!L/1\]G/67!?1%7/67!ao7	 *&&%IM	"m&7&7	5&II"55!45FM 2] 87s   %F#F(
F-c           	         |j                   j                  d      }t        |      }||k(  rf|dvrt        | ||||      }nt	        | |||      }|d   r|S t        d|D cg c]  }t        |       c}|D cg c]  }|d   	 c}| d| d      S ||k  rt        | ||||	      }nu||z
  }	t        |d
       }|D 
cg c]  }
|
d   	 }}
|d|	 }t        d||d|       }|j                  s-	 t        | |       t        | ||dd       t        dd|||      }|j                  s$t        | ||      }t        | ||||      }|d   r|}|S c c}w c c}w c c}
w # t        $ r}|j                  |d       Y d }~d }~ww xY w)Nr  r  rk  FrG   z instances already z, nothing to do.)rk  r  rF  rc   )r  r#  r  c                     | d   S )N
LaunchTimer  )r  s    r<   rp  zenforce_count.<locals>.<lambda>	  s    T,EW rh   r  r   Tz?Would have terminated following instances if not in check mode )rk  terminated_idsrF  rc   zUnable to terminate instancesrb   r3  r  z"Successfully terminated instances.)rk  rc   r  rF  r  r  )r+   r7   rd   r  r  r   r  ensure_presentr  r<  r!   r"   rf   rG  r  )rO   r>   r  r#  rx  r  r  r  r`   to_terminatero  all_instance_idsterminate_idsrU   updated_resultss                  r<   enforce_countr  p	  s    --##M2K()M#'??%ff6FH\^efG+FF<PRYZG9N3CDaq)D3CDa!L/D-23G2HHXY	
 	
 {" -!5'
 %{2!"28WX5EFAlOFF(<8()QR_Q`a	
   I#FM: FFMP\imn 8,-*G )&&'J)&&:JL`bij9%%GNk ED& G # I  (G HHIs*   E
7E?E1E 	F'E??Fc                    t        |j                  j                  d      xs i       }|j                  j                  d      }|r||d<   t        | ||      }|j                  r2|r"|D cg c]  }|d   	 }	}t        d|	||d      S t        d|d      S t        | fi |}
|
d	   }|D cg c]  }|d   	 }	}t        | ||	d
d       |D ]4  }	 t        | |d   gd       t        | ||ddg      }t        | |||       6 |r|D cg c]  }|d   	 c}|	z   }|j                  j                  d      s|rt        d|	|      S t        d|	|      S t        | ||	|       t        | ||	      }|r,||z   }t        d|	|D cg c]  }t        |       c}|      S t        d|	|D cg c]  }t        |       c}|      S c c}w c c}w # t        $ r}|j                  |d       Y d }~d }~ww xY wc c}w c c}w c c}w )Nr  r   rC   rG   Tz3Would have launched instances if not in check_mode.)rk  rF  r  r   rc   )rk  r   rc   r|  r-  r  )r:  IncludeAllInstancesz*Failed to fetch status of new EC2 instancerb   r   r   )rH  r&  )rk  changed_idsrF  r   )rk  rF  r   )r#  r  )rk  r  rF  r  r   )rk  rF  r  r   )r   r+   r7   r!  r<  r   rG  r   r   rP   re  r  r  r  )rO   r>   r  r#  r  r  r   instance_specro  rF  instance_responser  r`   insrU   r  r  all_instancess                     r<   r  r  	  sv    !!&)/R0D==V$DV+FFMJM5EFAlOFLF)*"I  "I 
 &f>>!+.I-67AlO7L7 FFLy]ab U	V$ ./$( +663jR`Eab%ff6JGTU  6FFAlOFU==V$(-"	  )" 
 FFLG[\vv<@I(94$)3@Aaq)A
 	
 %3<=aq)=	
 	
G G  8  	V  (T UU	V G0 B >s6   *G+G
GG9G>*H	G6G11G6r  c                     	 t        | fi |S # t        d      $ r$ t        j                  d       t        | fi |cY S w xY w)Nz Invalid IAM Instance Profile ARN
   )run_ec2_instancesr$   timesleep)rO   r  s     r<   r   r   
  sI    : 9=99'(JK : 	

2 9=99	:s    /A A rW  c                     t        | |      }|s|j                  d       |r|j                  j                  d      nd}t	        | |||      d   S )a1  
    Read default subnet associated to the AWS account
        Parameters:
            client: AWS API client
            module: The Ansible AWS module
            use_availability_zone: Whether to use availability zone to find the default subnet.
        Returns:
            The default subnet id.
    z^No default subnet could be found - you must include a VPC subnet ID (vpc_subnet_id parameter).rb   r   Nrz   )r  rf   r+   r7   r  )rO   r>   rW  default_vpcr   s        r<   r   r    
  s_     "&&1Kq 	 	
 CX))*=>]affk;LMjYYrh   c                    i }|j                   j                  d      xs. |j                   j                  d      xs i j                  d      }|j                   j                  d      r!|j                   j                  d      g|d<   |S |r*|D cg c]  }t        |t              r|d   n| c}|d<   |S t	        | |      |d<   |S c c}w )zz
    Create Network filters for the DescribeInstances API
        Returns:
            Dictionnary of network filters
    rY   r   r   r   z	subnet-idr   z&network-interface.network-interface-id)r+   r7   r   r   r   )rO   r>   rx  rY   r   s        r<   build_network_filtersr  3
  s     G#]]../GH V]]M^M^_hMiMomoLtLtM }}) & 1 1/ BC N 
 CY=
<?C.CIC7=
89
 N  7vvFN=
s   Cc                 x   g d}i }|j                   j                  d      }t        |t              r|g|d}|S t        |t              rt        |      r||d}|S |j                   j                  d      s|j                  t        | |             |j                   j                  d      r |j                   j                  d      g|d<   nN|j                   j                  d      r3|j                   j                  d      j                  dd       }|r|g|d<   |j                   j                  d	      r |j                   j                  d	      g|d
<   n]|j                   j                  d      xs i j                  d      r/|j                   j                  di       j                  d      g|d
<   |r||d<   |S )N)r  r/  r  r0  rF  )rF   instance-state-namer   r   ztag:Namer  rC   r   zimage-idr   r   r  )r+   r7   r   r   r}  rd   r   r  )rO   r>   instance_state_namesrx  rF  name_tags         r<   build_filtersr  H
  s   HG==$$^4L,-#/.I]^, N+ 
L$	'C,=".G[\( N% }}  !23NN0@A==V$#)==#4#4V#<"=GJ]]v&}}((044VTBH'/j
#==Z(#)==#4#4Z#@"AGJmm(.B33D9#)==#4#4Wb#A#E#Ed#K"LGJ-AG)*Nrh   c                     t        dvi dt        dg d      dt        dd      d	t        d
d      dt        d      dt        d      dt        dt        t        d      t        d      t        d                  dt        d      dt        d      dt        d      dt        ddgdgt        t        dd      t        dd      t        d      t        d      t        dd                  dt        d      d t        dd!g"      d#t        d      d$t        g d%d&      d't        d      d(t        dd)g"      d*t        d      d+t        dd,g"      d-t        dd      d.t        dd       d/t        d      d0t        d%dt        t        dd1      2      3      d4t        d      d5t        dd6d7g8      d9t        dt        t        dd1      t        dd:d;gd<      =            d>t        dd?d@g8      dAt        d      dBt        dt        t        d      t        d      t        d      t        d      t        d      t        d      t        dg dC8      D            dEt        ddFdGg8      dHt        d      dIt        dd      dJt        d      dKt        g d%d&      dLt        d d      dMt        d d%d&      dNt        dt        t        dOdPgdOQ      t        dd:      t        dRdSgdRQ      t        dPdOgdPQ      t        dPdOgdPQ      T            dUt        d      dVt        d%dt        t        dW      t        ddX      Y      3      dZt        d%dt        t        d      t               t        d%d[      t               t        d%dt        t        dW      t        d      \      3      t               t        dd      t        ddX      t        d%d[      ]	      3      d^t        d      } t        | d$d'gd#d gddgddgddgddKgd>dBgdAdBgdLdZgdLdVgd'dVgd$dVggd_      }t               }|j                  j                  dL      r|j	                  d`dadbc       |j                  dL   j                  dd      rZ|j                  j                  d'      r|j                  def       |j                  j                  d$      r|j                  dgf       |j                  dL   j                  d^      ,|j                  j                  d^      |j                  dh       |j                  j                  dA      r|j	                  didjdbc       |j                  j                  d>      r|j	                  dkdjdbc       |j                  j                  d      }t        j                  g dlm      }|j                  dn|o      }|j                  j                  d.      }|t        ||      }	 g }|rt        |||p      }|dqv r|rt        ||||      }nt        drds      }n|j                  j                  d      rt        |||||t      }n\|rK|j                  j                  d      s0|D ]  }t        ||       t        ||        t!        |||||p      }nt#        ||||u      } |j,                  dvi | y # t$        $ rY}	|	j&                  r'|j)                  |	j&                  |	j*                  f       |j                  |	j*                  f       Y d }	~	pd }	~	ww xY w)wNr  r-  r,  )defaultchoicesr&  Tr\  )r  typer6  r7  r8   r  )r  r  r   r   r  )r   r   r   )r  optionsr   r  r   r   tower_callback)r   F)r   r   r   FF)r  r  )r  no_log)r   r   r   r   r   )r  aliasesrequired_ifr  r   r   rt   )r  r  r   r   r}  )r  r  elementsr   rM   instance_roler   r  resource_tagsr  rx  r   r  )r  required)r  )r  r  r  r   r   standard	unlimited)r  r  r   rZ      )r  r  r  )r   r   r   	dedicatedr  r   r   )r  r  host)r   r   r   r   r   r   r   r   stop	terminater   r   r   rF  r   r.   r   enableddisabled)r  r  optionalr  )r   r   r   r   r   r   rY   )r  r   )r   rx   rX   )r  r  )rm   rl   )	r\   rm   rn   r   rk   rt   r   rx   ru   rY  )argument_specmutually_exclusivesupports_check_modezoThe network parameter has been deprecated, please use network_interfaces and/or network_interfaces_ids instead.z
2026-12-01z
amazon.aws)datecollection_namer   z>Parameter network.interfaces can't be used with security_grouprb   z?Parameter network.interfaces can't be used with security_groupsz^the source_dest_check option has been set therefore network.source_dest_check will be ignored.z[The placement_group parameter has been deprecated, please use placement.group_name instead.z
2025-12-01zPThe tenancy parameter has been deprecated, please use placement.tenancy instead.)IncorrectStateInsuffienctInstanceCapacityzInvalidInstanceID.NotFound)catch_extra_error_codesec2)retry_decoratorr  )r3  r4  zNo matching instances found)rc   rk  )r#  rx  )r  r#  r  )r   r%   r+   r7   	deprecaterf   r   r&   jittered_backoffrO   r  r  r  r  r   r   r  r  r   	exceptionrP   message	exit_json)
r  r>   r  r  r  rO   rx  r  matchrU   s
             r<   mainr  g
  s/    Bq
B
 $V,B #E2B B e$B U#%('
B" 5!#B$ &%B& E"'B( %&	 &%8!uT:". $% 0 $% =
)BV 'WBX }=YBZ E*[B\ RfuE]B^ '_B` "u6GHaBb ucBd v'89eBf VT2gBh &$/iBj &)kBl  $*.ED*I 
mBz 5!{B| "&5:{:S!T}B~ UT:!%51a&4!P
BL %+y)ABMBN %(OBP 5)"&E"2U+%((,%(8!%5!1%1QR
QBh .2uv{F[-\iBj  $0kBl !fe<mBn !f-oBp "6EBqBr T/sBt T@uBv "Iz+BIV,0eQ,G *j)A:V#'Y0GQ[#\'+Z4KU_'`	
wBJ %(KBL  $&!ua8 
MB\  !%6!2#'6#%@ F%)# D$4GQU[aQbc&
 &&*&E!ua8%8
]BB F+CBMJ # 01 /2[)!G$N+$,,-0178 89
 !!F& VF}}#}( 	 	

 ==#''5}}  !12  %e f}}  !23  %f gMM)$(()<=I!!"56BKKp }}*+i( 	 	
 }}#^( 	 	
 MMg&E//!
O ]]5/]BFmm	*G/(-ffgN,,.vvugN5! ]]}-"663CZ_ipqFfmm&7&7&@) ;4VUC+FE:; %VV5EuV]^F#FFEUlqrF Fv  (;;  !)) <QYY''(s   1B=\ 	]#
A]]#__main__)NN)r   )r-  F)N)T)`DOCUMENTATIONEXAMPLESRETURNr  r  collectionsr   typingr   r   r   r   r   r	   r
   r>  ImportErroransible.module_utils._textr   0ansible.module_utils.common.dict_transformationsr   r   ansible.module_utils.sixr   7ansible_collections.amazon.aws.plugins.module_utils.ec2r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r    r!   >ansible_collections.amazon.aws.plugins.module_utils.exceptionsr"   r#   r$   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr%   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr&   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr'   r(   9ansible_collections.amazon.aws.plugins.module_utils.towerr)   Bansible_collections.amazon.aws.plugins.module_utils.transformationr*   r  r=   r\  rV   rg   r8   rr   r   r   r   r   r   r   r	  r  r!  rG  re  rl  rw  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  __name__r  rh   r<   <module>r'     sX  M^JXE	
N   "       	
 1 U U 1 S b \ n _ \ V T Q _ S i ] d l f S R W Z d g X P f ` [ pQd38n Qd38n1E Q$/$/04S#X/VY/	/dv&6 v4 v$4 C D >55 CH~5 }	5
 U49c>*+5 
#s(^5p-'7 -Dc3h<P -`
1A 
TRUWZRZ^ 
`d 
,
(8 
DcN 
W[ 
8 $(#WW #tCy.!W C=	W
 }W 
#YW,h$4 hc3h hVS#X 4S#X3G +,< +S +Y]^acf^fYg +^ qv)r$)r+/9)rLO)rim)r	)rZ ]aY$Y04S#XYFNtTWyFYY	$sCx.Yx$5E $QUVY[^V^Q_ $dh $N)/? )4PSUXPX> )^b )Z lp$+3DI+>PXY]^acf^fYgPh	$sCx..%$4 %$sCx.9Q % NR$+/S>d38n<t$t<?tJRSWX[]`X`SaJbt	#s(^tnU=$U=/7S#X/GU=_bU=
3s8T#Yd38n)= >CDU=pII I 	I
 #tCH~%&I 
I0
f2B 
f3 
fY]^bcfhkck^lYm 
frv 
f11 4S>*1 	1
 d38n%1 
#s(^1hHH 4S>*H 	H
 d38n%H 
#s(^H` $(X
X
 tDcN34X
 	X

 C=X
 
#s(^X
v:4S> :d38n :Z,< ZUY Zeh Z&*: tCcN?S *"2 tCH~ >fR zF u0  		s    N. .N76N7