<s> import boto3 <EOL> def get_instances ( ) : <EOL> return boto3 . resource ( '<STR_LIT>' ) . instances . all ( ) <EOL> def get_running_instances ( ec2_instances ) : <EOL> return [ instance for instance in ec2_instances <EOL> if instance . state [ '<STR_LIT:Name>' ] == '<STR_LIT>' ] <EOL> def id_targets_for_instances ( instances ) : <EOL> return [ ( inst . id , [ inst . private_ip_address ] ) for inst in instances ] <EOL> def name_tag_targets_for_instances ( ilist ) : <EOL> tag_nodes = { } <EOL> for i in ilist : <EOL> if i . tags is None : <EOL> continue <EOL> for tag in i . tags : <EOL> if tag [ '<STR_LIT>' ] == '<STR_LIT:Name>' : <EOL> name = tag [ '<STR_LIT>' ] <EOL> if name not in tag_nodes : <EOL> tag_nodes [ name ] = [ ] <EOL> tag_nodes [ name ] . append ( i . private_ip_address ) <EOL> return [ ( '<STR_LIT>' . format ( tag ) , ips ) for tag , ips in tag_nodes . items ( ) ] <EOL> def environment_tag_targets_for_instances ( ilist ) : <EOL> """<STR_LIT>""" <EOL> env_nodes = { } <EOL> for i in ilist : <EOL> if i . tags is None : <EOL> continue <EOL> for tag in i . tags : <EOL> if tag [ '<STR_LIT>' ] == '<STR_LIT>' or tag [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> name = tag [ '<STR_LIT>' ] <EOL> if name not in env_nodes : <EOL> env_nodes [ name ] = [ ] <EOL> env_nodes [ name ] . append ( i . private_ip_address ) <EOL> return [ ( '<STR_LIT>' . format ( env ) , ips ) for env , ips in env_nodes . items ( ) ] <EOL> def asg_targets_for_instances ( ilist ) : <EOL> asg_nodes = { } <EOL> for i in ilist : <EOL> if i . tags is None : <EOL> continue <EOL> for tag in i . tags : <EOL> if tag [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> name = tag [ '<STR_LIT>' ] <EOL> if name not in asg_nodes : <EOL> asg_nodes [ name ] = [ ] <EOL> asg_nodes [ name ] . append ( i . private_ip_address ) <EOL> return [ ( '<STR_LIT>' . format ( asg ) , ips ) for asg , ips in asg_nodes . items ( ) ] <EOL> def opsworks_instance_name_targets_for_instances ( ilist ) : <EOL> """<STR_LIT>""" <EOL> oin_nodes = { } <EOL> for i in ilist : <EOL> if i . tags is None : <EOL> continue <EOL> for tag in i . tags : <EOL> if tag [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> name = tag [ '<STR_LIT>' ] <EOL> if name not in oin_nodes : <EOL> oin_nodes [ name ] = [ ] <EOL> oin_nodes [ name ] . append ( i . private_ip_address ) <EOL> return [ ( '<STR_LIT>' . format ( oin ) , ips ) for oin , ips in oin_nodes . items ( ) ] <EOL> def opsworks_instance_stack_targets_for_instances ( ilist ) : <EOL> """<STR_LIT>""" <EOL> ois_nodes = { } <EOL> for i in ilist : <EOL> if i . tags is None : <EOL> continue <EOL> for tag in i . tags : <EOL> if tag [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> name = tag [ '<STR_LIT>' ] <EOL> if name not in ois_nodes : <EOL> ois_nodes [ name ] = [ ] <EOL> ois_nodes [ name ] . append ( i . private_ip_address ) <EOL> return [ ( '<STR_LIT>' . format ( ois ) , ips ) for ois , ips in ois_nodes . items ( ) ] <EOL> def ami_targets_for_instances ( ilist ) : <EOL> ami_nodes = { } <EOL> for i in ilist : <EOL> if i . image_id not in ami_nodes : <EOL> ami_nodes [ i . image_id ] = [ ] <EOL> ami_nodes [ i . image_id ] . append ( i . private_ip_address ) <EOL> return [ ( ami , ips ) for ami , ips in ami_nodes . items ( ) ] <EOL> def targets_for_instances ( _instances ) : <EOL> """<STR_LIT>""" <EOL> by_id = id_targets_for_instances ( _instances ) <EOL> by_image = ami_targets_for_instances ( _instances ) <EOL> by_name_tag = name_tag_targets_for_instances ( _instances ) <EOL> by_environment_tag = environment_tag_targets_for_instances ( _instances ) <EOL> by_asg = asg_targets_for_instances ( _instances ) <EOL> by_oin = opsworks_instance_name_targets_for_instances ( _instances ) <EOL> by_ois = opsworks_instance_stack_targets_for_instances ( _instances ) <EOL> return dict ( <EOL> by_id + <EOL> by_name_tag + <EOL> by_environment_tag + <EOL> by_image + <EOL> by_asg + <EOL> by_ois + <EOL> by_oin <EOL> ) <EOL> def targets ( ) : <EOL> return targets_for_instances ( get_running_instances ( get_instances ( ) ) ) </s>
