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.
This commit is contained in:
Monty Taylor 2014-08-02 15:31:10 -07:00
parent 4e6685d772
commit efb989a50c

View file

@ -17,6 +17,8 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>. # along with this software. If not, see <http://www.gnu.org/licenses/>.
import os
try: try:
from novaclient.v1_1 import client as nova_client from novaclient.v1_1 import client as nova_client
from novaclient import exceptions from novaclient import exceptions
@ -224,13 +226,32 @@ def _get_server_state(module, nova):
def main(): 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( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
login_username = dict(default='admin'), login_username = dict(default=OS_USERNAME),
login_password = dict(required=True), login_password = login_password_arg,
login_tenant_name = dict(required='True'), login_tenant_name = tenant_name_arg,
auth_url = dict(default='http://127.0.0.1:35357/v2.0/'), auth_url = dict(default=OS_AUTH_URL),
region_name = dict(default=None), region_name = dict(default=OS_REGION_NAME),
name = dict(required=True), name = dict(required=True),
image_id = dict(default=None), image_id = dict(default=None),
flavor_id = dict(default=1), flavor_id = dict(default=1),