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
0ab970da
Commit
0ab970da
authored
Jul 23, 2015
by
dmccue
Committed by
James Cammarata
Aug 07, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support for _meta retrieval
parent
a91428c6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
18 deletions
+24
-18
contrib/inventory/cobbler.py
+24
-18
No files found.
contrib/inventory/cobbler.py
View file @
0ab970da
...
@@ -30,9 +30,14 @@ See http://ansible.github.com/api.html for more info
...
@@ -30,9 +30,14 @@ See http://ansible.github.com/api.html for more info
Tested with Cobbler 2.0.11.
Tested with Cobbler 2.0.11.
Changelog:
Changelog:
- 2015-06-21 dmccue: Heavily modified to support run-once _meta retrieval, results in
higher performance at ansible startup. Groups are determined by owner rather than
default mgmt_classes. DNS name determined from hostname.
- 2013-09-01 pgehres: Refactored implementation to make use of caching and to
- 2013-09-01 pgehres: Refactored implementation to make use of caching and to
limit the number of connections to external cobbler server for performance.
limit the number of connections to external cobbler server for performance.
Added use of cobbler.ini file to configure settings. Tested with Cobbler 2.4.0
Added use of cobbler.ini file to configure settings. Tested with Cobbler 2.4.0
"""
"""
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
...
@@ -54,7 +59,6 @@ Changelog:
...
@@ -54,7 +59,6 @@ Changelog:
######################################################################
######################################################################
import
argparse
import
argparse
import
ConfigParser
import
ConfigParser
import
os
import
os
...
@@ -75,6 +79,7 @@ except ImportError:
...
@@ -75,6 +79,7 @@ except ImportError:
class
CobblerInventory
(
object
):
class
CobblerInventory
(
object
):
def
__init__
(
self
):
def
__init__
(
self
):
""" Main execution path """
""" Main execution path """
self
.
conn
=
None
self
.
conn
=
None
...
@@ -97,15 +102,15 @@ class CobblerInventory(object):
...
@@ -97,15 +102,15 @@ class CobblerInventory(object):
data_to_print
=
""
data_to_print
=
""
# Data to print
# Data to print
if
self
.
args
.
host
:
#if self.args.host:
data_to_print
=
self
.
get_host_info
()
# data_to_print = self.get_host_info()
self
.
inventory
[
'_meta'
]
=
{
'hostvars'
:
{}
}
for
hostname
in
self
.
cache
:
self
.
inventory
[
'_meta'
][
'hostvars'
][
hostname
]
=
{
'cobbler'
:
self
.
cache
[
hostname
]
}
elif
self
.
args
.
list
:
# Display list of instances for inventory
data_to_print
=
self
.
json_format_dict
(
self
.
inventory
,
True
)
else
:
# default action with no options
data_to_print
=
self
.
json_format_dict
(
self
.
inventory
,
True
)
data_to_print
=
self
.
json_format_dict
(
self
.
inventory
,
True
)
print
data_to_print
print
data_to_print
...
@@ -144,7 +149,7 @@ class CobblerInventory(object):
...
@@ -144,7 +149,7 @@ class CobblerInventory(object):
parser
=
argparse
.
ArgumentParser
(
description
=
'Produce an Ansible Inventory file based on Cobbler'
)
parser
=
argparse
.
ArgumentParser
(
description
=
'Produce an Ansible Inventory file based on Cobbler'
)
parser
.
add_argument
(
'--list'
,
action
=
'store_true'
,
default
=
True
,
help
=
'List instances (default: True)'
)
parser
.
add_argument
(
'--list'
,
action
=
'store_true'
,
default
=
True
,
help
=
'List instances (default: True)'
)
parser
.
add_argument
(
'--host'
,
action
=
'store'
,
help
=
'Get all the variables about a specific instance'
)
#
parser.add_argument('--host', action='store', help='Get all the variables about a specific instance')
parser
.
add_argument
(
'--refresh-cache'
,
action
=
'store_true'
,
default
=
False
,
parser
.
add_argument
(
'--refresh-cache'
,
action
=
'store_true'
,
default
=
False
,
help
=
'Force refresh of cache by making API requests to cobbler (default: False - use cache files)'
)
help
=
'Force refresh of cache by making API requests to cobbler (default: False - use cache files)'
)
self
.
args
=
parser
.
parse_args
()
self
.
args
=
parser
.
parse_args
()
...
@@ -160,21 +165,22 @@ class CobblerInventory(object):
...
@@ -160,21 +165,22 @@ class CobblerInventory(object):
for
host
in
data
:
for
host
in
data
:
# Get the FQDN for the host and add it to the right groups
# Get the FQDN for the host and add it to the right groups
dns_name
=
None
dns_name
=
host
[
'hostname'
]
#
None
ksmeta
=
None
ksmeta
=
None
interfaces
=
host
[
'interfaces'
]
interfaces
=
host
[
'interfaces'
]
for
(
iname
,
ivalue
)
in
interfaces
.
iteritems
():
#for (iname, ivalue) in interfaces.iteritems():
if
ivalue
[
'management'
]:
# if ivalue['management']:
this_dns_name
=
ivalue
.
get
(
'dns_name'
,
None
)
# this_dns_name = ivalue.get('dns_name', None)
if
this_dns_name
is
not
None
and
this_dns_name
is
not
""
:
# #this_dns_name = ivalue.get('ip_address', None)
dns_name
=
this_dns_name
# if this_dns_name is not None and this_dns_name is not "":
# dns_name = this_dns_name
if
dns_name
is
None
:
if
dns_name
is
None
:
continue
continue
status
=
host
[
'status'
]
status
=
host
[
'status'
]
profile
=
host
[
'profile'
]
profile
=
host
[
'profile'
]
classes
=
host
[
'mgmt_classes'
]
classes
=
host
[
'
owners'
]
#host['
mgmt_classes']
if
status
not
in
self
.
inventory
:
if
status
not
in
self
.
inventory
:
self
.
inventory
[
status
]
=
[]
self
.
inventory
[
status
]
=
[]
...
@@ -193,7 +199,7 @@ class CobblerInventory(object):
...
@@ -193,7 +199,7 @@ class CobblerInventory(object):
# The old way was ksmeta only -- provide backwards compatibility
# The old way was ksmeta only -- provide backwards compatibility
self
.
cache
[
dns_name
]
=
dict
()
self
.
cache
[
dns_name
]
=
host
#dict() # sub dict with host to output json
if
"ks_meta"
in
host
:
if
"ks_meta"
in
host
:
for
key
,
value
in
host
[
"ks_meta"
]
.
iteritems
():
for
key
,
value
in
host
[
"ks_meta"
]
.
iteritems
():
self
.
cache
[
dns_name
][
key
]
=
value
self
.
cache
[
dns_name
][
key
]
=
value
...
@@ -242,7 +248,7 @@ class CobblerInventory(object):
...
@@ -242,7 +248,7 @@ class CobblerInventory(object):
def
write_to_cache
(
self
,
data
,
filename
):
def
write_to_cache
(
self
,
data
,
filename
):
""" Writes data in JSON format to a file """
""" Writes data in JSON format to a file """
if
data
:
print
"DEBUG: data = "
+
str
(
data
)
json_data
=
self
.
json_format_dict
(
data
,
True
)
json_data
=
self
.
json_format_dict
(
data
,
True
)
cache
=
open
(
filename
,
'w'
)
cache
=
open
(
filename
,
'w'
)
cache
.
write
(
json_data
)
cache
.
write
(
json_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