Updates to the ansible API implementation to allow for 43/63143/1
authorspisarski <s.pisarski@cablelabs.com>
Wed, 3 Oct 2018 19:54:39 +0000 (13:54 -0600)
committerspisarski <s.pisarski@cablelabs.com>
Wed, 3 Oct 2018 19:54:39 +0000 (13:54 -0600)
playbooks to be applied directly against the localhost
as well as removing the return code in favor of reliance
of unchecked Errors/Exceptions.

Change-Id: Ib963aa9b4f5a75010d8f7b25db7d66a2e1950cce
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
requirements.txt
setup.cfg
snaps/openstack/create_instance.py
snaps/openstack/utils/launch_utils.py
snaps/playbook_runner.py
snaps/provisioning/ansible_utils.py
snaps/provisioning/tests/ansible_utils_tests.py

index 1b4481f..2404837 100644 (file)
@@ -13,3 +13,5 @@ wrapt>=1.7.0 # BSD License
 scp
 cryptography>=2.1 # BSD/Apache-2.0
 concurrencytest
+jinja2
+keystoneauth1
index c999c81..3d42cd7 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,6 +1,6 @@
 [metadata]
-name = snaps
-version = 1.0
+name = snaps-oo
+version = 7.0.0
 home-page = https://gerrit.opnfv.org/gerrit/gitweb?p=snaps.git;a=summary
 author = Steve Pisarski
 author-email = s.pisarski@cablelabs.com
index 16bd0ce..e2a6991 100644 (file)
@@ -474,9 +474,8 @@ class OpenStackVmInstance(OpenStackComputeObject):
                           playbook
         :param fip_name: the name of the floating IP to use for applying the
                          playbook (default - will take the first)
-        :return: the return value from ansible
         """
-        return ansible_utils.apply_playbook(
+        ansible_utils.apply_playbook(
             pb_file_loc, [self.get_floating_ip(fip_name=fip_name).ip],
             self.get_image_user(),
             ssh_priv_key_file_path=self.keypair_settings.private_filepath,
index 00ea822..bb7600c 100644 (file)
@@ -464,17 +464,13 @@ def __apply_ansible_playbook(ansible_config, os_creds_dict, vm_dict,
                 ansible_config.get('variables'), os_creds_dict, vm_dict,
                 image_dict, flavor_dict, networks_dict, routers_dict)
 
-            retval = ansible_utils.apply_playbook(
+            ansible_utils.apply_playbook(
                 ansible_config['playbook_location'], floating_ips, remote_user,
                 ssh_priv_key_file_path=private_key_filepath,
                 variables=variables,
                 proxy_setting=proxy_settings)
-            if retval != 0:
-                # Not a fatal type of event
-                raise Exception(
-                    'Error applying playbook found at location - %s',
-                    ansible_config.get('playbook_location'))
-            elif ansible_config.get('post_processing'):
+
+            if 'post_processing' in ansible_config:
                 post_proc_config = ansible_config['post_processing']
                 if 'sleep' in post_proc_config:
                     time.sleep(post_proc_config['sleep'])
@@ -484,8 +480,6 @@ def __apply_ansible_playbook(ansible_config, os_creds_dict, vm_dict,
                             logger.info('Rebooting VM - %s', vm_name)
                             vm_dict[vm_name].reboot(RebootType.hard)
 
-            return retval
-
 
 def __get_connection_info(ansible_config, vm_dict):
     """
index 03b7006..7b10390 100644 (file)
@@ -67,12 +67,11 @@ def main(parsed_args):
     if not variables.get('env_file'):
         variables['env_file'] = parsed_args.env_file
 
