Ansible Collections

We briefly touched on ansible galaxy collections, but here we’ll get into a little more detail. We’ll install a collection using a workflow similar to being in a disconnected environment. Take a look at collections/requirements.yml in the top of the workshop directory. This defines a collection that we want to install.

Download the collection.

ansible-galaxy collection download -r ../requirements.yml

This creates a local tar file based on the contents of the requirement.yml file in the collections directory. This step would be performed on an Internet facing systems and the results would be transferred to the disconnected environment.

Now we’ll view the download, install the collection and validate it’s installed.

ls collections
ansible-galaxy collection install collections/nutanix-ncp-2.5.0.tar.gz
ansible-galaxy collection list | grep nutanix

View the available documentation for all of the Nutanix modules included in the collection.

ansible-doc -l | grep nutanix

Next view the documentation for creating a Nutanix VM.

ansible-doc nutanix.ncp.ntnx_vms

Using the Nutanix collection would require that you have access to the credentials to hit the Nutanix API, which is beyond the scope of this workshop. However, let’s review the vm.yml playbook.

---
- name: playbook to create VM
  hosts: localhost
  gather_facts: false
  collections:
    - nutanix.ncp
  module_defaults:
    group/nutanix.ncp.ntnx:
      nutanix_host: "{{ vault_pc_host }}"
      nutanix_username: "{{ vault_pc_username }}"
      nutanix_password: "{{ vault_pc_password }}"
      validate_certs: false
  tasks:
  - name: setting variables
    set_fact:
      SCRIPT_PATH: "cloud-init-httpd"
      IMAGE_NAME: "RHEL-9.7"
      VM_NAME: "httpd"

  - name: create VM
    ntnx_vms:
      state: present
      name: "{{ VM_NAME }}"
      desc: "httpd web server"
      cluster:
        name: "{{ CLUSTER_NAME }}"
      networks:
        - is_connected: True
          subnet:
            name: "{{ SUBNET_NAME }}"
      disks:
        - type: "DISK"
          size_gb: 30
          clone_image:
            name: "{{ IMAGE_NAME }}"
      vcpus: 1
      cores_per_vcpu: 1
      memory_gb: 2
      guest_customization:
        type: "cloud_init"
        script_path: "{{ SCRIPT_PATH }}"
        is_overridable: False
    register: result

  - name: result of VM created
    debug:
      msg:
        - "VM Name: '{{ result.response.spec.name }}'"
        - "VM IP Address: '{{ result.response.spec.resources.nic_list[0].ip_endpoint_list[0].ip }}'"
        - "VM UUID: '{{ result.response.metadata.uuid}}'"
  • lines 5 and 6 make the collection modules available in the playbook.
  • lines 7 - 12 set the variables for API access. This would be held in a vault encrypted file for sure.
  • the first task sets some variables that will be used in the playbook
  • the second task creates the VM defining characteristics such as image, CPU, etc.
  • the final module outputs some details about the VM that was just created