Préparation ansible-pull

This commit is contained in:
Navas 2024-10-08 12:19:49 +02:00
parent 952f44abf2
commit de8c9f3fc6
2 changed files with 16 additions and 29 deletions

View file

@ -1,9 +1,7 @@
import subprocess import subprocess
import os
import glob
def runAnsiblePlaybook(config, key, branch): def runAnsiblePlaybook(config, key):
subprocess.run(['ansible-playbook', config['ansiblemenu'][key]['playbook']], check=True, cwd=config['base_path']) subprocess.check_call(['ansible-playbook', config['ansiblemenu'][key]['playbook']], cwd=config['base_path'])
def pullAnsiblePlaybook(config, key, branch): def pullAnsiblePlaybook(url, playbook):
subprocess.run(['ansible-pull', config['ansiblemenu'][key]['playbook']], check=True, cwd=config['base_path']) subprocess.check_call(['ansible-pull', '-U', url, playbook])

View file

@ -70,39 +70,28 @@ def hostnameChoice():
### Menu du choix playbook externe ### Menu du choix playbook externe
def extPlaybookChoice(): def extPlaybookChoice():
url = inquirer.text( url = inquirer.text(
message = "Renseigner l'url du projet git (exemple : https://adresse-serveur-git/projet.git)", message = "Renseigner l'url du projet git (exemple : https://adresse-serveur-git/projet.git) :",
validate = lambda url : validate = lambda url :
len(url) > 0 and len(url) > 0 and
(url.startswith("http://") or url.startswith("https://")) and (url.startswith("http://") or url.startswith("https://")) and
url.endswith(".git") url.endswith(".git"),
invalid_message = "Une url de projet git doit commencer par http(s):// et se terminer par .git"
).execute() ).execute()
playbook = inquirer.text( playbook = inquirer.text(
message = "Renseigner le nom du playbook", message = "Renseigner le nom du playbook :",
validate = lambda playbook : validate = lambda playbook :
len(playbook) > 0 and len(playbook) > 0 and
(playbook.endswith(".yaml") or playbook.endswith(".yml")) (playbook.endswith(".yaml") or playbook.endswith(".yml")),
).execute() invalid_message = "Un nom de playbook doit se terminer par .yml ou .yaml"
branch = inquirer.text(
message = "Branche du projet (laisser vide pour branche par défaut)",
).execute()
username = inquirer.text(
message = "Identifiant de connexion (laisser vide si projet public)",
).execute()
if username != "":
password = inquirer.secret(
message = "Mot de passe de connexion",
).execute() ).execute()
proceed = inquirer.confirm( proceed = inquirer.confirm(
message ="Définir {} comme nom de machine ?".format(project), message ="Confirmer l'exécution du playbook {} du projet {} ?".format(playbook, url),
confirm_letter = "o", confirm_letter = "o",
default=False).execute() default=False).execute()
if proceed: if proceed:
print("Confirmation") AnsibleActions.pullAnsiblePlaybook(url, playbook)
else: else:
print("Annulation") print("Annulation")
@ -149,11 +138,11 @@ def main():
for entry in config['ansiblemenu']: for entry in config['ansiblemenu']:
if not config['ansiblemenu'][entry].get('menu_hidden', False): if not config['ansiblemenu'][entry].get('menu_hidden', False):
menu_main = menu_main | { menu_main = menu_main | {
config['ansiblemenu'][entry]['menu_title']: lambda e=entry: AnsibleActions.runAnsiblePlaybook(config, e, currentBranch) config['ansiblemenu'][entry]['menu_title']: lambda e=entry: AnsibleActions.runAnsiblePlaybook(config, e)
} }
menu_main = menu_main | { menu_main = menu_main | {
"Exécuter un playbook d'un projet externe": extPlaybookChoice, "Exécuter un playbook d'un projet spécifique": extPlaybookChoice,
"Changer de branche": branchChoice, "Changer de branche": branchChoice,
"Quitter": None "Quitter": None
} }
@ -162,7 +151,7 @@ def main():
cmdlineOptionPassed = False cmdlineOptionPassed = False
for entry in config['ansiblemenu']: for entry in config['ansiblemenu']:
if entry in list(arg for arg in vars(args) if getattr(args, arg)): if entry in list(arg for arg in vars(args) if getattr(args, arg)):
AnsibleActions.runAnsiblePlaybook(config, entry, currentBranch) AnsibleActions.runAnsiblePlaybook(config, entry)
cmdlineOptionPassed = True cmdlineOptionPassed = True