-    retval = ansible_utils.apply_playbook(
+    ansible_utils.apply_playbook(
         parsed_args.playbook, [parsed_args.ip_addr], parsed_args.host_user,
         ssh_priv_key_file_path=parsed_args.priv_key,
         password=parsed_args.password, variables=variables,
         proxy_setting=proxy_settings)
-    exit(retval)
 
 
 if __name__ == '__main__':
index 9b687f8..b4c86f2 100644 (file)
@@ -53,7 +53,10 @@ def apply_playbook(playbook_path, hosts_inv=None, host_user=None,
     :param variables: a dictionary containing any substitution variables needed
                       by the Jinga 2 templates
     :param proxy_setting: instance of os_credentials.ProxySettings class
-    :return: the results
+    :raises AnsibleException when the return code from the Ansible library is
+            not 0
+    :return: the return code from the Ansible library only when 0.
+             Implementation now raises an exception otherwise
     """
     if not os.path.isfile(playbook_path):
         raise AnsibleException(
@@ -79,8 +82,9 @@ def apply_playbook(playbook_path, hosts_inv=None, host_user=None,
     if hosts_inv:
         for host in hosts_inv:
             inventory.add_host(host=host, group='ungrouped')
+        connection = 'ssh'
     else:
-        inventory.remove_restriction()
+        connection = 'local'
 
     variable_manager = VariableManager(loader=loader, inventory=inventory)
 
@@ -100,10 +104,11 @@ def apply_playbook(playbook_path, hosts_inv=None, host_user=None,
 
     ansible_opts = options(
         listtags=False, listtasks=False, listhosts=False, syntax=False,
-        connection='ssh', module_path=None, forks=100, remote_user=host_user,
-        private_key_file=pk_file_path, ssh_common_args=None,
-        ssh_extra_args=ssh_extra_args, become=None, become_method=None,
-        become_user=None, verbosity=11111, check=False, timeout=30, diff=None)
+        connection=connection, module_path=None, forks=100,
+        remote_user=host_user, private_key_file=pk_file_path,
+        ssh_common_args=None, ssh_extra_args=ssh_extra_args, become=None,
+        become_method=None, become_user=None, verbosity=11111, check=False,
+        timeout=30, diff=None)
 
     logger.debug('Setting up Ansible Playbook Executor for playbook - ' +
                  playbook_path)
@@ -116,7 +121,15 @@ def apply_playbook(playbook_path, hosts_inv=None, host_user=None,
         passwords=passwords)
 
     logger.debug('Executing Ansible Playbook - ' + playbook_path)
-    return executor.run()
+    ret_val = executor.run()
+
+    if ret_val != 0:
+        raise AnsibleException(
+            'Error applying playbook [{}] with value [{}] using the connection'
+            ' type of [{}]'.format(
+                playbook_path, ret_val, connection))
+
+    return ret_val
 
 
 def ssh_client(ip, user, private_key_filepath=None, password=None,
@@ -149,9 +162,10 @@ def ssh_client(ip, user, private_key_filepath=None, password=None,
         ssh.connect(
             ip, username=user, key_filename=pk_abs_path, password=password,
             sock=proxy_cmd)
+        logger.info('Obtained SSH connection to %s', ip)
         return ssh
     except Exception as e:
-        logger.warning('Unable to connect via SSH with message - ' + str(e))
+        logger.debug('Unable to connect via SSH with message - ' + str(e))
 
 
 class AnsibleException(Exception):
index 209b1e0..33e8edd 100644 (file)
@@ -265,8 +265,7 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
 
         relative_pb_path = pkg_resources.resource_filename(
             'snaps.provisioning.tests.playbooks', 'simple_playbook.yml')
-        retval = self.inst_creator.apply_ansible_playbook(relative_pb_path)
-        self.assertEqual(0, retval)
+        self.inst_creator.apply_ansible_playbook(relative_pb_path)
 
         ssh = ansible_utils.ssh_client(
             ip, user, private_key_filepath=priv_key,
@@ -332,9 +331,8 @@ class AnsibleProvisioningTests(OSIntegrationTestCase):
         relative_pb_path = pkg_resources.resource_filename(
             'snaps.provisioning.tests.playbooks',
             'template_playbook.yml')
-        retval = self.inst_creator.apply_ansible_playbook(
+        self.inst_creator.apply_ansible_playbook(
             relative_pb_path, variables={'name': 'Foo'})
-        self.assertEqual(0, retval)
 
         ssh = ansible_utils.ssh_client(
             ip, user, private_key_filepath=priv_key,