From f254c915f0a2b5356b4e7172a4a602910c6da66e Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Sat, 2 Aug 2014 15:31:10 -0700 Subject: [PATCH] Consume standard OpenStack environment settings The OpenStack client utilities consume a set of input environment variables for things like username and auth_url, so it's very common for OpenStack users to have such settings set in their environment. Indeed, things like devstack also output a shell file to be sourced to set them. Although in a playbook it's entirely expected that variables should be used to pass in system settings like api passwords, for ad-hoc command line usage, needing to pass in five parameters which are almost certainly in the environment already reduces the utility. Grab the environment variables and inject them as default. Special care is taken to ensure that in the case where the values are not found, the behavior of which parameters are required is not altered. --- cloud/nova_compute | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/cloud/nova_compute b/cloud/nova_compute index 37df3665ea4..8a3725ed2fc 100644 --- a/cloud/nova_compute +++ b/cloud/nova_compute @@ -17,6 +17,8 @@ # You should have received a copy of the GNU General Public License # along with this software. If not, see . +import os + try: from novaclient.v1_1 import client as nova_client from novaclient import exceptions @@ -224,13 +226,32 @@ def _get_server_state(module, nova): def main(): + # Consume standard OpenStack environment variables. + # This is mainly only useful for ad-hoc command line operation as + # in playbooks one would assume variables would be used appropriately + OS_USERNAME=os.environ.get('OS_USERNAME', 'admin') + OS_PASSWORD=os.environ.get('OS_PASSWORD', None) + login_password_arg = dict() + if OS_PASSWORD: + login_password_arg['default'] = OS_PASSWORD + else: + login_password_arg['required'] = True + OS_TENANT_NAME=os.environ.get('OS_TENANT_NAME', None) + tenant_name_arg = dict() + if OS_TENANT_NAME: + tenant_name_arg['default'] = OS_TENANT_NAME + else: + tenant_name_arg['required'] = True + OS_REGION_NAME=os.environ.get('OS_REGION_NAME', None) + OS_AUTH_URL=os.environ.get('OS_AUTH_URL', 'http://127.0.0.1:35357/v2.0/') + module = AnsibleModule( argument_spec = dict( - login_username = dict(default='admin'), - login_password = dict(required=True), - login_tenant_name = dict(required='True'), - auth_url = dict(default='http://127.0.0.1:35357/v2.0/'), - region_name = dict(default=None), + login_username = dict(default=OS_USERNAME), + login_password = login_password_arg, + login_tenant_name = tenant_name_arg, + auth_url = dict(default=OS_AUTH_URL), + region_name = dict(default=OS_REGION_NAME), name = dict(required=True), image_id = dict(default=None), flavor_id = dict(default=1),