Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/lookup/vm_moid.py

raw
1 # Copyright: (c) 2021, Alina Buzachis <@alinabuzachis>
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4
5 from __future__ import absolute_import, division, print_function
6
7 __metaclass__ = type
8
9
10 DOCUMENTATION = r"""
11 name: vm_moid
12 short_description: Look up MoID for vSphere vm objects using vCenter REST API
13 description:
14 - Returns Managed Object Reference (MoID) of the vSphere vm object contained in the specified path.
15 author:
16 - Alina Buzachis (@alinabuzachis)
17 version_added: 2.1.0
18 requirements:
19 - vSphere 7.0.3 or greater
20 - python >= 3.6
21 - aiohttp
22 notes:
23 - This plugin requires the cloud.common collection, which will not be installed automatically as a dependency.
24 extends_documentation_fragment:
25 - vmware.vmware_rest.moid
26 """
27
28
29 EXAMPLES = r"""
30 #
31 #
32 # The examples below assume you have a datacenter named 'my_dc' and a vm named 'my_vm'.
33 # Replace these values as needed for your environment.
34 #
35 #
36
37 #
38 # Authentication / Connection Arguments
39 #
40 # You can explicitly set the connection arguments in each lookup. This may be clearer for some use cases
41 - name: Pass In Connection Arguments Explicitly
42 ansible.builtin.debug:
43 msg: >-
44 {{ lookup('vmware.vmware_rest.vm_moid', '/my_dc/vm/my_cluster/my_vm',
45 vcenter_hostname="vcenter.test",
46 vcenter_username="[email protected]",
47 vcenter_password="1234") }}
48
49 # Alternatively, you can add the connection arguments to a dictionary variable, and then pass that variable to the
50 # lookup plugins. This makes the individual lookup plugin calls simpler
51 - name: Example Playbook
52 hosts: all
53 vars:
54 connection_args:
55 vcenter_hostname: "vcenter.test"
56 vcenter_username: "[email protected]"
57 vcenter_password: "1234"
58 tasks:
59 # Add more tasks or lookups as needed, referencing the same connection_args variable
60 - name: Lookup MoID of the object
61 ansible.builtin.debug:
62 msg: "{{ lookup('vmware.vmware_rest.vm_moid', '/my_dc/vm/my_cluster/my_vm', **connection_args) }}"
63
64 # Finally, you can also leverage the environment variables associated with each connection arg, and avoid passing
65 # extra args to the lookup plugins
66 - name: Use a lookup plugin with VMWARE_* environment variables set
67 ansible.builtin.debug:
68 msg: "{{ lookup('vmware.vmware_rest.vm_moid', '/my_dc/vm/my_cluster/my_vm') }}"
69
70 #
71 # VM Search Path Examples
72 #
73 # VMs are located under the 'vm' folder in a datacenter. If they are not in a folder, the path
74 # should include the cluster name.
75 # The basic path for a VM should look like '/<datacenter-name>/vm/<cluster-name>/<vm-name>'
76 - name: Lookup VM Named 'my_vm' in Datacenter 'my_dc' in Cluster 'my_cluster'
77 ansible.builtin.debug:
78 msg: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/vm/my_cluster/my_vm') }}"
79
80 # If the VM is in a user created VM folder, you just include the folder name.
81 - name: Lookup VM Named 'my_vm' in Datacenter 'my_dc' in Folder 'production' (also in Cluster 'my_cluster')
82 ansible.builtin.debug:
83 msg: "{{ lookup('vmware.vmware_rest.folder_moid', '/my_dc/production/my_vm') }}"
84
85 #
86 # Usage in Playbooks
87 #
88 #
89 # The lookup plugin can be used to simplify your playbook. Here is an example of how you might use it.
90 #
91 # Without the lookup, this takes two modules which both run on the remote host. This can slow down execution
92 # and adds extra steps to the playbook:
93 - name: Look up the VM called 'my_vm' in the inventory
94 vmware.vmware_rest.vcenter_vm_info:
95 filter_names:
96 - my_vm
97 register: my_vm_info
98
99 - name: Delete a VM
100 vmware.vmware_rest.vcenter_vm:
101 vm: '{{ my_vm_info.value[0].vm }}'
102 state: absent
103
104 # With the lookup, playbooks are shorter, quicker, and more intuitive:
105 - name: Delete a VM
106 vmware.vmware_rest.vcenter_vm:
107 vm: "{{ lookup('vmware.vmware_rest.vm_moid', '/my_dc/vm/my_vm') }}"
108 state: absent
109 """
110
111
112 RETURN = r"""
113 _raw:
114 description: MoID of the vSphere vm object
115 type: str
116 sample: vm-1026
117 """
118
119
120 from ansible.errors import AnsiblePluginError
121
122 try:
123 from ansible_collections.cloud.common.plugins.plugin_utils.turbo.lookup import (
124 TurboLookupBase as LookupBase,
125 )
126 except ImportError:
127 raise AnsiblePluginError(
128 message="This plugin requires the cloud.common collection."
129 )
130
131 from ansible_collections.vmware.vmware_rest.plugins.plugin_utils.lookup import Lookup
132
133
134 class LookupModule(LookupBase):
135 async def _run(self, terms, variables, **kwargs):
136 self.set_options(var_options=variables, direct=kwargs)
137 self.set_option("object_type", "vm")
138 result = await Lookup.entry_point(terms, self._options)
139 return [result]
140
141 run = _run if not hasattr(LookupBase, "run_on_daemon") else LookupBase.run_on_daemon