community.general.one_vm (8.5.0) — module

Creates or terminates OpenNebula instances

Authors: Milan Ilic (@ilicmilan), Jan Meerkamp (@meerkampdvv)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

Description

Manages OpenNebula instances


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create a new instance
  community.general.one_vm:
    template_id: 90
  register: result
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Print VM properties
  ansible.builtin.debug:
    msg: result
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Deploy a new VM on hold
  community.general.one_vm:
    template_name: 'app1_template'
    vm_start_on_hold: 'True'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Deploy a new VM and set its name to 'foo'
  community.general.one_vm:
    template_name: 'app1_template'
    attributes:
      name: foo
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Deploy a new VM and set its group_id and mode
  community.general.one_vm:
    template_id: 90
    group_id: 16
    mode: 660
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Deploy a new VM  as persistent
  community.general.one_vm:
    template_id: 90
    persistent: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Change VM's permissions to 640
  community.general.one_vm:
    instance_ids: 5
    mode: 640
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Deploy 2 new instances and set memory, vcpu, disk_size and 3 networks
  community.general.one_vm:
    template_id: 15
    disk_size: 35.2 GB
    memory: 4 GB
    vcpu: 4
    count: 2
    networks:
      - NETWORK_ID: 27
      - NETWORK: "default-network"
        NETWORK_UNAME: "app-user"
        SECURITY_GROUPS: "120,124"
      - NETWORK_ID: 27
        SECURITY_GROUPS: "10"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Deploy a new instance which uses a Template with two Disks
  community.general.one_vm:
    template_id: 42
    disk_size:
      - 35.2 GB
      - 50 GB
    memory: 4 GB
    vcpu: 4
    count: 1
    networks:
      - NETWORK_ID: 27
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "Deploy an new instance with attribute 'bar: bar1' and set its name to 'foo'"
  community.general.one_vm:
    template_id: 53
    attributes:
      name: foo
      bar: bar1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "Enforce that 2 instances with attributes 'foo1: app1' and 'foo2: app2' are deployed"
  community.general.one_vm:
    template_id: 53
    attributes:
      foo1: app1
      foo2: app2
    exact_count: 2
    count_attributes:
      foo1: app1
      foo2: app2
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Enforce that 4 instances with an attribute 'bar' are deployed
  community.general.one_vm:
    template_id: 53
    attributes:
      name: app
      bar: bar2
    exact_count: 4
    count_attributes:
      bar:
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Deploy 2 new instances with attribute 'foo: bar' and labels 'app1' and 'app2' and names in format 'fooapp-##'
# Names will be: fooapp-00 and fooapp-01
- name: Deploy 2 new instances
  community.general.one_vm:
    template_id: 53
    attributes:
      name: fooapp-##
      foo: bar
    labels:
      - app1
      - app2
    count: 2
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Deploy 2 new instances with attribute 'app: app1' and names in format 'fooapp-###'
# Names will be: fooapp-002 and fooapp-003
- name: Deploy 2 new instances
  community.general.one_vm:
    template_id: 53
    attributes:
      name: fooapp-###
      app: app1
    count: 2
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Reboot all instances with name in format 'fooapp-#'
# Instances 'fooapp-00', 'fooapp-01', 'fooapp-002' and 'fooapp-003' will be rebooted
- name: Reboot all instances with names in a certain format
  community.general.one_vm:
    attributes:
      name: fooapp-#
    state: rebooted
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Enforce that only 1 instance with name in format 'fooapp-#' is deployed
# The task will delete oldest instances, so only the 'fooapp-003' will remain
- name: Enforce that only 1 instance with name in a certain format is deployed
  community.general.one_vm:
    template_id: 53
    exact_count: 1
    count_attributes:
      name: fooapp-#
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Deploy an new instance with a network
  community.general.one_vm:
    template_id: 53
    networks:
      - NETWORK_ID: 27
  register: vm
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Wait for SSH to come up
  ansible.builtin.wait_for_connection:
  delegate_to: '{{ vm.instances[0].networks[0].ip }}'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Terminate VMs by ids
  community.general.one_vm:
    instance_ids:
      - 153
      - 160
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Reboot all VMs that have labels 'foo' and 'app1'
  community.general.one_vm:
    labels:
      - foo
      - app1
    state: rebooted
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "Fetch all VMs that have name 'foo' and attribute 'app: bar'"
  community.general.one_vm:
    attributes:
      name: foo
      app: bar
  register: results
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Deploy 2 new instances with labels 'foo1' and 'foo2'
  community.general.one_vm:
    template_name: app_template
    labels:
      - foo1
      - foo2
    count: 2
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Enforce that only 1 instance with label 'foo1' will be running
  community.general.one_vm:
    template_name: app_template
    labels:
      - foo1
    exact_count: 1
    count_labels:
      - foo1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Terminate all instances that have attribute foo
  community.general.one_vm:
    template_id: 53
    exact_count: 0
    count_attributes:
      foo:
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "Power-off the VM and save VM's disk with id=0 to the image with name 'foo-image'"
  community.general.one_vm:
    instance_ids: 351
    state: poweredoff
    disk_saveas:
      name: foo-image
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "Save VM's disk with id=1 to the image with name 'bar-image'"
  community.general.one_vm:
    instance_ids: 351
    disk_saveas:
      name: bar-image
      disk_id: 1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "Deploy 2 new instances with a custom 'start script'"
  community.general.one_vm:
    template_name: app_template
    count: 2
    updateconf:
      CONTEXT:
        START_SCRIPT: ip r r 169.254.16.86/32 dev eth0
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "Add a custom 'start script' to a running VM"
  community.general.one_vm:
    instance_ids: 351
    updateconf:
      CONTEXT:
        START_SCRIPT: ip r r 169.254.16.86/32 dev eth0
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: "Update SSH public keys inside the VM's context"
  community.general.one_vm:
    instance_ids: 351
    updateconf:
      CONTEXT:
        SSH_PUBLIC_KEY: |-
          ssh-rsa ...
          ssh-ed25519 ...

