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
74068bc5
Commit
74068bc5
authored
Jul 31, 2014
by
James Cammarata
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #8189 from bcoca/vmware_inv_env
vmware inventory fixes and features
parents
ac67351d
b65536bc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
54 deletions
+54
-54
plugins/inventory/vmware.ini
+2
-2
plugins/inventory/vmware.py
+52
-52
No files found.
plugins/inventory/vmware.ini
View file @
74068bc5
...
...
@@ -6,8 +6,8 @@ guests_only = True
#hw_group =
[cache]
cache_
max_age
=
3600
cache_dir
=
/var/tmp
max_age
=
3600
dir
=
~/.cache/ansible
[auth]
host
=
vcenter.example.com
...
...
plugins/inventory/vmware.py
View file @
74068bc5
...
...
@@ -14,6 +14,9 @@ i.e vmware.py/vmware_colo.ini vmware_idf.py/vmware_idf.ini
so if you don't have clustered vcenter but multiple esx machines or
just diff clusters you can have a inventory per each and automatically
group hosts based on file name or specify a group in the ini.
You can also use <SCRIPT_NAME>_HOST|USER|PASSWORD environment variables
to override the ini.
'''
import
os
...
...
@@ -31,44 +34,51 @@ except ImportError:
def
save_cache
(
cache_item
,
data
,
config
):
''' saves item to cache '''
dpath
=
config
.
get
(
'defaults'
,
'cache_dir'
)
try
:
cache
=
open
(
'/'
.
join
([
dpath
,
cache_item
]),
'w'
)
cache
.
write
(
json
.
dumps
(
data
))
cache
.
close
()
except
IOError
,
e
:
pass
# not really sure what to do here
if
config
.
has_option
(
'cache'
,
'dir'
):
dpath
=
os
.
path
.
expanduser
(
config
.
get
(
'cache'
,
'dir'
))
try
:
if
not
os
.
path
.
exists
(
dpath
):
os
.
makedirs
(
dpath
)
if
os
.
path
.
isdir
(
dpath
):
cache
=
open
(
'/'
.
join
([
dpath
,
cache_item
]),
'w'
)
cache
.
write
(
json
.
dumps
(
data
))
cache
.
close
()
except
IOError
,
e
:
pass
# not really sure what to do here
def
get_cache
(
cache_item
,
config
):
''' returns cached item '''
dpath
=
config
.
get
(
'defaults'
,
'cache_dir'
)
inv
=
{}
try
:
cache
=
open
(
'/'
.
join
([
dpath
,
cache_item
]),
'r'
)
inv
=
json
.
loads
(
cache
.
read
())
cache
.
close
()
except
IOError
,
e
:
pass
# not really sure what to do here
if
config
.
has_option
(
'cache'
,
'dir'
):
dpath
=
os
.
path
.
expanduser
(
config
.
get
(
'cache'
,
'dir'
))
try
:
cache
=
open
(
'/'
.
join
([
dpath
,
cache_item
]),
'r'
)
inv
=
json
.
loads
(
cache
.
read
())
cache
.
close
()
except
IOError
,
e
:
pass
# not really sure what to do here
return
inv
def
cache_available
(
cache_item
,
config
):
''' checks if we have a 'fresh' cache available for item requested '''
if
config
.
has_option
(
'
defaults'
,
'cache_
dir'
):
dpath
=
config
.
get
(
'defaults'
,
'cache_dir'
)
if
config
.
has_option
(
'
cache'
,
'
dir'
):
dpath
=
os
.
path
.
expanduser
(
config
.
get
(
'cache'
,
'dir'
)
)
try
:
existing
=
os
.
stat
(
'/'
.
join
([
dpath
,
cache_item
]))
existing
=
os
.
stat
(
'/'
.
join
([
dpath
,
cache_item
]))
except
:
# cache doesn't exist or isn't accessible
return
False
if
config
.
has_option
(
'
defaults'
,
'cache_
max_age'
):
maxage
=
config
.
get
(
'
defaults'
,
'cache_
max_age'
)
if
(
existing
.
st_mtime
-
int
(
time
.
time
()))
<=
maxage
:
if
config
.
has_option
(
'
cache'
,
'
max_age'
):
maxage
=
config
.
get
(
'
cache'
,
'
max_age'
)
fileage
=
int
(
time
.
time
()
-
existing
.
st_mtime
)
if
(
maxage
>
fileage
)
:
return
True
return
False
...
...
@@ -77,10 +87,8 @@ def get_host_info(host):
''' Get variables about a specific host '''
hostinfo
=
{
'vmware_name'
:
host
.
name
,
'vmware_tag'
:
host
.
tag
,
'vmware_parent'
:
host
.
parent
.
name
,
}
'vmware_name'
:
host
.
name
,
}
for
k
in
host
.
capability
.
__dict__
.
keys
():
if
k
.
startswith
(
'_'
):
continue
...
...
@@ -95,9 +103,11 @@ def get_host_info(host):
def
get_inventory
(
client
,
config
):
''' Reads the inventory from cache or vmware api '''
inv
=
{}
if
cache_available
(
'inventory'
,
config
):
inv
=
get_cache
(
'inventory'
,
config
)
el
se
:
el
if
client
:
inv
=
{
'all'
:
{
'hosts'
:
[]},
'_meta'
:
{
'hostvars'
:
{}
}
}
default_group
=
os
.
path
.
basename
(
sys
.
argv
[
0
])
.
rstrip
(
'.py'
)
...
...
@@ -125,39 +135,25 @@ def get_inventory(client, config):
if
not
guests_only
:
inv
[
'all'
][
'hosts'
]
.
append
(
host
.
name
)
inv
[
hw_group
]
.
append
(
host
.
name
)
if
host
.
tag
:
taggroup
=
'vmware_'
+
host
.
tag
if
taggroup
in
inv
:
inv
[
taggroup
]
.
append
(
host
.
name
)
else
:
inv
[
taggroup
]
=
[
host
.
name
]
inv
[
'_meta'
][
'hostvars'
][
host
.
name
]
=
get_host_info
(
host
)
save_cache
(
vm
.
name
,
inv
[
'_meta'
][
'hostvars'
][
host
.
name
],
config
)
for
vm
in
host
.
vm
:
inv
[
'all'
][
'hosts'
]
.
append
(
vm
.
name
)
inv
[
vm_group
]
.
append
(
vm
.
name
)
if
vm
.
tag
:
taggroup
=
'vmware_'
+
vm
.
tag
if
taggroup
in
inv
:
inv
[
taggroup
]
.
append
(
vm
.
name
)
else
:
inv
[
taggroup
]
=
[
vm
.
name
]
inv
[
'_meta'
][
'hostvars'
][
vm
.
name
]
=
get_host_info
(
host
)
inv
[
'_meta'
][
'hostvars'
][
vm
.
name
]
=
get_host_info
(
vm
)
save_cache
(
vm
.
name
,
inv
[
'_meta'
][
'hostvars'
][
vm
.
name
],
config
)
save_cache
(
'inventory'
,
inv
,
config
)
save_cache
(
'inventory'
,
inv
,
config
)
return
json
.
dumps
(
inv
)
def
get_single_host
(
client
,
config
,
hostname
):
inv
=
{}
if
cache_available
(
hostname
,
config
):
inv
=
get_cache
(
hostname
,
config
)
el
se
:
el
if
client
:
hosts
=
HostSystem
.
all
(
client
)
#TODO: figure out single host getter
for
host
in
hosts
:
if
hostname
==
host
.
name
:
...
...
@@ -165,13 +161,14 @@ def get_single_host(client, config, hostname):
break
for
vm
in
host
.
vm
:
if
hostname
==
vm
.
name
:
inv
=
get_host_info
(
host
)
inv
=
get_host_info
(
vm
)
break
save_cache
(
hostname
,
inv
,
config
)
return
json
.
dumps
(
inv
)
if
__name__
==
'__main__'
:
inventory
=
{}
hostname
=
None
...
...
@@ -181,25 +178,28 @@ if __name__ == '__main__':
# Read config
config
=
ConfigParser
.
SafeConfigParser
()
for
configfilename
in
[
os
.
path
.
abspath
(
sys
.
argv
[
0
])
.
rstrip
(
'.py'
)
+
'.ini'
,
'vmware.ini'
]:
me
=
os
.
path
.
abspath
(
sys
.
argv
[
0
])
.
rstrip
(
'.py'
)
for
configfilename
in
[
me
+
'.ini'
,
'vmware.ini'
]:
if
os
.
path
.
exists
(
configfilename
):
config
.
read
(
configfilename
)
break
mename
=
os
.
path
.
basename
(
me
)
.
upper
()
host
=
os
.
getenv
(
'VMWARE_'
+
mename
+
'_HOST'
,
os
.
getenv
(
'VMWARE_HOST'
,
config
.
get
(
'auth'
,
'host'
)))
user
=
os
.
getenv
(
'VMWARE_'
+
mename
+
'_USER'
,
os
.
getenv
(
'VMWARE_USER'
,
config
.
get
(
'auth'
,
'user'
)))
password
=
os
.
getenv
(
'VMWARE_'
+
mename
+
'_PASSWORD'
,
os
.
getenv
(
'VMWARE_PASSWORD'
,
config
.
get
(
'auth'
,
'password'
)))
try
:
client
=
Client
(
config
.
get
(
'auth'
,
'host'
),
config
.
get
(
'auth'
,
'user'
),
config
.
get
(
'auth'
,
'password'
),
)
client
=
Client
(
host
,
user
,
password
)
except
Exception
,
e
:
client
=
None
#print >> STDERR "Unable to login (only cache avilable): %s", str(e)
#
aci
tually do the work
#
Ac
tually do the work
if
hostname
is
None
:
inventory
=
get_inventory
(
client
,
config
)
else
:
inventory
=
get_single_host
(
client
,
config
,
hostname
)
#
r
eturn to ansible
#
R
eturn to ansible
print
inventory
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