From 9278591758fa81022bb77d30f9e9cb92aff844f4 Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Thu, 26 Mar 2015 21:38:18 -0700 Subject: [PATCH] add cobbler api authentication options add cobbler api authentication options: username and password, which can be provided if authentication is enabled or cobbler api is behind a proxy that needs authentication. --- contrib/inventory/cobbler.ini | 4 ++++ contrib/inventory/cobbler.py | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/contrib/inventory/cobbler.ini b/contrib/inventory/cobbler.ini index b7b63595de..2dc8cd3379 100644 --- a/contrib/inventory/cobbler.ini +++ b/contrib/inventory/cobbler.ini @@ -5,6 +5,10 @@ host = http://PATH_TO_COBBLER_SERVER/cobbler_api +# If API needs authentication add 'username' and 'password' options here. +#username = foo +#password = bar + # API calls to Cobbler can be slow. For this reason, we cache the results of an API # call. Set this to the path you want cache files to be written to. Two files # will be written to this directory: diff --git a/contrib/inventory/cobbler.py b/contrib/inventory/cobbler.py index b5fcdeacbb..89f9bf75b3 100755 --- a/contrib/inventory/cobbler.py +++ b/contrib/inventory/cobbler.py @@ -120,6 +120,9 @@ class CobblerInventory(object): def _connect(self): if not self.conn: self.conn = xmlrpclib.Server(self.cobbler_host, allow_none=True) + self.token = None + if self.cobbler_username is not None: + self.token = self.conn.login(self.cobbler_username, self.cobbler_password) def is_cache_valid(self): """ Determines if the cache files have expired, or if it is still valid """ @@ -140,6 +143,12 @@ class CobblerInventory(object): config.read(os.path.dirname(os.path.realpath(__file__)) + '/cobbler.ini') self.cobbler_host = config.get('cobbler', 'host') + self.cobbler_username = None + self.cobbler_password = None + if config.has_option('cobbler', 'username'): + self.cobbler_username = config.get('cobbler', 'username') + if config.has_option('cobbler', 'password'): + self.cobbler_password = config.get('cobbler', 'password') # Cache related cache_path = config.get('cobbler', 'cache_path') @@ -163,8 +172,10 @@ class CobblerInventory(object): self._connect() self.groups = dict() self.hosts = dict() - - data = self.conn.get_systems() + if self.token is not None: + data = self.conn.get_systems(self.token) + else: + data = self.conn.get_systems() for host in data: # Get the FQDN for the host and add it to the right groups