Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cloudstack: add additional env vars #17946

Merged
merged 1 commit into from
Oct 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions docsite/rst/guide_cloudstack.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions lib/ansible/module_utils/cloudstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -252,6 +253,8 @@ def get_project(self, key=None):
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 = {}
Expand Down Expand Up @@ -341,6 +344,8 @@ def get_zone(self, key=None):
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
Expand Down Expand Up @@ -397,6 +402,8 @@ def get_account(self, key=None):
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

Expand All @@ -420,6 +427,8 @@ def get_domain(self, key=None):
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

Expand Down