Move TripleO ansible inventory file to .ansible/inventory folder

Change-Id: I2ac2615e7c18e9feb1214d19d0f862bbf32999ee
This commit is contained in:
Federico Ressi 2022-05-25 14:42:52 +02:00
parent 6791976345
commit b3fbd45f5a
7 changed files with 38 additions and 41 deletions

4
.gitignore vendored
View File

@ -45,8 +45,8 @@ tobiko.conf
.infrared .infrared
workspace.tgz workspace.tgz
# Tripleo files # Ansible files
tripleo-hosts.yaml .ansible/
# Report files # Report files
tobiko_results* tobiko_results*

View File

@ -98,7 +98,7 @@ class AnsiblePlaybook(tobiko.SharedFixture):
-> typing.List[str]: -> typing.List[str]:
filenames = list(inventory_filenames) filenames = list(inventory_filenames)
filenames.extend(self._inventory_filenames) filenames.extend(self._inventory_filenames)
filenames.extend(tobiko.tobiko_config().ansible.inventory) filenames.extend(tobiko.tobiko_config().ansible.inventory_files)
existing_filenames = [] existing_filenames = []
for filename in sorted(filenames): for filename in sorted(filenames):
filename = tobiko.tobiko_config_path(filename) filename = tobiko.tobiko_config_path(filename)

View File

@ -21,7 +21,7 @@ from oslo_config import cfg
GROUP_NAME = 'ansible' GROUP_NAME = 'ansible'
OPTIONS = [ OPTIONS = [
cfg.ListOpt('inventory', cfg.ListOpt('inventory_files',
default=['/etc/ansible/hosts'], default=['/etc/ansible/hosts'],
help="Default Ansible inventory files"), help="Default Ansible inventory files"),
] ]

View File

