logger = logging.getLogger('ansible_utils')
-def apply_playbook(playbook_path, hosts_inv, host_user,
+def apply_playbook(playbook_path, hosts_inv=None, host_user=None,
ssh_priv_key_file_path=None, password=None, variables=None,
proxy_setting=None):
"""
Executes an Ansible playbook to the given host
:param playbook_path: the (relative) path to the Ansible playbook
:param hosts_inv: a list of hostnames/ip addresses to which to apply the
- Ansible playbook
+ Ansible playbook (not required when PB is configured for
+ localhost)
:param host_user: A user for the host instances (must be a password-less
- sudo user if playbook has "sudo: yes"
+ sudo user if playbook has "sudo: yes") (not required when
+ PB is configured for localhost)
:param ssh_priv_key_file_path: the file location of the ssh key. Required
- if password is None
+ if password is None (not required when PB is
+ configured for localhost)
:param password: the file location of the ssh key. Required if
- ssh_priv_key_file_path is None
+ ssh_priv_key_file_path is None (not required when PB is
+ configured for localhost)
: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(
pk_file_path = os.path.expanduser(ssh_priv_key_file_path)
if not password:
if not os.path.isfile(pk_file_path):
- raise AnsibleException('Requested private SSH key not found - ' +
- pk_file_path)
-
- if not ssh_priv_key_file_path and not password:
- raise AnsibleException('Invalid credentials, no priv key or password')
+ raise AnsibleException(
+ 'Requested private SSH key not found - ' + pk_file_path)
passwords = None
if password:
loader = DataLoader()
inventory = InventoryManager(loader=loader)
- for host in hosts_inv:
- inventory.add_host(host=host, group='ungrouped')
+ if hosts_inv:
+ for host in hosts_inv:
+ inventory.add_host(host=host, group='ungrouped')
+ connection = 'ssh'
+ else:
+ connection = 'local'
+
variable_manager = VariableManager(loader=loader, inventory=inventory)
+
if variables:
variable_manager.extra_vars = variables
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)
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,
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):