#!/usr/bin/env python3 # ansible.py - PVC Cluster Auto-bootstrap Ansible libraries # Part of the Parallel Virtual Cluster (PVC) system # # Copyright (C) 2018-2021 Joshua M. Boniface # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # ############################################################################### import pvcbootstrapd.lib.git as git import ansible_runner import tempfile import yaml from time import sleep from celery.utils.log import get_task_logger logger = get_task_logger(__name__) def run_bootstrap(config, cspec, cluster, nodes): """ Run an Ansible bootstrap against a cluster """ logger.debug(nodes) # Construct our temporary INI inventory string logger.info(f"Constructing virtual Ansible inventory") base_yaml = git.load_base_yaml(config, cluster.name) local_domain = base_yaml.get('local_domain') inventory = [f"""[{cluster.name}]"""] for node in nodes: inventory.append(f"""{node.name}.{local_domain} ansible_host={node.host_ipaddr}""") inventory = '\n'.join(inventory) logger.debug(inventory) # Waiting 30 seconds to ensure everything is booted an stabilized logger.info("Waiting 30s before starting Ansible bootstrap.") sleep(30) # Run the Ansible playbooks with tempfile.TemporaryDirectory(prefix="pvc-ansible-bootstrap_") as pdir: r = ansible_runner.run(private_data_dir=f"{pdir}", inventory=inventory, limit=f"{cluster.name}", playbook=f"{config['ansible_path']}/pvc.yml", extravars={"bootstrap": "yes"}) logger.info("Final status:") logger.info("{}: {}".format(r.status, r.rc)) logger.info(r.stats) if r.rc == 0: git.commit_repository() git.push_repository()