From 01af85909069b644ee1781a07b2464224bf946f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Moser?= Date: Sun, 30 Oct 2016 12:24:03 +0100 Subject: [PATCH] cloudstack: add support for defining some args as ENV vars (#17946) These ENV vars are: - CLOUDSTACK_ZONE - CLOUDSTACK_DOMAIN - CLOUDSTACK_ACCOUNT - CLOUDSTACK_PROJECT help to DRY on every task, args still have precedence. --- docsite/rst/guide_cloudstack.rst | 37 ++++++++++++++++++++++++++ lib/ansible/module_utils/cloudstack.py | 9 +++++++ 2 files changed, 46 insertions(+) diff --git a/docsite/rst/guide_cloudstack.rst b/docsite/rst/guide_cloudstack.rst index 0df2d8a4705..abdaafddc91 100644 --- a/docsite/rst/guide_cloudstack.rst +++ b/docsite/rst/guide_cloudstack.rst @@ -97,6 +97,43 @@ Or by looping over a regions list if you want to do the task in every region: - exmaple_cloud_one - exmaple_cloud_two +Environment Variables +````````````````````` +.. versionadded:: 2.3 + +Since Ansible 2.3 it is possible to use environment variables for domain (``CLOUDSTACK_DOMAIN``), account (``CLOUDSTACK_ACCOUNT``), project (``CLOUDSTACK_PROJECT``) and zone (``CLOUDSTACK_ZONE``). This simplifies the tasks by not repeating the arguments for every tasks. + +Below you see an example how it can be used in combination with Ansible's block feature: + +.. code-block:: yaml + + - hosts: cloud-vm + tasks: + - block: + - name: ensure my ssh public key + local_action: + module: cs_sshkeypair + name: my-ssh-key + public_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}" + + - name: ensure my ssh public key + local_action: + module: cs_instance: + display_name: "{{ inventory_hostname_short }}" + template: Linux Debian 7 64-bit 20GB Disk + service_offering: "{{ cs_offering }}" + ssh_key: my-ssh-key + state: running + + environment: + CLOUDSTACK_DOMAIN: root/customers + CLOUDSTACK_PROJECT: web-app + CLOUDSTACK_ZONE: sf-1 + +.. Note:: You are still able overwrite the environment variables using the module arguments, e.g.``zone: sf-2`` + +.. Note:: Unlike ``CLOUDSTACK_REGION`` these additional environment variables are ingored in the CLI ``cs``. + Use Cases ````````` The following should give you some ideas how to use the modules to provision VMs to the cloud. As always, there isn't only one way to do it. But as always: keep it simple for the beginning is always a good start. diff --git a/lib/ansible/module_utils/cloudstack.py b/lib/ansible/module_utils/cloudstack.py index 6e93a32d897..5962c64ce79 100644 --- a/lib/ansible/module_utils/cloudstack.py +++ b/lib/ansible/module_utils/cloudstack.py @@ -27,6 +27,7 @@ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +import os import time try: @@ -252,6 +253,8 @@ class AnsibleCloudStack(object): return self._get_by_key(key, self.project) project = self.module.params.get('project') + if not project: + project = os.environ.get('CLOUDSTACK_PROJECT') if not project: return None args = {} @@ -341,6 +344,8 @@ class AnsibleCloudStack(object): return self._get_by_key(key, self.zone) zone = self.module.params.get('zone') + if not zone: + zone = os.environ.get('CLOUDSTACK_ZONE') zones = self.cs.listZones() # use the first zone if no zone param given @@ -397,6 +402,8 @@ class AnsibleCloudStack(object): return self._get_by_key(key, self.account) account = self.module.params.get('account') + if not account: + account = os.environ.get('CLOUDSTACK_ACCOUNT') if not account: return None @@ -420,6 +427,8 @@ class AnsibleCloudStack(object): return self._get_by_key(key, self.domain) domain = self.module.params.get('domain') + if not domain: + domain = os.environ.get('CLOUDSTACK_DOMAIN') if not domain: return None