Refactor ansible inventory generation
- Removes the incorrect swift_url lookup - Removes the usage of session object - Removes os_auth_token for undercloud - Moves the code to inventory module for it to be used in ansible module Change-Id: I3cdce51764a27a389e008832ed366b6346c4f0e0
This commit is contained in:
parent
a81d63eb8d
commit
3952924859
@ -521,38 +521,20 @@ class AnsibleGenerateInventoryAction(base.TripleOAction):
|
||||
'undercloud_key_file', None)
|
||||
self.ansible_python_interpreter = self._kwargs_for_run.pop(
|
||||
'ansible_python_interpreter', None)
|
||||
self._work_dir = self._kwargs_for_run.pop(
|
||||
self.work_dir = self._kwargs_for_run.pop(
|
||||
'work_dir', None)
|
||||
self.plan_name = self._kwargs_for_run.pop(
|
||||
'plan_name', 'overcloud')
|
||||
self.ssh_network = self._kwargs_for_run.pop(
|
||||
'ssh_network', 'ctlplane')
|
||||
|
||||
@property
|
||||
def work_dir(self):
|
||||
if self._work_dir:
|
||||
return self._work_dir
|
||||
self._work_dir = tempfile.mkdtemp(prefix='ansible-mistral-action')
|
||||
return self._work_dir
|
||||
|
||||
def run(self, context):
|
||||
|
||||
inventory_path = os.path.join(
|
||||
self.work_dir, 'tripleo-ansible-inventory.yaml')
|
||||
|
||||
inv = inventory.TripleoInventory(
|
||||
session=self.get_session(context, 'heat'),
|
||||
hclient=self.get_orchestration_client(context),
|
||||
auth_url=context.security.auth_uri,
|
||||
cacert=context.security.auth_cacert,
|
||||
project_name=context.security.project_name,
|
||||
username=context.security.user_name,
|
||||
return inventory.generate_tripleo_ansible_inventory(
|
||||
heat=self.get_orchestration_client(context),
|
||||
work_dir=self.work_dir,
|
||||
plan=self.plan_name,
|
||||
auth=context.security,
|
||||
ansible_ssh_user=self.ansible_ssh_user,
|
||||
undercloud_key_file=self.undercloud_key_file,
|
||||
undercloud_connection=inventory.UNDERCLOUD_CONNECTION_SSH,
|
||||
ansible_python_interpreter=self.ansible_python_interpreter,
|
||||
plan_name=self.plan_name,
|
||||
host_network=self.ssh_network)
|
||||
|
||||
inv.write_static_inventory(inventory_path)
|
||||
return inventory_path
|
||||
ssh_network=self.ssh_network)
|
||||
|
@ -19,6 +19,7 @@ from collections import OrderedDict
|
||||
import logging
|
||||
import os.path
|
||||
import sys
|
||||
import tempfile
|
||||
import yaml
|
||||
|
||||
from heatclient.exc import HTTPNotFound
|
||||
@ -90,7 +91,6 @@ class TripleoInventory(object):
|
||||
host_network=None, ansible_python_interpreter=None,
|
||||
undercloud_connection=UNDERCLOUD_CONNECTION_LOCAL,
|
||||
undercloud_key_file=None, serial=1):
|
||||
self.session = session
|
||||
self.hclient = hclient
|
||||
self.host_network = host_network or HOST_NETWORK
|
||||
self.auth_url = auth_url
|
||||
@ -168,9 +168,6 @@ class TripleoInventory(object):
|
||||
# see https://github.com/ansible/ansible/issues/41808
|
||||
'ansible_remote_tmp': '/tmp/ansible-${USER}',
|
||||
'auth_url': self.auth_url,
|
||||
'cacert': self.cacert,
|
||||
'os_auth_token':
|
||||
self.session.get_token() if self.session else None,
|
||||
'plan': self.plan_name,
|
||||
'project_name': self.project_name,
|
||||
'username': self.username,
|
||||
@ -178,6 +175,10 @@ class TripleoInventory(object):
|
||||
}
|
||||
})
|
||||
|
||||
if self.cacert:
|
||||
ret['Undercloud']['vars']['cacert'] = \
|
||||
self.cacert
|
||||
|
||||
if self.ansible_python_interpreter:
|
||||
ret['Undercloud']['vars']['ansible_python_interpreter'] = \
|
||||
self.ansible_python_interpreter
|
||||
@ -189,12 +190,6 @@ class TripleoInventory(object):
|
||||
ret['Undercloud']['vars']['ansible_ssh_private_key_file'] = \
|
||||
self.undercloud_key_file
|
||||
|
||||
swift_url = None
|
||||
if self.session:
|
||||
swift_url = self.session.get_endpoint(service_type='object-store',
|
||||
interface='public')
|
||||
ret['Undercloud']['vars']['undercloud_swift_url'] = swift_url
|
||||
|
||||
ret['Undercloud']['vars']['undercloud_service_list'] = \
|
||||
self.get_undercloud_service_list()
|
||||
|
||||
@ -385,3 +380,33 @@ class TripleoInventory(object):
|
||||
|
||||
with open(inventory_file_path, 'w') as inventory_file:
|
||||
yaml.dump(inventory, inventory_file, TemplateDumper)
|
||||
|
||||
|
||||
def generate_tripleo_ansible_inventory(heat, auth,
|
||||
cacert=None,
|
||||
plan='overcloud',
|
||||
work_dir=None,
|
||||
ansible_python_interpreter=None,
|
||||
ansible_ssh_user='tripleo-admin',
|
||||
undercloud_key_file=None,
|
||||
ssh_network='ctlplane'):
|
||||
if not work_dir:
|
||||
work_dir = tempfile.mkdtemp(prefix='tripleo-ansible')
|
||||
|
||||
inventory_path = os.path.join(
|
||||
work_dir, 'tripleo-ansible-inventory.yaml')
|
||||
inv = TripleoInventory(
|
||||
hclient=heat,
|
||||
auth_url=auth.auth_uri,
|
||||
cacert=cacert,
|
||||
project_name=auth.project_name,
|
||||
username=auth.user_name,
|
||||
ansible_ssh_user=ansible_ssh_user,
|
||||
undercloud_key_file=undercloud_key_file,
|
||||
undercloud_connection=UNDERCLOUD_CONNECTION_SSH,
|
||||
ansible_python_interpreter=ansible_python_interpreter,
|
||||
plan_name=plan,
|
||||
host_network=ssh_network)
|
||||
|
||||
inv.write_static_inventory(inventory_path)
|
||||
return inventory_path
|
||||
|
@ -117,13 +117,8 @@ class TestInventory(base.TestCase):
|
||||
self.mock_stack.outputs = self.outputs_data['outputs']
|
||||
self.hclient.stacks.get.return_value = self.mock_stack
|
||||
|
||||
self.session = MagicMock()
|
||||
self.session.get_token.return_value = 'atoken'
|
||||
self.session.get_endpoint.return_value = 'anendpoint'
|
||||
|
||||
self.outputs = StackOutputs(self.mock_stack)
|
||||
self.inventory = TripleoInventory(
|
||||
session=self.session,
|
||||
hclient=self.hclient,
|
||||
plan_name=self.plan_name,
|
||||
auth_url='xyz://keystone.local',
|
||||
@ -230,7 +225,6 @@ class TestInventory(base.TestCase):
|
||||
'ansible_remote_tmp': '/tmp/ansible-${USER}',
|
||||
'auth_url': 'xyz://keystone.local',
|
||||
'cacert': 'acacert',
|
||||
'os_auth_token': 'atoken',
|
||||
'overcloud_keystone_url': 'xyz://keystone',
|
||||
'overcloud_admin_password': 'theadminpw',
|
||||
'plan': 'overcloud',
|
||||
@ -242,7 +236,6 @@ class TestInventory(base.TestCase):
|
||||
'tripleo_swift_container_server',
|
||||
'tripleo_swift_object_server',
|
||||
'tripleo_mistral_engine'],
|
||||
'undercloud_swift_url': 'anendpoint',
|
||||
'username': 'admin'}}}
|
||||
inv_list = inventory.list()
|
||||
for k in expected:
|
||||
@ -251,20 +244,12 @@ class TestInventory(base.TestCase):
|
||||
def test_ansible_ssh_user(self):
|
||||
self._try_alternative_args(
|
||||
ansible_ssh_user='my-custom-admin',
|
||||
undercloud_connection='ssh',
|
||||
session=self.session,)
|
||||
undercloud_connection='ssh')
|
||||
|
||||
def test_no_session(self):
|
||||
self._try_alternative_args(
|
||||
ansible_ssh_user='my-custom-admin',
|
||||
undercloud_connection='ssh',
|
||||
session=None)
|
||||
|
||||
def _try_alternative_args(self, ansible_ssh_user, session,
|
||||
def _try_alternative_args(self, ansible_ssh_user,
|
||||
undercloud_connection):
|
||||
key_file = '/var/lib/mistral/.ssh/%s-key' % ansible_ssh_user
|
||||
self.inventory = TripleoInventory(
|
||||
session=session,
|
||||
hclient=self.hclient,
|
||||
plan_name=self.plan_name,
|
||||
auth_url='xyz://keystone.local',
|
||||
@ -347,8 +332,6 @@ class TestInventory(base.TestCase):
|
||||
'ansible_remote_tmp': '/tmp/ansible-${USER}',
|
||||
'auth_url': 'xyz://keystone.local',
|
||||
'cacert': 'acacert',
|
||||
'os_auth_token':
|
||||
'atoken' if session else None,
|
||||
'overcloud_keystone_url': 'xyz://keystone',
|
||||
'overcloud_admin_password': 'theadminpw',
|
||||
'plan': 'overcloud',
|
||||
@ -360,8 +343,6 @@ class TestInventory(base.TestCase):
|
||||
'tripleo_swift_container_server',
|
||||
'tripleo_swift_object_server',
|
||||
'tripleo_mistral_engine'],
|
||||
'undercloud_swift_url':
|
||||
'anendpoint' if session else None,
|
||||
'username': 'admin'}}}
|
||||
|
||||
inv_list = self.inventory.list()
|
||||
@ -475,7 +456,6 @@ class TestInventory(base.TestCase):
|
||||
'/tmp/ansible-${USER}',
|
||||
'auth_url': 'xyz://keystone.local',
|
||||
'cacert': 'acacert',
|
||||
'os_auth_token': 'atoken',
|
||||
'overcloud_admin_password': 'theadminpw',
|
||||
'overcloud_keystone_url': 'xyz://keystone',
|
||||
'plan': 'overcloud',
|
||||
@ -487,7 +467,6 @@ class TestInventory(base.TestCase):
|
||||
'tripleo_swift_container_server',
|
||||
'tripleo_swift_object_server',
|
||||
'tripleo_mistral_engine'],
|
||||
'undercloud_swift_url': 'anendpoint',
|
||||
'username': 'admin'}}}
|
||||
if extra_vars:
|
||||
expected['Undercloud']['vars']['anextravar'] = 123
|
||||
|
Loading…
x
Reference in New Issue
Block a user