Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add uptime parameter for ec2_instance_info module in minutes #356

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions plugins/modules/ec2_instance_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
required: false
default: {}
type: dict
uptime:
description:
- minimum running uptime in minutes of instances. For example if uptime is 60, it would return all instances that have run more than 60 minutes.
tremble marked this conversation as resolved.
Show resolved Hide resolved
tremble marked this conversation as resolved.
Show resolved Hide resolved
required: false
type: int

extends_documentation_fragment:
- amazon.aws.aws
Expand Down Expand Up @@ -66,6 +71,15 @@
filters:
instance-state-name: [ "shutting-down", "stopping", "stopped" ]

- name: Gather information about any instance with Name beginning with RHEL and been running at least 60 minutes
tremble marked this conversation as resolved.
Show resolved Hide resolved
community.aws.ec2_instance_info:
region: "{{ ec2_region }}"
uptime: 60
filters:
"tag:Name": "RHEL-*"
instance-state-name: [ "running"]
register: ec2_node_info

'''

RETURN = r'''
Expand Down Expand Up @@ -492,6 +506,8 @@
'''

import traceback
import datetime

tremble marked this conversation as resolved.
Show resolved Hide resolved

try:
import boto3
Expand All @@ -509,18 +525,34 @@
def list_ec2_instances(connection, module):

instance_ids = module.params.get("instance_ids")
uptime = module.params.get('uptime')
filters = ansible_dict_to_boto3_filter_list(module.params.get("filters"))

# Determine oldest launch_time
if uptime is None:
oldest_launch_time = datetime.datetime.utcnow() + datetime.timedelta(days=30 * 365)
else:
oldest_launch_time = datetime.datetime.utcnow() - datetime.timedelta(minutes=int(uptime))

tremble marked this conversation as resolved.
Show resolved Hide resolved
try:
reservations_paginator = connection.get_paginator('describe_instances')
reservations = reservations_paginator.paginate(InstanceIds=instance_ids, Filters=filters).build_full_result()
except ClientError as e:
module.fail_json_aws(e, msg="Failed to list ec2 instances")

# Get instances from reservations
instances = []
for reservation in reservations['Reservations']:
instances = instances + reservation['Instances']



if uptime:
tremble marked this conversation as resolved.
Show resolved Hide resolved
timedelta = int(uptime) if uptime else 0
oldest_launch_time = datetime.datetime.utcnow() - datetime.timedelta(minutes=timedelta)
# Get instances from reservations
for reservation in reservations['Reservations']:
instances += [instance for instance in reservation['Instances'] if instance['LaunchTime'].replace(tzinfo=None) < oldest_launch_time]
else:
for reservation in reservations['Reservations']:
instances = instances + reservation['Instances']

# Turn the boto3 result in to ansible_friendly_snaked_names
snaked_instances = [camel_dict_to_snake_dict(instance) for instance in instances]
Expand All @@ -535,6 +567,7 @@ def list_ec2_instances(connection, module):
def main():

argument_spec = dict(
uptime=dict(required=False, type='int', default=None),
instance_ids=dict(default=[], type='list', elements='str'),
filters=dict(default={}, type='dict')
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
- block:
- name: "create t3.nano instance"
ec2_instance:
name: "{{ resource_prefix }}-test-t3nano-1"
tremble marked this conversation as resolved.
Show resolved Hide resolved
region: "{{ ec2_region }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
instance_type: t3.nano
wait: yes

- name: "check ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-t3nano-1"
instance-state-name: [ "running"]
register: instance_facts

- name: "Confirm existence of instance id."
assert:
that:
- "{{ instance_facts.instances | length }} == 1"

- name: "check using uptime 100 hours - should fail"
tremble marked this conversation as resolved.
Show resolved Hide resolved
ec2_instance_info:
region: "{{ ec2_region }}"
uptime: 6000
filters:
instance-state-name: [ "running"]
"tag:Name": "{{ resource_prefix }}-test-t3nano-1"
register: instance_facts

- name: "Confirm there is no running instance"
assert:
that:
- "{{ instance_facts.instances | length }} == 0"

- name: "check using uptime 1 minute"
tremble marked this conversation as resolved.
Show resolved Hide resolved
ec2_instance_info:
region: "{{ ec2_region }}"
uptime: 1
filters:
instance-state-name: [ "running"]
"tag:Name": "{{ resource_prefix }}-test-t3nano-1"
register: instance_facts

- name: "Confirm there is one running instance"
assert:
that:
- "{{ instance_facts.instances | length }} == 1"

always:
- name: "Terminate instances"
ec2_instance:
state: absent
filters:
"tag:TestId": "{{ ec2_instance_tag_TestId }}"
wait: yes
ignore_errors: yes