Make 'ansible_playbooks' pre-network config only

Distingushing between pre- and post- network config
playbooks is not neccecary. Anything post-network
config can be done using the THT extra config interfaces.

Also, refactor bm post deploy playbooks to utils:
* Add methods in utils module to run playbooks against
  nodes provisioned by the baremetal deploy workflow.
* Re-factors the implementation in node provision to use
  the new methods in utils module.

Change-Id: Ief11590f3421ffd6fb1b4312c07ce9de48f17c3d
This commit is contained in:
Harald Jensås 2021-06-01 17:34:53 +02:00
parent ef077696a0
commit c3a8abfdc1
3 changed files with 70 additions and 71 deletions

View File

@ -352,6 +352,7 @@ class TestProvisionNode(fakes.TestOvercloudNode):
'node_timeout': 3600,
'concurrency': 20,
'manage_network_ports': False,
'configure_networking': False,
'working_dir': mock.ANY,
},
inventory='localhost,',

View File

@ -2813,3 +2813,65 @@ def is_network_data_v2(networks_file_path):
return False
return True
def rel_or_abs_path_role_playbook(roles_file_dir, playbook):
if os.path.isabs(playbook):
playbook_path = playbook
else:
# Load for playbook relative to the roles file
playbook_path = os.path.join(roles_file_dir, playbook)
return playbook_path
def validate_roles_playbooks(roles_file_dir, roles):
not_found = []
playbooks = []
for role in roles:
playbooks.extend(role.get('ansible_playbooks', []))
for x in playbooks:
path = rel_or_abs_path_role_playbook(roles_file_dir, x['playbook'])
if not os.path.exists(path) or not os.path.isfile(path):
not_found.append(path)
if not_found:
raise exceptions.InvalidPlaybook(
'Invalid Playbook(s) {}, file(s) not found.'.format(
', '.join(not_found)))
def run_role_playbook(self, inventory, relative_dir, playbook,
limit_hosts=None, extra_vars=dict()):
playbook_path = rel_or_abs_path_role_playbook(relative_dir, playbook)
playbook_dir = os.path.basename(playbook_path)
with TempDirs() as tmp:
run_ansible_playbook(
playbook=playbook_path,
inventory=inventory,
workdir=tmp,
playbook_dir=playbook_dir,
verbosity=playbook_verbosity(self=self),
limit_hosts=limit_hosts,
extra_vars=extra_vars,
)
def run_role_playbooks(self, working_dir, roles_file_dir, roles):
inventory_file = os.path.join(working_dir,
'tripleo-ansible-inventory.yaml')
with open(inventory_file, 'r') as f:
inventory = yaml.safe_load(f.read())
# Pre-Network Config
for role in roles:
for x in role.get('ansible_playbooks', []):
run_role_playbook(self, inventory, roles_file_dir, x['playbook'],
limit_hosts=role['name'],
extra_vars=x.get('extra_vars', {}))
# Network Config
run_role_playbook(self, inventory, constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
'cli-overcloud-node-network-config.yaml')

View File

@ -290,9 +290,12 @@ class ProvisionNode(command.Command):
oooutils.makedirs(working_dir)
roles_file_path = os.path.abspath(parsed_args.input)
roles_file_dir = os.path.dirname(roles_file_path)
with open(roles_file_path, 'r') as fp:
roles = yaml.safe_load(fp)
oooutils.validate_roles_playbooks(roles_file_dir, roles)
key = self.get_key_pair(parsed_args)
with open('{}.pub'.format(key), 'rt') as fp:
ssh_key = fp.read()
@ -310,6 +313,7 @@ class ProvisionNode(command.Command):
"concurrency": parsed_args.concurrency,
"manage_network_ports": (parsed_args.network_ports
or parsed_args.network_config),
"configure_networking": parsed_args.network_config,
"working_dir": working_dir
}
@ -323,77 +327,9 @@ class ProvisionNode(command.Command):
extra_vars=extra_vars,
)
if parsed_args.network_ports or parsed_args.network_config:
roles_file_dir = os.path.dirname(roles_file_path)
inventory_file = os.path.join(working_dir,
'tripleo-ansible-inventory.yaml')
with open(inventory_file, 'r') as f:
inventory = yaml.safe_load(f.read())
# Pre-Network Config
for role in roles:
for playbook in role.get('ansible_playbooks', []):
if not playbook.get('pre_network'):
continue
if os.path.isabs(playbook['playbook']):
playbook_path = playbook['playbook']
else:
# Load for playbook relative to the roles file
playbook_path = os.path.join(roles_file_dir,
playbook['playbook'])
self._validate_playbook(playbook_path)
playbook_dir = os.path.basename(playbook_path)
with oooutils.TempDirs() as tmp:
oooutils.run_ansible_playbook(
playbook=playbook_path,
inventory=inventory,
workdir=tmp,
playbook_dir=playbook_dir,
verbosity=oooutils.playbook_verbosity(self=self),
limit_hosts=role['name'],
extra_vars=playbook.get('extra_vars', {})
)
# Network Config
if parsed_args.network_config:
with oooutils.TempDirs() as tmp:
oooutils.run_ansible_playbook(
playbook='cli-overcloud-node-network-config.yaml',
inventory=inventory,
workdir=tmp,
playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=oooutils.playbook_verbosity(self=self),
)
# Post-Network Config
for role in roles:
for playbook in role.get('ansible_playbooks', []):
if playbook.get('pre_network'):
continue
if os.path.isabs(playbook['playbook']):
playbook_path = playbook['playbook']
else:
# Load for playbook relative to the roles file
playbook_path = os.path.join(roles_file_dir,
playbook['playbook'])
self._validate_playbook(playbook_path)
playbook_dir = os.path.basename(playbook_path)
with oooutils.TempDirs() as tmp:
oooutils.run_ansible_playbook(
playbook=playbook_path,
inventory=inventory,
workdir=tmp,
playbook_dir=playbook_dir,
verbosity=oooutils.playbook_verbosity(self=self),
limit_hosts=role['name'],
extra_vars=playbook.get('extra_vars', {})
)
if parsed_args.network_config:
oooutils.run_role_playbooks(self, working_dir, roles_file_dir,
roles)
print('Nodes deployed successfully, add %s to your deployment '
'environment' % parsed_args.output)