Inputs

    
cpu:
    description:
    - Percentage of CPU divided by 100 required for the new instance. Half a processor
      is written 0.5.
    type: float

hard:
    default: false
    description:
    - Reboot, power-off or terminate instances C(hard).
    type: bool

mode:
    description:
    - Set permission mode of the instance in octet format, for example V(0600) to give
      owner C(use) and C(manage) and nothing to group and others.
    type: str

vcpu:
    description:
    - Number of CPUs (cores) new VM will have.
    type: int

wait:
    default: true
    description:
    - Wait for the instance to reach its desired state before returning. Keep in mind
      if you are waiting for instance to be in running state it doesn't mean that you
      will be able to SSH on that machine only that boot process have started on that
      instance, see 'wait_for' example for details.
    type: bool

count:
    default: 1
    description:
    - Number of instances to launch
    type: int

state:
    choices:
    - present
    - absent
    - running
    - rebooted
    - poweredoff
    default: present
    description:
    - V(present) - create instances from a template specified with C(template_id)/C(template_name).
    - V(running) - run instances
    - V(poweredoff) - power-off instances
    - V(rebooted) - reboot instances
    - V(absent) - terminate instances
    type: str

labels:
    default: []
    description:
    - A list of labels to associate with new instances, or for setting C(state) of instances
      with these labels.
    elements: str
    type: list

memory:
    description:
    - The size of the memory for new instances (in MB, GB, ...)
    type: str

api_url:
    description:
    - URL of the OpenNebula RPC server.
    - It is recommended to use HTTPS so that the username/password are not transferred
      over the network unencrypted.
    - If not set then the value of the E(ONE_URL) environment variable is used.
    type: str

group_id:
    description:
    - ID of the group which will be set as the group of the instance
    type: int

networks:
    default: []
    description:
    - A list of dictionaries with network parameters. See examples for more details.
    elements: dict
    type: list

owner_id:
    description:
    - ID of the user which will be set as the owner of the instance
    type: int

disk_size:
    description:
    - The size of the disk created for new instances (in MB, GB, TB,...).
    - B(NOTE:) If The Template hats Multiple Disks the Order of the Sizes is matched against
      the order specified in O(template_id)/O(template_name).
    elements: str
    type: list

attributes:
    default: {}
    description:
    - A dictionary of key/value attributes to add to new instances, or for setting C(state)
      of instances with these attributes.
    - Keys are case insensitive and OpenNebula automatically converts them to upper case.
    - Be aware C(NAME) is a special attribute which sets the name of the VM when it's
      deployed.
    - C(#) character(s) can be appended to the C(NAME) and the module will automatically
      add indexes to the names of VMs.
    - For example':' C(NAME':' foo-###) would create VMs with names C(foo-000), C(foo-001),...
    - When used with O(count_attributes) and O(exact_count) the module will match the
      base name without the index part.
    type: dict

persistent:
    default: false
    description:
    - Create a private persistent copy of the template plus any image defined in DISK,
      and instantiate that copy.
    type: bool
    version_added: 0.2.0
    version_added_collection: community.general

updateconf:
    description:
    - When O(instance_ids) is provided, updates running VMs with the C(updateconf) API
      call.
    - When new VMs are being created, emulates the C(updateconf) API call via direct template
      merge.
    - Allows for complete modifications of the C(CONTEXT) attribute.
    type: dict
    version_added: 6.3.0
    version_added_collection: community.general

disk_saveas:
    description:
    - Creates an image from a VM disk.
    - It is a dictionary where you have to specify C(name) of the new image.
    - Optionally you can specify C(disk_id) of the disk you want to save. By default C(disk_id)
      is 0.
    - B(NOTE:) This operation will only be performed on the first VM (if more than one
      VM ID is passed) and the VM has to be in the C(poweredoff) state.
    - Also this operation will fail if an image with specified C(name) already exists.
    type: dict

exact_count:
    description:
    - Indicates how many instances that match O(count_attributes) and O(count_labels)
      parameters should be deployed. Instances are either created or terminated based
      on this value.
    - B(NOTE:) Instances with the least IDs will be terminated first.
    type: int

template_id:
    description:
    - ID of a VM template to use to create a new instance
    type: int

api_password:
    description:
    - Password of the user to login into OpenNebula RPC server. If not set then the value
      of the E(ONE_PASSWORD) environment variable is used. if both O(api_username) or
      O(api_password) are not set, then it will try authenticate with ONE auth file. Default
      path is "~/.one/one_auth".
    - Set environment variable E(ONE_AUTH) to override this path.
    type: str

api_username:
    description:
    - Name of the user to login into the OpenNebula RPC server. If not set then the value
      of the E(ONE_USERNAME) environment variable is used.
    type: str

count_labels:
    description:
    - A list of labels that can only be used with O(exact_count) to determine how many
      nodes based on a specific labels criteria should be deployed. This can be expressed
      in multiple ways and is shown in the EXAMPLES section.
    elements: str
    type: list

datastore_id:
    description:
    - Name of Datastore to use to create a new instance
    type: int
    version_added: 0.2.0
    version_added_collection: community.general

instance_ids:
    aliases:
    - ids
    description:
    - 'A list of instance ids used for states: V(absent), V(running), V(rebooted), V(poweredoff).'
    elements: int
    type: list

wait_timeout:
    default: 300
    description:
    - How long before wait gives up, in seconds
    type: int

template_name:
    description:
    - Name of VM template to use to create a new instance
    type: str

datastore_name:
    description:
    - Name of Datastore to use to create a new instance
    type: str
    version_added: 0.2.0
    version_added_collection: community.general

count_attributes:
    description:
    - A dictionary of key/value attributes that can only be used with O(exact_count) to
      determine how many nodes based on a specific attributes criteria should be deployed.
      This can be expressed in multiple ways and is shown in the EXAMPLES section.
    type: dict

vm_start_on_hold:
    default: false
    description:
    - Set to true to put vm on hold while creating
    type: bool

Outputs

instances:
  contains:
    attributes:
      description: A dictionary of key/values attributes that are associated with
        the instance
      sample:
        HYPERVISOR: kvm
        LOGO: images/logos/centos.png
        TE_GALAXY: bar
        USER_INPUTS: null
      type: dict
    cpu:
      description: Percentage of CPU divided by 100
      sample: 0.2
      type: float
    disk_size:
      description: The size of the disk in MB
      sample: 20480 MB
      type: str
    group_id:
      description: vm's group id
      sample: 1
      type: int
    group_name:
      description: vm's group name
      sample: one-users
      type: str
    labels:
      description: A list of string labels that are associated with the instance
      sample:
      - foo
      - spec-label
      type: list
    lcm_state:
      description: lcm state of an instance that is only relevant when the state is
        ACTIVE
      sample: RUNNING
      type: str
    memory:
      description: The size of the memory in MB
      sample: 4096 MB
      type: str
    mode:
      description: vm's mode
      returned: success
      sample: 660
      type: str
    networks:
      description: a list of dictionaries with info about IP, NAME, MAC, SECURITY_GROUPS
        for each NIC
      sample:
      - ip: 10.120.5.33
        mac: 02:00:0a:78:05:21
        name: default-test-private
        security_groups: 0,10
      - ip: 10.120.5.34
        mac: 02:00:0a:78:05:22
        name: default-test-private
        security_groups: '0'
      type: list
    owner_id:
      description: vm's owner id
      sample: 143
      type: int
    owner_name:
      description: vm's owner name
      sample: app-user
      type: str
    state:
      description: state of an instance
      sample: ACTIVE
      type: str
    template_id:
      description: vm's template id
      sample: 153
      type: int
    updateconf:
      description: A dictionary of key/values attributes that are set with the updateconf
        API call.
      sample:
        CONTEXT:
          SSH_PUBLIC_KEY: 'ssh-rsa ...

            ssh-ed25519 ...'
          START_SCRIPT: ip r r 169.254.16.86/32 dev eth0
        OS:
          ARCH: x86_64
      type: dict
      version_added: 6.3.0
      version_added_collection: community.general
    uptime_h:
      description: Uptime of the instance in hours
      sample: 35
      type: int
    vcpu:
      description: Number of CPUs (cores)
      sample: 2
      type: int
    vm_id:
      description: vm id
      sample: 153
      type: int
    vm_name:
      description: vm name
      sample: foo
      type: str
  description: a list of instances info whose state is changed or which are fetched
    with O(instance_ids) option.
  returned: success
  type: complex
instances_ids:
  description: a list of instances ids whose state is changed or which are fetched
    with O(instance_ids) option.
  returned: success
  sample:
  - 1234
  - 1235
  type: list
tagged_instances:
  contains:
    attributes:
      description: A dictionary of key/values attributes that are associated with
        the instance
      sample:
        HYPERVISOR: kvm
        LOGO: images/logos/centos.png
        TE_GALAXY: bar
        USER_INPUTS: null
      type: dict
    cpu:
      description: Percentage of CPU divided by 100
      sample: 0.2
      type: float
    disk_size:
      description: The size of the disk in MB
      sample:
      - 20480 MB
      - 10240 MB
      type: list
    group_id:
      description: vm's group id
      sample: 1
      type: int
    group_name:
      description: vm's group name
      sample: one-users
      type: str
    labels:
      description: A list of string labels that are associated with the instance
      sample:
      - foo
      - spec-label
      type: list
    lcm_state:
      description: lcm state of an instance that is only relevant when the state is
        ACTIVE
      sample: RUNNING
      type: str
    memory:
      description: The size of the memory in MB
      sample: 4096 MB
      type: str
    mode:
      description: vm's mode
      returned: success
      sample: 660
      type: str
    networks:
      description: a list of dictionaries with info about IP, NAME, MAC, SECURITY_GROUPS
        for each NIC
      sample:
      - ip: 10.120.5.33
        mac: 02:00:0a:78:05:21
        name: default-test-private
        security_groups: 0,10
      - ip: 10.120.5.34
        mac: 02:00:0a:78:05:22
        name: default-test-private
        security_groups: '0'
      type: list
    owner_id:
      description: vm's user id
      sample: 143
      type: int
    owner_name:
      description: vm's user name
      sample: app-user
      type: str
    state:
      description: state of an instance
      sample: ACTIVE
      type: str
    template_id:
      description: vm's template id
      sample: 153
      type: int
    updateconf:
      description: A dictionary of key/values attributes that are set with the updateconf
        API call
      sample:
        CONTEXT:
          SSH_PUBLIC_KEY: 'ssh-rsa ...

            ssh-ed25519 ...'
          START_SCRIPT: ip r r 169.254.16.86/32 dev eth0
        OS:
          ARCH: x86_64
      type: dict
      version_added: 6.3.0
      version_added_collection: community.general
    uptime_h:
      description: Uptime of the instance in hours
      sample: 35
      type: int
    vcpu:
      description: Number of CPUs (cores)
      sample: 2
      type: int
    vm_id:
      description: vm id
      sample: 153
      type: int
    vm_name:
      description: vm name
      sample: foo
      type: str
  description:
  - A list of instances info based on a specific attributes and/or
  - labels that are specified with O(count_attributes) and O(count_labels)
  - options.
  returned: success
  type: complex