Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_info
16 short_description: Gather information about Intersight
17 description:
18 - Gathers information about servers in L(Cisco Intersight,https://intersight.com).
19 - This module was called C(intersight_facts) before Ansible 2.9. The usage did not change.
20 extends_documentation_fragment: intersight
21 options:
22 server_names:
23 description:
24 - Server names to retrieve information from.
25 - An empty list will return all servers.
26 type: list
27 elements: str
28 author:
29 - David Soper (@dsoper2)
30 - CiscoUcs (@CiscoUcs)
31 '''
32
33 EXAMPLES = r'''
34 - name: Get info for all servers
35 intersight_info:
36 api_private_key: ~/Downloads/SecretKey.txt
37 api_key_id: 64612d300d0982/64612d300d0b00/64612d300d3650
38 server_names:
39 - debug:
40 msg: "server name {{ item.Name }}, moid {{ item.Moid }}"
41 loop: "{{ intersight_servers }}"
42 when: intersight_servers is defined
43
44 - name: Get info for servers by name
45 intersight_info:
46 api_private_key: ~/Downloads/SecretKey.txt
47 api_key_id: 64612d300d0982/64612d300d0b00/64612d300d3650
48 server_names:
49 - SJC18-L14-UCS1-1
50 - debug:
51 msg: "server moid {{ intersight_servers[0].Moid }}"
52 when: intersight_servers[0] is defined
53 '''
54
55 RETURN = r'''
56 intersight_servers:
57 description: A list of Intersight Servers. See L(Cisco Intersight,https://intersight.com/apidocs) for details.
58 returned: always
59 type: complex
60 contains:
61 Name:
62 description: The name of the server.
63 returned: always
64 type: str
65 sample: SJC18-L14-UCS1-1
66 Moid:
67 description: The unique identifier of this Managed Object instance.
68 returned: always
69 type: str
70 sample: 5978bea36ad4b000018d63dc
71 '''
72
73 from ansible.module_utils.basic import AnsibleModule
74 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
75
76
77 def get_servers(module, intersight):
78 query_list = []
79 if module.params['server_names']:
80 for server in module.params['server_names']:
81 query_list.append("Name eq '%s'" % server)
82 query_str = ' or '.join(query_list)
83 options = {
84 'http_method': 'get',
85 'resource_path': '/compute/PhysicalSummaries',
86 'query_params': {
87 '$filter': query_str,
88 '$top': 1000,
89 }
90 }
91 response_dict = intersight.call_api(**options)
92
93 return response_dict.get('Results')
94
95
96 def main():
97 argument_spec = intersight_argument_spec.copy()
98 argument_spec.update(
99 server_names=dict(type='list', elements='str'),
100 )
101
102 module = AnsibleModule(
103 argument_spec,
104 supports_check_mode=True,
105 )
106
107 intersight = IntersightModule(module)
108
109 # one API call returning all requested servers
110 module.exit_json(intersight_servers=get_servers(module, intersight))
111
112
113 if __name__ == '__main__':
114 main()