summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorspisarski <s.pisarski@cablelabs.com>2018-09-27 13:22:54 -0600
committerspisarski <s.pisarski@cablelabs.com>2018-09-27 13:22:54 -0600
commitbee558d02691fc16824cf6dae06e9daaa6a92c8a (patch)
tree44ed0feae34bf737ff3d26a7035464d12f47414f
parent4f8fae85f9fa6b7a82189b6f68f0ac1d70949611 (diff)
Opened up API to allow for playbook application to localhost
by making the user, password, and private key attributes optional. Change-Id: I7e1e2b5848730ae5febde7d7243e741c87e5c7ea Signed-off-by: spisarski <s.pisarski@cablelabs.com>
-rw-r--r--snaps/provisioning/ansible_utils.py30
1 files changed, 18 insertions, 12 deletions
diff --git a/snaps/provisioning/ansible_utils.py b/snaps/provisioning/ansible_utils.py
index 757797e..9b687f8 100644
--- a/snaps/provisioning/ansible_utils.py
+++ b/snaps/provisioning/ansible_utils.py
@@ -32,20 +32,24 @@ __author__ = 'spisarski'
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
@@ -60,11 +64,8 @@ def apply_playbook(playbook_path, hosts_inv, host_user,
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:
@@ -75,9 +76,14 @@ def apply_playbook(playbook_path, hosts_inv, host_user,
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')
+ else:
+ inventory.remove_restriction()
+
variable_manager = VariableManager(loader=loader, inventory=inventory)
+
if variables:
variable_manager.extra_vars = variables