@ -23,10 +23,9 @@ from tobiko import tripleo
PLAYBOOK_DIRNAME = os.path.join(os.path.dirname(__file__), 'playbooks') PLAYBOOK_DIRNAME = os.path.join(os.path.dirname(__file__), 'playbooks')
@tripleo.skip_if_missing_undercloud @tripleo.skip_if_missing_tripleo_ansible_inventory
class OpenShiftTest(testtools.TestCase): class OpenShiftTest(testtools.TestCase):
@tripleo.skip_if_missing_tripleo_ansible_inventory
def test_ping_all_hosts(self): def test_ping_all_hosts(self):
tripleo.run_playbook_from_undercloud( tripleo.run_playbook_from_undercloud(
playbook='ping-shiftstack.yaml', playbook='ping-shiftstack.yaml',

View File

@ -13,20 +13,20 @@
# under the License. # under the License.
from __future__ import absolute_import from __future__ import absolute_import
from tobiko.tripleo import _ansible as ansible from tobiko.tripleo import _ansible
from tobiko.tripleo import _overcloud as overcloud from tobiko.tripleo import _overcloud as overcloud
from tobiko.tripleo import _topology as topology from tobiko.tripleo import _topology as topology
from tobiko.tripleo import _undercloud as undercloud from tobiko.tripleo import _undercloud as undercloud
get_tripleo_ansible_inventory = ansible.get_tripleo_ansible_inventory get_tripleo_ansible_inventory = _ansible.get_tripleo_ansible_inventory
get_tripleo_ansible_inventory_file = \ get_tripleo_ansible_inventory_file = \
ansible.get_tripleo_ansible_inventory_file _ansible.get_tripleo_ansible_inventory_file
has_tripleo_ansible_inventory = ansible.has_tripleo_ansible_inventory has_tripleo_ansible_inventory = _ansible.has_tripleo_ansible_inventory
read_tripleo_ansible_inventory = ansible.read_tripleo_ansible_inventory read_tripleo_ansible_inventory = _ansible.read_tripleo_ansible_inventory
skip_if_missing_tripleo_ansible_inventory = \ skip_if_missing_tripleo_ansible_inventory = \
ansible.skip_if_missing_tripleo_ansible_inventory _ansible.skip_if_missing_tripleo_ansible_inventory
run_playbook_from_undercloud = ansible.run_playbook_from_undercloud run_playbook_from_undercloud = _ansible.run_playbook_from_undercloud
find_overcloud_node = overcloud.find_overcloud_node find_overcloud_node = overcloud.find_overcloud_node
list_overcloud_nodes = overcloud.list_overcloud_nodes list_overcloud_nodes = overcloud.list_overcloud_nodes

View File

@ -31,9 +31,9 @@ def get_tripleo_ansible_inventory():
return tobiko.load_yaml(fd) return tobiko.load_yaml(fd)
def has_tripleo_ansible_inventory(): def has_tripleo_ansible_inventory() -> bool:
inventory_file = get_tripleo_ansible_inventory_file() inventory_file = get_tripleo_ansible_inventory_file()
return inventory_file and os.path.isfile(inventory_file) return bool(inventory_file)
skip_if_missing_tripleo_ansible_inventory = \ skip_if_missing_tripleo_ansible_inventory = \
@ -41,22 +41,12 @@ skip_if_missing_tripleo_ansible_inventory = \
has_tripleo_ansible_inventory) has_tripleo_ansible_inventory)
def get_tripleo_ansible_inventory_file(): def get_tripleo_ansible_inventory_file() -> typing.Optional[str]:
return tobiko.setup_fixture(TripleoAnsibleInventoryFixture).inventory_file if _undercloud.has_undercloud():
inventory_file = _config.get_tripleo_config().inventory_file
if inventory_file:
class TripleoAnsibleInventoryFixture(tobiko.SharedFixture): return tobiko.tobiko_config_path(inventory_file)
return None
inventory_file = None
def setup_fixture(self):
tripleo = _config.get_tripleo_config()
self.inventory_file = inventory_file = tobiko.tobiko_config_path(
tripleo.inventory_file)
if inventory_file is not None and not os.path.isfile(inventory_file):
content = read_tripleo_ansible_inventory()
with io.open(inventory_file, 'w') as fd:
fd.write(content)
READ_TRIPLEO_ANSIBLE_INVENTORY_SCRIPT = """ READ_TRIPLEO_ANSIBLE_INVENTORY_SCRIPT = """
@ -86,19 +76,27 @@ def read_tripleo_ansible_inventory():
return sh.execute('/bin/bash', stdin=script, ssh_client=ssh_client).stdout return sh.execute('/bin/bash', stdin=script, ssh_client=ssh_client).stdout
def create_tripleo_inventary_file(inventory_file: str):
content = read_tripleo_ansible_inventory()
tobiko.makedirs(os.path.dirname(inventory_file))
with io.open(inventory_file, 'w') as fd:
fd.write(content)
class UndercloudAnsiblePlaybook(ansible.AnsiblePlaybook): class UndercloudAnsiblePlaybook(ansible.AnsiblePlaybook):
@property @property
def ssh_client(self) -> ssh.SSHClientType: def ssh_client(self) -> ssh.SSHClientType:
return _undercloud.undercloud_ssh_client() return _undercloud.undercloud_ssh_client()
def setup_fixture(self): def _ensure_inventory_files(self, *inventory_filenames: str) \
self._inventory_filenames.append(self.get_ansible_inventory_file()) -> typing.List[str]:
super(UndercloudAnsiblePlaybook, self).setup_fixture() inventory_file = get_tripleo_ansible_inventory_file()
if inventory_file is not None:
@staticmethod if not os.path.isfile(inventory_file):
def get_ansible_inventory_file() -> str: create_tripleo_inventary_file(inventory_file=inventory_file)
return get_tripleo_ansible_inventory_file() inventory_filenames += (inventory_file,)
return super()._ensure_inventory_files(*inventory_filenames)
def undercloud_ansible_playbook() -> UndercloudAnsiblePlaybook: def undercloud_ansible_playbook() -> UndercloudAnsiblePlaybook:

View File

@ -59,7 +59,7 @@ OPTIONS = [
# Other options # Other options
cfg.StrOpt('inventory_file', cfg.StrOpt('inventory_file',
default='tripleo-hosts.yaml', default='.ansible/inventory/tripleo.yaml',
help="path to where to export tripleo inventory file"), help="path to where to export tripleo inventory file"),
] ]
@ -75,10 +75,10 @@ def list_options():
def setup_tobiko_config(conf): def setup_tobiko_config(conf):
# pylint: disable=unused-argument # pylint: disable=unused-argument
from tobiko.tripleo import ansible from tobiko.tripleo import _ansible
from tobiko.tripleo import overcloud from tobiko.tripleo import overcloud
from tobiko.tripleo import topology from tobiko.tripleo import topology
ansible.setup_undercloud_ansible_playbook() _ansible.setup_undercloud_ansible_playbook()
overcloud.setup_overcloud_keystone_crederntials() overcloud.setup_overcloud_keystone_crederntials()
topology.setup_tripleo_topology() topology.setup_tripleo_topology()