Skip to content

Instantly share code, notes, and snippets.

@patrickmslatteryvt
Last active December 2, 2016 05:49
Show Gist options
  • Save patrickmslatteryvt/8526b878e870a38643bd897e2a1f5b70 to your computer and use it in GitHub Desktop.
Save patrickmslatteryvt/8526b878e870a38643bd897e2a1f5b70 to your computer and use it in GitHub Desktop.

Sample of how to use lists to select from multiple predefined choices in Ansible

playbooks/rackspace.yaml

---
- name: "Provision system(s) in Rackspace"
  hosts: localhost
  connection: local
  gather_facts: yes

  vars_prompt:
    - name: "rax_flavor"
      prompt: >
        What flavor server should be provisioned?
          1. 1 GB (general1-1)
          2. 15 GB (memory1-15)
          3. 15 GB (compute1-15)
          4. 30 GB (memory1-30)
          5. 30 GB (compute1-30)
        Please choose the desired flavor. Press enter for default (#1 - 1GB) flavor.
      default: "1"
      private: no

    - name: "provision_cinder_volume"
      prompt: "\nProvision a Cinder data volume? Press enter for default (true).\n"
      private: no
      default: "true"

    - name: "rax_cbs_volume_type"
      prompt: >
        What type of disk do you want to provision?
          1. SATA
          2. SSD
        Please choose the disk type. Press enter for default (SATA).
      default: "1"
      private: no

    - name: "rax_cbs_size"
      prompt: >
        What size disk do you want to provision?
          1. 50GB (Minimum SSD disk size)
          2. 75GB (Minimum SATA disk size)
          3. 100GB
          4. 150GB
          5. 200GB
        Please choose the disk size. Press enter for default (75GB).
      default: "2"
      private: no

# <snip>

  roles:
    - rackspace-provision

playbooks/roles/rackspace-provision/vars/main.yaml

---
# https://developer.rackspace.com/docs/cloud-servers/v2/general-api-info/flavors/
flavors:
  "1": "general1-1"
  "2": "memory1-15"
  "3": "compute1-15"
  "4": "memory1-30"
  "5": "compute1-30"

volume_type:
  "1": "SATA"
  "2": "SSD"

volume_size:
  "1": "50"
  "2": "75"
  "3": "100"
  "4": "150"
  "4": "200"

playbooks/roles/rackspace-provision/tasks/provision-cinder-volume.yaml

- name: "Create Cinder volume"
  local_action:
    module: rax_cbs
    username: "{{ rax_username }}"
    tenant_name: "{{ rax_tenant_name }}"
    api_key: "{{ rax_api_key }}"
    region: "{{ rax_region }}"
    # image: "{{ rax_image }}"  # image to use for bootable volumes
    # mwgdlqspaceu01_ZFS_mirror-0_xvdd
    name: "{{ rax_name }}_ZFS_basic-0_{{ rax_cbs_device }}"
    description: "{{ rax_name }}_ZFS_basic-0_{{ rax_cbs_device }}"
    volume_type: "{{ volume_type[rax_cbs_volume_type] }}"
    size: "{{ volume_size[rax_cbs_size] }}"
    wait: yes
    state: present
  register: create_cinder_vol

- name: "Attach Cinder volume"
  local_action:
    module: rax_cbs_attachments
    username: "{{ rax_username }}"
    tenant_name: "{{ rax_tenant_name }}"
    api_key: "{{ rax_api_key }}"
    region: "{{ rax_region }}"
    volume: "{{ rax_name }}_ZFS_basic-0_{{ rax_cbs_device }}"
    server: "{{ rax_name }}"
    device: "/dev/{{ rax_cbs_device }}"
    wait: yes
    state: present
  register: attach_cinder_vol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment