mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-08 11:40:32 -07:00
[PR #8907/8df9d0d7 backport][stable-9] one_host: Fix ID logic (#9014)
one_host: Fix ID logic (#8907)
* Fix one_host module
* Add CHANGELOG fragment
* PR Fixes
* Update exceptions
(cherry picked from commit 8df9d0d7de
)
Co-authored-by: alexander <79072457+abakanovskii@users.noreply.github.com>
This commit is contained in:
parent
21a840eab7
commit
280a5b0e61
2 changed files with 38 additions and 23 deletions
2
changelogs/fragments/8907-fix-one-host-id.yml
Normal file
2
changelogs/fragments/8907-fix-one-host-id.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- one_host - fix if statements for cases when ``ID=0`` (https://github.com/ansible-collections/community.general/issues/1199, https://github.com/ansible-collections/community.general/pull/8907).
|
|
@ -152,16 +152,19 @@ class HostModule(OpenNebulaModule):
|
||||||
def allocate_host(self):
|
def allocate_host(self):
|
||||||
"""
|
"""
|
||||||
Creates a host entry in OpenNebula
|
Creates a host entry in OpenNebula
|
||||||
|
self.one.host.allocate returns ID of a host
|
||||||
Returns: True on success, fails otherwise.
|
Returns: True on success, fails otherwise.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not self.one.host.allocate(self.get_parameter('name'),
|
try:
|
||||||
self.get_parameter('vmm_mad_name'),
|
self.one.host.allocate(self.get_parameter('name'),
|
||||||
self.get_parameter('im_mad_name'),
|
self.get_parameter('vmm_mad_name'),
|
||||||
self.get_parameter('cluster_id')):
|
self.get_parameter('im_mad_name'),
|
||||||
self.fail(msg="could not allocate host")
|
self.get_parameter('cluster_id'))
|
||||||
else:
|
|
||||||
self.result['changed'] = True
|
self.result['changed'] = True
|
||||||
|
except Exception as e:
|
||||||
|
self.fail(msg="Could not allocate host, ERROR: " + str(e))
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def wait_for_host_state(self, host, target_states):
|
def wait_for_host_state(self, host, target_states):
|
||||||
|
@ -221,11 +224,13 @@ class HostModule(OpenNebulaModule):
|
||||||
if current_state == HOST_ABSENT:
|
if current_state == HOST_ABSENT:
|
||||||
self.fail(msg='absent host cannot be put in disabled state')
|
self.fail(msg='absent host cannot be put in disabled state')
|
||||||
elif current_state in [HOST_STATES.MONITORED, HOST_STATES.OFFLINE]:
|
elif current_state in [HOST_STATES.MONITORED, HOST_STATES.OFFLINE]:
|
||||||
if one.host.status(host.ID, HOST_STATUS.DISABLED):
|
# returns host ID integer
|
||||||
self.wait_for_host_state(host, [HOST_STATES.DISABLED])
|
try:
|
||||||
|
one.host.status(host.ID, HOST_STATUS.DISABLED)
|
||||||
result['changed'] = True
|
result['changed'] = True
|
||||||
else:
|
except Exception as e:
|
||||||
self.fail(msg="could not disable host")
|
self.fail(msg="Could not disable host, ERROR: " + str(e))
|
||||||
|
self.wait_for_host_state(host, [HOST_STATES.DISABLED])
|
||||||
elif current_state in [HOST_STATES.DISABLED]:
|
elif current_state in [HOST_STATES.DISABLED]:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
@ -235,11 +240,13 @@ class HostModule(OpenNebulaModule):
|
||||||
if current_state == HOST_ABSENT:
|
if current_state == HOST_ABSENT:
|
||||||
self.fail(msg='absent host cannot be placed in offline state')
|
self.fail(msg='absent host cannot be placed in offline state')
|
||||||
elif current_state in [HOST_STATES.MONITORED, HOST_STATES.DISABLED]:
|
elif current_state in [HOST_STATES.MONITORED, HOST_STATES.DISABLED]:
|
||||||
if one.host.status(host.ID, HOST_STATUS.OFFLINE):
|
# returns host ID integer
|
||||||
self.wait_for_host_state(host, [HOST_STATES.OFFLINE])
|
try:
|
||||||
|
one.host.status(host.ID, HOST_STATUS.OFFLINE)
|
||||||
result['changed'] = True
|
result['changed'] = True
|
||||||
else:
|
except Exception as e:
|
||||||
self.fail(msg="could not set host offline")
|
self.fail(msg="Could not set host offline, ERROR: " + str(e))
|
||||||
|
self.wait_for_host_state(host, [HOST_STATES.OFFLINE])
|
||||||
elif current_state in [HOST_STATES.OFFLINE]:
|
elif current_state in [HOST_STATES.OFFLINE]:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
@ -247,10 +254,12 @@ class HostModule(OpenNebulaModule):
|
||||||
|
|
||||||
elif desired_state == 'absent':
|
elif desired_state == 'absent':
|
||||||
if current_state != HOST_ABSENT:
|
if current_state != HOST_ABSENT:
|
||||||
if one.host.delete(host.ID):
|
# returns host ID integer
|
||||||
|
try:
|
||||||
|
one.host.delete(host.ID)
|
||||||
result['changed'] = True
|
result['changed'] = True
|
||||||
else:
|
except Exception as e:
|
||||||
self.fail(msg="could not delete host from cluster")
|
self.fail(msg="Could not delete host from cluster, ERROR: " + str(e))
|
||||||
|
|
||||||
# if we reach this point we can assume that the host was taken to the desired state
|
# if we reach this point we can assume that the host was taken to the desired state
|
||||||
|
|
||||||
|
@ -268,17 +277,21 @@ class HostModule(OpenNebulaModule):
|
||||||
if self.requires_template_update(host.TEMPLATE, desired_template_changes):
|
if self.requires_template_update(host.TEMPLATE, desired_template_changes):
|
||||||
# setup the root element so that pyone will generate XML instead of attribute vector
|
# setup the root element so that pyone will generate XML instead of attribute vector
|
||||||
desired_template_changes = {"TEMPLATE": desired_template_changes}
|
desired_template_changes = {"TEMPLATE": desired_template_changes}
|
||||||
if one.host.update(host.ID, desired_template_changes, 1): # merge the template
|
# merge the template, returns host ID integer
|
||||||
|
try:
|
||||||
|
one.host.update(host.ID, desired_template_changes, 1)
|
||||||
result['changed'] = True
|
result['changed'] = True
|
||||||
else:
|
except Exception as e:
|
||||||
self.fail(msg="failed to update the host template")
|
self.fail(msg="Failed to update the host template, ERROR: " + str(e))
|
||||||
|
|
||||||
# the cluster
|
# the cluster
|
||||||
if host.CLUSTER_ID != self.get_parameter('cluster_id'):
|
if host.CLUSTER_ID != self.get_parameter('cluster_id'):
|
||||||
if one.cluster.addhost(self.get_parameter('cluster_id'), host.ID):
|
# returns cluster id in int
|
||||||
|
try:
|
||||||
|
one.cluster.addhost(self.get_parameter('cluster_id'), host.ID)
|
||||||
result['changed'] = True
|
result['changed'] = True
|
||||||
else:
|
except Exception as e:
|
||||||
self.fail(msg="failed to update the host cluster")
|
self.fail(msg="Failed to update the host cluster, ERROR: " + str(e))
|
||||||
|
|
||||||
# return
|
# return
|
||||||
self.exit()
|
self.exit()
|
||||||
|
|
Loading…
Add table
Reference in a new issue