Skip to content

Commit 238d2e5

Browse files
authored
cs_instance_info: add host filter (#83)
1 parent d16c15f commit 238d2e5

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- cs_instance_info - implemented support for ``host`` filter (https://github.com/ngine-io/ansible-collection-cloudstack/pull/83).

plugins/modules/cs_instance_info.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
description:
3636
- Project the instance is related to.
3737
type: str
38+
host:
39+
description:
40+
- Filter by host name.
41+
type: str
42+
version_added: 2.2.0
3843
extends_documentation_fragment:
3944
- ngine_io.cloudstack.cloudstack
4045
'''
@@ -56,6 +61,11 @@
5661
- name: Show information on all instances
5762
debug:
5863
msg: "{{ vms }}"
64+
65+
- name: Gather information from all instances on a host
66+
ngine_io.cloudstack.cs_instance_info:
67+
host: host01.example.com
68+
register: vms
5969
'''
6070

6171
RETURN = '''
@@ -279,13 +289,29 @@ def __init__(self, module):
279289
'hostname': 'host',
280290
}
281291

292+
def get_host(self, key=None):
293+
host = self.module.params.get('host')
294+
if not host:
295+
return
296+
297+
args = {
298+
'fetch_list': True,
299+
}
300+
res = self.query_api('listHosts', **args)
301+
if res:
302+
for h in res:
303+
if host.lower() in [h['id'], h['ipaddress'], h['name'].lower()]:
304+
return self._get_by_key(key, h)
305+
self.fail_json(msg="Host not found: %s" % host)
306+
282307
def get_instances(self):
283308
instance_name = self.module.params.get('name')
284309

285310
args = {
286311
'account': self.get_account(key='name'),
287312
'domainid': self.get_domain(key='id'),
288313
'projectid': self.get_project(key='id'),
314+
'hostid': self.get_host(key='id'),
289315
'fetch_list': True,
290316
}
291317
# Do not pass zoneid, as the instance name must be unique across zones.
@@ -354,6 +380,7 @@ def main():
354380
domain=dict(),
355381
account=dict(),
356382
project=dict(),
383+
host=dict(),
357384
))
358385

359386
module = AnsibleModule(

tests/integration/targets/cs_instance_info/tasks/main.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,16 @@
7878
- '"name" in instance_info.instances[0]'
7979
- '"service_offering" in instance_info.instances[0]'
8080
- '"host" in instance_info.instances[0]'
81+
82+
- name: remember host
83+
set_fact:
84+
host: "{{ instance_info.instances[0]['host']}}"
85+
86+
- name: test instance info for all instances of a host
87+
cs_instance_info:
88+
host: "{{ host }}"
89+
register: instance_info
90+
- name: verify test instance info
91+
assert:
92+
that:
93+
- instance_info.instances[0]['host'] == host

0 commit comments

Comments
 (0)