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
2a893ab0
Commit
2a893ab0
authored
Jan 24, 2013
by
Silviu Dicu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ec2 facts module - updated as per comments
parent
ce9e9af6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
25 deletions
+55
-25
library/ec2_facts
+55
-25
No files found.
library/ec2_facts
View file @
2a893ab0
...
...
@@ -24,32 +24,36 @@ short_description: Gathers facts about remote hosts within ec2 (aws)
options: {}
description:
- This module fetches data from the metadata servers in ec2 (aws).
Eucalyptus cloud provides a similar service and this module should
work this cloud provider as well.
notes:
- Parameters to filter on ec2_facts may be added later.
Some of the facts are not returned ( like mapping of the devices - but
may be added later).
examples:
- code: ansible all -m ec2_facts --tree /tmp/facts
description: Obtain facts from ec2 metatdata servers. You will need to
run an instance within ec2.
author: Silviu Dicu
author: Silviu Dicu: silviudicu@gmail.com
'''
import
urllib2
import
socket
import
re
socket
.
setdefaulttimeout
(
5
)
class
Ec2Metadata
(
object
):
ec2_metadata_ur
l
=
'http://169.254.169.254/latest/meta-data/'
ec2_sshdata_ur
l
=
'http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key'
ec2_userdata_ur
l
=
'http://169.254.169.254/latest/user-data/'
ec2_metadata_ur
i
=
'http://169.254.169.254/latest/meta-data/'
ec2_sshdata_ur
i
=
'http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key'
ec2_userdata_ur
i
=
'http://169.254.169.254/latest/user-data/'
def
__init__
(
self
,
ec2_metadata_url
=
None
,
ec2_sshdata_url
=
None
,
ec2_userdata_url
=
None
):
self
.
url_meta
=
ec2_metadata_url
or
self
.
ec2_metadata_url
self
.
url_user
=
ec2_userdata_url
or
self
.
ec2_userdata_url
self
.
url_ssh
=
ec2_sshdata_url
or
self
.
ec2_sshdata_url
def
__init__
(
self
,
ec2_metadata_uri
=
None
,
ec2_sshdata_uri
=
None
,
ec2_userdata_uri
=
None
):
self
.
uri_meta
=
ec2_metadata_uri
or
self
.
ec2_metadata_uri
self
.
uri_user
=
ec2_userdata_uri
or
self
.
ec2_userdata_uri
self
.
uri_ssh
=
ec2_sshdata_uri
or
self
.
ec2_sshdata_uri
self
.
_data
=
{}
self
.
_prefix
=
'ansible_ec2_
%
s'
def
_fetch
(
self
,
url
):
try
:
...
...
@@ -59,22 +63,48 @@ class Ec2Metadata(object):
except
urllib2
.
URLError
:
return
def
run
(
self
,
field
=
None
):
data
=
{}
raw_fields
=
self
.
_fetch
(
self
.
url_meta
)
if
not
raw_fields
:
return
data
fields
=
raw_fields
.
split
(
'
\n
'
)
for
field
in
fields
:
if
field
.
endswith
(
'/'
):
continue
# deal with this later
field_data
=
self
.
_fetch
(
self
.
url_meta
+
field
)
if
field
==
'security-groups'
:
sg_fields
=
","
.
join
(
field_data
.
split
(
'
\n
'
))
data
[
'ansible_ec2_
%
s'
%
field
]
=
sg_fields
def
_mangle_fields
(
self
,
fields
,
uri
,
filter_patterns
=
[
'public-keys-0'
]):
new_fields
=
{}
for
key
,
value
in
fields
.
iteritems
():
split_fields
=
key
[
len
(
uri
):]
.
split
(
'/'
)
if
len
(
split_fields
)
>
1
and
split_fields
[
1
]:
new_key
=
"-"
.
join
(
split_fields
)
new_fields
[
self
.
_prefix
%
new_key
]
=
value
else
:
data
[
'ansible_ec2_
%
s'
%
field
]
=
field_data
data
[
'ansible_ec2_
%
s'
%
'user-data'
]
=
self
.
_fetch
(
self
.
url_user
)
data
[
'ensible_ec2_
%
s'
%
'public-keys'
]
=
self
.
_fetch
(
self
.
url_ssh
)
new_key
=
""
.
join
(
split_fields
)
new_fields
[
self
.
_prefix
%
new_key
]
=
value
for
pattern
in
filter_patterns
:
for
key
in
new_fields
.
keys
():
match
=
re
.
search
(
pattern
,
key
)
if
match
:
new_fields
.
pop
(
key
)
return
new_fields
def
fetch
(
self
,
uri
,
recurse
=
True
):
raw_subfields
=
self
.
_fetch
(
uri
)
if
not
raw_subfields
:
return
subfields
=
raw_subfields
.
split
(
'
\n
'
)
for
field
in
subfields
:
if
field
.
endswith
(
'/'
)
and
recurse
:
self
.
fetch
(
uri
+
field
)
if
uri
.
endswith
(
'/'
):
new_uri
=
uri
+
field
else
:
new_uri
=
uri
+
'/'
+
field
if
new_uri
not
in
self
.
_data
and
not
new_uri
.
endswith
(
'/'
):
content
=
self
.
_fetch
(
new_uri
)
if
field
==
'security-groups'
:
sg_fields
=
","
.
join
(
content
.
split
(
'
\n
'
))
self
.
_data
[
'
%
s'
%
(
new_uri
)]
=
sg_fields
else
:
self
.
_data
[
'
%
s'
%
(
new_uri
)]
=
content
def
run
(
self
):
self
.
fetch
(
self
.
uri_meta
)
# populate _data
data
=
self
.
_mangle_fields
(
self
.
_data
,
self
.
uri_meta
)
data
[
self
.
_prefix
%
'user-data'
]
=
self
.
_fetch
(
self
.
uri_user
)
data
[
self
.
_prefix
%
'public-key'
]
=
self
.
_fetch
(
self
.
uri_ssh
)
return
data
...
...
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