diff --git a/.gitignore b/.gitignore index aff3db9f2..bc9e036de 100644 --- a/.gitignore +++ b/.gitignore @@ -45,8 +45,8 @@ tobiko.conf .infrared workspace.tgz -# Tripleo files -tripleo-hosts.yaml +# Ansible files +.ansible/ # Report files tobiko_results* diff --git a/tobiko/shell/ansible/_playbook.py b/tobiko/shell/ansible/_playbook.py index 5681c5402..f857fdc1a 100644 --- a/tobiko/shell/ansible/_playbook.py +++ b/tobiko/shell/ansible/_playbook.py @@ -98,7 +98,7 @@ class AnsiblePlaybook(tobiko.SharedFixture): -> typing.List[str]: filenames = list(inventory_filenames) filenames.extend(self._inventory_filenames) - filenames.extend(tobiko.tobiko_config().ansible.inventory) + filenames.extend(tobiko.tobiko_config().ansible.inventory_files) existing_filenames = [] for filename in sorted(filenames): filename = tobiko.tobiko_config_path(filename) diff --git a/tobiko/shell/ansible/config.py b/tobiko/shell/ansible/config.py index d08740029..e1a2444f3 100644 --- a/tobiko/shell/ansible/config.py +++ b/tobiko/shell/ansible/config.py @@ -21,7 +21,7 @@ from oslo_config import cfg GROUP_NAME = 'ansible' OPTIONS = [ - cfg.ListOpt('inventory', + cfg.ListOpt('inventory_files', default=['/etc/ansible/hosts'], help="Default Ansible inventory files"), ] diff --git a/tobiko/tests/sanity/shiftstack/test_shiftstack.py b/tobiko/tests/sanity/shiftstack/test_shiftstack.py index f3497e3b3..14e2973d3 100644 --- a/tobiko/tests/sanity/shiftstack/test_shiftstack.py +++ b/tobiko/tests/sanity/shiftstack/test_shiftstack.py @@ -23,10 +23,9 @@ from tobiko import tripleo 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): - @tripleo.skip_if_missing_tripleo_ansible_inventory def test_ping_all_hosts(self): tripleo.run_playbook_from_undercloud( playbook='ping-shiftstack.yaml', diff --git a/tobiko/tripleo/__init__.py b/tobiko/tripleo/__init__.py index 028f01df1..d94200f0c 100644 --- a/tobiko/tripleo/__init__.py +++ b/tobiko/tripleo/__init__.py @@ -13,20 +13,20 @@ # under the License. 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 _topology as topology 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 = \ - ansible.get_tripleo_ansible_inventory_file -has_tripleo_ansible_inventory = ansible.has_tripleo_ansible_inventory -read_tripleo_ansible_inventory = ansible.read_tripleo_ansible_inventory + _ansible.get_tripleo_ansible_inventory_file +has_tripleo_ansible_inventory = _ansible.has_tripleo_ansible_inventory +read_tripleo_ansible_inventory = _ansible.read_tripleo_ansible_inventory skip_if_missing_tripleo_ansible_inventory = \ - ansible.skip_if_missing_tripleo_ansible_inventory -run_playbook_from_undercloud = ansible.run_playbook_from_undercloud + _ansible.skip_if_missing_tripleo_ansible_inventory +run_playbook_from_undercloud = _ansible.run_playbook_from_undercloud find_overcloud_node = overcloud.find_overcloud_node list_overcloud_nodes = overcloud.list_overcloud_nodes diff --git a/tobiko/tripleo/_ansible.py b/tobiko/tripleo/_ansible.py index 56d267789..fbe619e9c 100644 --- a/tobiko/tripleo/_ansible.py +++ b/tobiko/tripleo/_ansible.py @@ -31,9 +31,9 @@ def get_tripleo_ansible_inventory(): return tobiko.load_yaml(fd) -def has_tripleo_ansible_inventory(): +def has_tripleo_ansible_inventory() -> bool: 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 = \ @@ -41,22 +41,12 @@ skip_if_missing_tripleo_ansible_inventory = \ has_tripleo_ansible_inventory) -def get_tripleo_ansible_inventory_file(): - return tobiko.setup_fixture(TripleoAnsibleInventoryFixture).inventory_file - - -class TripleoAnsibleInventoryFixture(tobiko.SharedFixture): - - 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) +def get_tripleo_ansible_inventory_file() -> typing.Optional[str]: + if _undercloud.has_undercloud(): + inventory_file = _config.get_tripleo_config().inventory_file + if inventory_file: + return tobiko.tobiko_config_path(inventory_file) + return None 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 +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): @property def ssh_client(self) -> ssh.SSHClientType: return _undercloud.undercloud_ssh_client() - def setup_fixture(self): - self._inventory_filenames.append(self.get_ansible_inventory_file()) - super(UndercloudAnsiblePlaybook, self).setup_fixture() - - @staticmethod - def get_ansible_inventory_file() -> str: - return get_tripleo_ansible_inventory_file() + def _ensure_inventory_files(self, *inventory_filenames: str) \ + -> typing.List[str]: + inventory_file = get_tripleo_ansible_inventory_file() + if inventory_file is not None: + if not os.path.isfile(inventory_file): + create_tripleo_inventary_file(inventory_file=inventory_file) + inventory_filenames += (inventory_file,) + return super()._ensure_inventory_files(*inventory_filenames) def undercloud_ansible_playbook() -> UndercloudAnsiblePlaybook: diff --git a/tobiko/tripleo/config.py b/tobiko/tripleo/config.py index c3ba39b03..a62b35581 100644 --- a/tobiko/tripleo/config.py +++ b/tobiko/tripleo/config.py @@ -59,7 +59,7 @@ OPTIONS = [ # Other options cfg.StrOpt('inventory_file', - default='tripleo-hosts.yaml', + default='.ansible/inventory/tripleo.yaml', help="path to where to export tripleo inventory file"), ] @@ -75,10 +75,10 @@ def list_options(): def setup_tobiko_config(conf): # pylint: disable=unused-argument - from tobiko.tripleo import ansible + from tobiko.tripleo import _ansible from tobiko.tripleo import overcloud from tobiko.tripleo import topology - ansible.setup_undercloud_ansible_playbook() + _ansible.setup_undercloud_ansible_playbook() overcloud.setup_overcloud_keystone_crederntials() topology.setup_tripleo_topology()