Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
a76a6ee7
Commit
a76a6ee7
authored
Feb 11, 2015
by
Brian Coca
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #10211 from cchurch/tower_ec2_inventory
Update EC2 inventory script with changes from Tower.
parents
67fb3945
05bb3145
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
144 additions
and
71 deletions
+144
-71
plugins/inventory/ec2.ini
+16
-0
plugins/inventory/ec2.py
+128
-71
No files found.
plugins/inventory/ec2.ini
View file @
a76a6ee7
...
@@ -71,6 +71,22 @@ cache_max_age = 300
...
@@ -71,6 +71,22 @@ cache_max_age = 300
# Organize groups into a nested/hierarchy instead of a flat namespace.
# Organize groups into a nested/hierarchy instead of a flat namespace.
nested_groups
=
False
nested_groups
=
False
# The EC2 inventory output can become very large. To manage its size,
# configure which groups should be created.
group_by_instance_id
=
True
group_by_region
=
True
group_by_availability_zone
=
True
group_by_ami_id
=
True
group_by_instance_type
=
True
group_by_key_pair
=
True
group_by_vpc_id
=
True
group_by_security_group
=
True
group_by_tag_keys
=
True
group_by_tag_none
=
True
group_by_route53_names
=
True
group_by_rds_engine
=
True
group_by_rds_parameter_group
=
True
# If you only want to include hosts that match a certain regular expression
# If you only want to include hosts that match a certain regular expression
# pattern_include = stage-*
# pattern_include = stage-*
...
...
plugins/inventory/ec2.py
View file @
a76a6ee7
...
@@ -253,6 +253,28 @@ class Ec2Inventory(object):
...
@@ -253,6 +253,28 @@ class Ec2Inventory(object):
else
:
else
:
self
.
nested_groups
=
False
self
.
nested_groups
=
False
# Configure which groups should be created.
group_by_options
=
[
'group_by_instance_id'
,
'group_by_region'
,
'group_by_availability_zone'
,
'group_by_ami_id'
,
'group_by_instance_type'
,
'group_by_key_pair'
,
'group_by_vpc_id'
,
'group_by_security_group'
,
'group_by_tag_keys'
,
'group_by_tag_none'
,
'group_by_route53_names'
,
'group_by_rds_engine'
,
'group_by_rds_parameter_group'
,
]
for
option
in
group_by_options
:
if
config
.
has_option
(
'ec2'
,
option
):
setattr
(
self
,
option
,
config
.
getboolean
(
'ec2'
,
option
))
else
:
setattr
(
self
,
option
,
True
)
# Do we need to just include hosts that match a pattern?
# Do we need to just include hosts that match a pattern?
try
:
try
:
pattern_include
=
config
.
get
(
'ec2'
,
'pattern_include'
)
pattern_include
=
config
.
get
(
'ec2'
,
'pattern_include'
)
...
@@ -273,11 +295,16 @@ class Ec2Inventory(object):
...
@@ -273,11 +295,16 @@ class Ec2Inventory(object):
except
ConfigParser
.
NoOptionError
,
e
:
except
ConfigParser
.
NoOptionError
,
e
:
self
.
pattern_exclude
=
None
self
.
pattern_exclude
=
None
# Instance filters (see boto and EC2 API docs)
# Instance filters (see boto and EC2 API docs)
. Ignore invalid filters.
self
.
ec2_instance_filters
=
defaultdict
(
list
)
self
.
ec2_instance_filters
=
defaultdict
(
list
)
if
config
.
has_option
(
'ec2'
,
'instance_filters'
):
if
config
.
has_option
(
'ec2'
,
'instance_filters'
):
for
x
in
config
.
get
(
'ec2'
,
'instance_filters'
,
''
)
.
split
(
','
):
for
instance_filter
in
config
.
get
(
'ec2'
,
'instance_filters'
,
''
)
.
split
(
','
):
filter_key
,
filter_value
=
x
.
split
(
'='
)
instance_filter
=
instance_filter
.
strip
()
if
not
instance_filter
or
'='
not
in
instance_filter
:
continue
filter_key
,
filter_value
=
[
x
.
strip
()
for
x
in
instance_filter
.
split
(
'='
,
1
)]
if
not
filter_key
:
continue
self
.
ec2_instance_filters
[
filter_key
]
.
append
(
filter_value
)
self
.
ec2_instance_filters
[
filter_key
]
.
append
(
filter_value
)
def
parse_cli_args
(
self
):
def
parse_cli_args
(
self
):
...
@@ -409,60 +436,77 @@ class Ec2Inventory(object):
...
@@ -409,60 +436,77 @@ class Ec2Inventory(object):
self
.
index
[
dest
]
=
[
region
,
instance
.
id
]
self
.
index
[
dest
]
=
[
region
,
instance
.
id
]
# Inventory: Group by instance ID (always a group of 1)
# Inventory: Group by instance ID (always a group of 1)
self
.
inventory
[
instance
.
id
]
=
[
dest
]
if
self
.
group_by_instance_id
:
if
self
.
nested_groups
:
self
.
inventory
[
instance
.
id
]
=
[
dest
]
self
.
push_group
(
self
.
inventory
,
'instances'
,
instance
.
id
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'instances'
,
instance
.
id
)
# Inventory: Group by region
# Inventory: Group by region
if
self
.
nested_groups
:
if
self
.
group_by_region
:
self
.
push_group
(
self
.
inventory
,
'regions'
,
region
)
else
:
self
.
push
(
self
.
inventory
,
region
,
dest
)
self
.
push
(
self
.
inventory
,
region
,
dest
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'regions'
,
region
)
# Inventory: Group by availability zone
# Inventory: Group by availability zone
self
.
push
(
self
.
inventory
,
instance
.
placement
,
dest
)
if
self
.
group_by_availability_zone
:
if
self
.
nested_groups
:
self
.
push
(
self
.
inventory
,
instance
.
placement
,
dest
)
self
.
push_group
(
self
.
inventory
,
region
,
instance
.
placement
)
if
self
.
nested_groups
:
if
self
.
group_by_region
:
self
.
push_group
(
self
.
inventory
,
region
,
instance
.
placement
)
self
.
push_group
(
self
.
inventory
,
'zones'
,
instance
.
placement
)
# Inventory: Group by Amazon Machine Image (AMI) ID
if
self
.
group_by_ami_id
:
ami_id
=
self
.
to_safe
(
instance
.
image_id
)
self
.
push
(
self
.
inventory
,
ami_id
,
dest
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'images'
,
ami_id
)
# Inventory: Group by instance type
# Inventory: Group by instance type
type_name
=
self
.
to_safe
(
'type_'
+
instance
.
instance_type
)
if
self
.
group_by_instance_type
:
self
.
push
(
self
.
inventory
,
type_name
,
dest
)
type_name
=
self
.
to_safe
(
'type_'
+
instance
.
instance_type
)
if
self
.
nested_groups
:
self
.
push
(
self
.
inventory
,
type_name
,
dest
)
self
.
push_group
(
self
.
inventory
,
'types'
,
type_name
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'types'
,
type_name
)
# Inventory: Group by key pair
# Inventory: Group by key pair
if
instance
.
key_name
:
if
self
.
group_by_key_pair
and
instance
.
key_name
:
key_name
=
self
.
to_safe
(
'key_'
+
instance
.
key_name
)
key_name
=
self
.
to_safe
(
'key_'
+
instance
.
key_name
)
self
.
push
(
self
.
inventory
,
key_name
,
dest
)
self
.
push
(
self
.
inventory
,
key_name
,
dest
)
if
self
.
nested_groups
:
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'keys'
,
key_name
)
self
.
push_group
(
self
.
inventory
,
'keys'
,
key_name
)
# Inventory: Group by VPC
# Inventory: Group by VPC
if
instance
.
vpc_id
:
if
self
.
group_by_vpc_id
and
instance
.
vpc_id
:
self
.
push
(
self
.
inventory
,
self
.
to_safe
(
'vpc_id_'
+
instance
.
vpc_id
),
dest
)
vpc_id_name
=
self
.
to_safe
(
'vpc_id_'
+
instance
.
vpc_id
)
self
.
push
(
self
.
inventory
,
vpc_id_name
,
dest
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'vpcs'
,
vpc_id_name
)
# Inventory: Group by security group
# Inventory: Group by security group
try
:
if
self
.
group_by_security_group
:
for
group
in
instance
.
groups
:
try
:
key
=
self
.
to_safe
(
"security_group_"
+
group
.
name
)
for
group
in
instance
.
groups
:
self
.
push
(
self
.
inventory
,
key
,
dest
)
key
=
self
.
to_safe
(
"security_group_"
+
group
.
name
)
if
self
.
nested_groups
:
self
.
push
(
self
.
inventory
,
key
,
dest
)
self
.
push_group
(
self
.
inventory
,
'security_groups'
,
key
)
if
self
.
nested_groups
:
except
AttributeError
:
self
.
push_group
(
self
.
inventory
,
'security_groups'
,
key
)
print
'Package boto seems a bit older.'
except
AttributeError
:
print
'Please upgrade boto >= 2.3.0.'
print
'Package boto seems a bit older.'
sys
.
exit
(
1
)
print
'Please upgrade boto >= 2.3.0.'
sys
.
exit
(
1
)
# Inventory: Group by tag keys
# Inventory: Group by tag keys
for
k
,
v
in
instance
.
tags
.
iteritems
():
if
self
.
group_by_tag_keys
:
key
=
self
.
to_safe
(
"tag_"
+
k
+
"="
+
v
)
for
k
,
v
in
instance
.
tags
.
iteritems
():
self
.
push
(
self
.
inventory
,
key
,
dest
)
key
=
self
.
to_safe
(
"tag_"
+
k
+
"="
+
v
)
if
self
.
nested_groups
:
self
.
push
(
self
.
inventory
,
key
,
dest
)
self
.
push_group
(
self
.
inventory
,
'tags'
,
self
.
to_safe
(
"tag_"
+
k
))
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
self
.
to_safe
(
"tag_"
+
k
),
key
)
self
.
push_group
(
self
.
inventory
,
'tags'
,
self
.
to_safe
(
"tag_"
+
k
))
self
.
push_group
(
self
.
inventory
,
self
.
to_safe
(
"tag_"
+
k
),
key
)
# Inventory: Group by Route53 domain names if enabled
# Inventory: Group by Route53 domain names if enabled
if
self
.
route53_enabled
:
if
self
.
route53_enabled
and
self
.
group_by_route53_names
:
route53_names
=
self
.
get_instance_route53_names
(
instance
)
route53_names
=
self
.
get_instance_route53_names
(
instance
)
for
name
in
route53_names
:
for
name
in
route53_names
:
self
.
push
(
self
.
inventory
,
name
,
dest
)
self
.
push
(
self
.
inventory
,
name
,
dest
)
...
@@ -470,9 +514,11 @@ class Ec2Inventory(object):
...
@@ -470,9 +514,11 @@ class Ec2Inventory(object):
self
.
push_group
(
self
.
inventory
,
'route53'
,
name
)
self
.
push_group
(
self
.
inventory
,
'route53'
,
name
)
# Global Tag: instances without tags
# Global Tag: instances without tags
if
len
(
instance
.
tags
)
==
0
:
if
self
.
group_by_tag_none
and
len
(
instance
.
tags
)
==
0
:
self
.
push
(
self
.
inventory
,
'tag_none'
,
dest
)
self
.
push
(
self
.
inventory
,
'tag_none'
,
dest
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'tags'
,
'tag_none'
)
# Global Tag: tag all EC2 instances
# Global Tag: tag all EC2 instances
self
.
push
(
self
.
inventory
,
'ec2'
,
dest
)
self
.
push
(
self
.
inventory
,
'ec2'
,
dest
)
...
@@ -488,10 +534,6 @@ class Ec2Inventory(object):
...
@@ -488,10 +534,6 @@ class Ec2Inventory(object):
return
return
# Select the best destination address
# Select the best destination address
#if instance.subnet_id:
#dest = getattr(instance, self.vpc_destination_variable)
#else:
#dest = getattr(instance, self.destination_variable)
dest
=
instance
.
endpoint
[
0
]
dest
=
instance
.
endpoint
[
0
]
if
not
dest
:
if
not
dest
:
...
@@ -502,49 +544,64 @@ class Ec2Inventory(object):
...
@@ -502,49 +544,64 @@ class Ec2Inventory(object):
self
.
index
[
dest
]
=
[
region
,
instance
.
id
]
self
.
index
[
dest
]
=
[
region
,
instance
.
id
]
# Inventory: Group by instance ID (always a group of 1)
# Inventory: Group by instance ID (always a group of 1)
self
.
inventory
[
instance
.
id
]
=
[
dest
]
if
self
.
group_by_instance_id
:
if
self
.
nested_groups
:
self
.
inventory
[
instance
.
id
]
=
[
dest
]
self
.
push_group
(
self
.
inventory
,
'instances'
,
instance
.
id
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'instances'
,
instance
.
id
)
# Inventory: Group by region
# Inventory: Group by region
if
self
.
nested_groups
:
if
self
.
group_by_region
:
self
.
push_group
(
self
.
inventory
,
'regions'
,
region
)
else
:
self
.
push
(
self
.
inventory
,
region
,
dest
)
self
.
push
(
self
.
inventory
,
region
,
dest
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'regions'
,
region
)
# Inventory: Group by availability zone
# Inventory: Group by availability zone
self
.
push
(
self
.
inventory
,
instance
.
availability_zone
,
dest
)
if
self
.
group_by_availability_zone
:
if
self
.
nested_groups
:
self
.
push
(
self
.
inventory
,
instance
.
availability_zone
,
dest
)
self
.
push_group
(
self
.
inventory
,
region
,
instance
.
availability_zone
)
if
self
.
nested_groups
:
if
self
.
group_by_region
:
self
.
push_group
(
self
.
inventory
,
region
,
instance
.
availability_zone
)
self
.
push_group
(
self
.
inventory
,
'zones'
,
instance
.
availability_zone
)
# Inventory: Group by instance type
# Inventory: Group by instance type
type_name
=
self
.
to_safe
(
'type_'
+
instance
.
instance_class
)
if
self
.
group_by_instance_type
:
self
.
push
(
self
.
inventory
,
type_name
,
dest
)
type_name
=
self
.
to_safe
(
'type_'
+
instance
.
instance_class
)
if
self
.
nested_groups
:
self
.
push
(
self
.
inventory
,
type_name
,
dest
)
self
.
push_group
(
self
.
inventory
,
'types'
,
type_name
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'types'
,
type_name
)
# Inventory: Group by VPC
if
self
.
group_by_vpc_id
and
instance
.
subnet_group
and
instance
.
subnet_group
.
vpc_id
:
vpc_id_name
=
self
.
to_safe
(
'vpc_id_'
+
instance
.
subnet_group
.
vpc_id
)
self
.
push
(
self
.
inventory
,
vpc_id_name
,
dest
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'vpcs'
,
vpc_id_name
)
# Inventory: Group by security group
# Inventory: Group by security group
try
:
if
self
.
group_by_security_group
:
if
instance
.
security_group
:
try
:
key
=
self
.
to_safe
(
"security_group_"
+
instance
.
security_group
.
name
)
if
instance
.
security_group
:
self
.
push
(
self
.
inventory
,
key
,
dest
)
key
=
self
.
to_safe
(
"security_group_"
+
instance
.
security_group
.
name
)
if
self
.
nested_groups
:
self
.
push
(
self
.
inventory
,
key
,
dest
)
self
.
push_group
(
self
.
inventory
,
'security_groups'
,
key
)
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'security_groups'
,
key
)
except
AttributeError
:
except
AttributeError
:
print
'Package boto seems a bit older.'
print
'Package boto seems a bit older.'
print
'Please upgrade boto >= 2.3.0.'
print
'Please upgrade boto >= 2.3.0.'
sys
.
exit
(
1
)
sys
.
exit
(
1
)
# Inventory: Group by engine
# Inventory: Group by engine
self
.
push
(
self
.
inventory
,
self
.
to_safe
(
"rds_"
+
instance
.
engine
),
dest
)
if
self
.
group_by_rds_engine
:
if
self
.
nested_groups
:
self
.
push
(
self
.
inventory
,
self
.
to_safe
(
"rds_"
+
instance
.
engine
),
dest
)
self
.
push_group
(
self
.
inventory
,
'rds_engines'
,
self
.
to_safe
(
"rds_"
+
instance
.
engine
))
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'rds_engines'
,
self
.
to_safe
(
"rds_"
+
instance
.
engine
))
# Inventory: Group by parameter group
# Inventory: Group by parameter group
self
.
push
(
self
.
inventory
,
self
.
to_safe
(
"rds_parameter_group_"
+
instance
.
parameter_group
.
name
),
dest
)
if
self
.
group_by_rds_parameter_group
:
if
self
.
nested_groups
:
self
.
push
(
self
.
inventory
,
self
.
to_safe
(
"rds_parameter_group_"
+
instance
.
parameter_group
.
name
),
dest
)
self
.
push_group
(
self
.
inventory
,
'rds_parameter_groups'
,
self
.
to_safe
(
"rds_parameter_group_"
+
instance
.
parameter_group
.
name
))
if
self
.
nested_groups
:
self
.
push_group
(
self
.
inventory
,
'rds_parameter_groups'
,
self
.
to_safe
(
"rds_parameter_group_"
+
instance
.
parameter_group
.
name
))
# Global Tag: all RDS instances
# Global Tag: all RDS instances
self
.
push
(
self
.
inventory
,
'rds'
,
dest
)
self
.
push
(
self
.
inventory
,
'rds'
,
dest
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment