Adds new command to rename baremetal compute nodes

Story: 2002176
Task: 20042
Change-Id: I2174a4719aaff63fff24e35ce62eab57b369b457
This commit is contained in:
Will Szumski 2018-06-07 11:50:38 +01:00
parent f034ae4741
commit 05dfcc8199
6 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,58 @@
---
# This playbook will ensure that all baremetal compute nodes are named after
# their inventory host names. It matches the ipmi address in the inventory to
# the one gathered from inspection
- name: Rename baremetal compute nodes
hosts: controllers[0]
gather_facts: False
vars:
venv: "{{ virtualenv_path }}/openstack-cli"
pre_tasks:
- name: Set up openstack cli virtualenv
pip:
virtualenv: "{{ venv }}"
name:
- python-openstackclient
- python-ironicclient
- name: Rename baremetal compute nodes
hosts: baremetal-compute
gather_facts: False
vars:
venv: "{{ virtualenv_path }}/openstack-cli"
controller_host: "{{ groups['controllers'][0] }}"
tasks:
- name: Fail if ipmi host variable not set
vars:
ipmi_address: "{{ hostvars[inventory_hostname].ipmi_address }}"
fail:
msg: >
The host variable, ipmi_address is not defined for {{ inventory_hostname }}. This variable is required
to run this playbook.
when: ipmi_address is not defined or not ipmi_address
- name: Get list of nodes
command: >
{{ venv }}/bin/openstack baremetal node list -f json --fields uuid name driver_info
register: nodes
delegate_to: "{{ controller_host }}"
environment: "{{ openstack_auth_env }}"
run_once: true
changed_when: false
vars:
# NOTE: Without this, the controller's ansible_host variable will not
# be respected when using delegate_to.
ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}"
- name: Rename baremetal compute nodes
command: >
{{ venv }}/bin/openstack baremetal node set --name "{{ inventory_hostname }}" "{{ node['UUID'] }}"
delegate_to: "{{ controller_host }}"
environment: "{{ openstack_auth_env }}"
vars:
# NOTE: Without this, the controller's ansible_host variable will not
# be respected when using delegate_to.
ansible_host: "{{ hostvars[controller_host].ansible_host | default(controller_host) }}"
ipmi_address: "{{ hostvars[inventory_hostname].ipmi_address }}"
node: "{{ (nodes.stdout | from_json) | selectattr('Driver Info.ipmi_address', 'equalto', ipmi_address) | first }}"
when: node['Name'] != inventory_hostname

View File

@ -169,6 +169,18 @@ compute nodes::
(kayobe) $ kayobe baremetal compute inspect
Rename
------
Once nodes have been discovered, it is helpful to associate them with a name
to make them easier to work with. If you would like the nodes to be named
according to their inventory host names, you can run the following command:
(kayobe) $ kayobe baremetal compute rename
This command will use the ``ipmi_address`` host variable from the inventory
to map the inventory host name to the correct node.
Running Kayobe Playbooks on Demand
==================================

View File

@ -1137,3 +1137,12 @@ class BaremetalComputeProvide(KayobeAnsibleMixin, VaultMixin, Command):
self.app.LOG.debug("Making baremetal compute nodes available")
playbooks = _build_playbook_list("baremetal-compute-provide")
self.run_kayobe_playbooks(parsed_args, playbooks)
class BaremetalComputeRename(KayobeAnsibleMixin, VaultMixin, Command):
"""Rename baremetal compute nodes to match inventory hostname"""
def take_action(self, parsed_args):
self.app.LOG.debug("Renaming baremetal compute nodes")
playbooks = _build_playbook_list("baremetal-compute-rename")
self.run_kayobe_playbooks(parsed_args, playbooks)

View File

@ -749,3 +749,21 @@ class TestCase(unittest.TestCase):
),
]
self.assertEqual(expected_calls, mock_run.call_args_list)
@mock.patch.object(commands.KayobeAnsibleMixin,
"run_kayobe_playbooks")
def test_baremetal_compute_rename(self, mock_run):
command = commands.BaremetalComputeRename(TestApp(), [])
parser = command.get_parser("test")
parsed_args = parser.parse_args([])
result = command.run(parsed_args)
self.assertEqual(0, result)
expected_calls = [
mock.call(
mock.ANY,
[
"ansible/baremetal-compute-rename.yml",
],
),
]
self.assertEqual(expected_calls, mock_run.call_args_list)

View File

@ -0,0 +1,5 @@
---
features:
- |
Adds a command to rename baremetal compute nodes to match
their inventory host name - ``kayobe baremetal compute rename``

View File

@ -32,6 +32,7 @@ kayobe.cli=
baremetal_compute_inspect = kayobe.cli.commands:BaremetalComputeInspect
baremetal_compute_manage = kayobe.cli.commands:BaremetalComputeManage
baremetal_compute_provide = kayobe.cli.commands:BaremetalComputeProvide
baremetal_compute_rename = kayobe.cli.commands:BaremetalComputeRename
control_host_bootstrap = kayobe.cli.commands:ControlHostBootstrap
control_host_upgrade = kayobe.cli.commands:ControlHostUpgrade
configuration_dump = kayobe.cli.commands:ConfigurationDump