kayobe/ansible/baremetal-compute-rename.yml
Mark Goddard 21ce37e698 Fixes for baremetal compute commands
Gather facts prior to using the stackhpc.os-shade role. This is required
following a recent change in that role to reference the ansible_user_uid and
ansible_user_gid facts.

Allow 'kayobe baremetal compute rename' command to ignore compute nodes in the
ansible inventory but not found in ironic.

Change-Id: I75a2f0dd0192e41de7b91376bb32b4149df04a60
TrivialFix
2018-08-17 13:51:01 +01:00

62 lines
2.4 KiB
YAML

---
# 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 }}"
matching_nodes: "{{ (nodes.stdout | from_json) | selectattr('Driver Info.ipmi_address', 'defined') | selectattr('Driver Info.ipmi_address', 'equalto', ipmi_address) | list }}"
node: "{{ matching_nodes | first }}"
when:
- matching_nodes | length > 0
- node['Name'] != inventory_hostname