From 21f811ef6854f1a8b6c78f7be89462342c4a8dea Mon Sep 17 00:00:00 2001 From: Olivier Date: Sun, 20 Oct 2024 13:10:52 +0200 Subject: [PATCH] tags --- ansible/roles/base/vars/Linux Mint.yml | 2 ++ config.yml | 2 ++ linuxinstaller-firstboot.sh | 2 +- linuxinstaller/AnsibleActions.py | 36 +++++++++++++++++++------- linuxinstaller/linuxinstaller.py | 20 +++++++++++--- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/ansible/roles/base/vars/Linux Mint.yml b/ansible/roles/base/vars/Linux Mint.yml index 88216b1..03ccbbf 100644 --- a/ansible/roles/base/vars/Linux Mint.yml +++ b/ansible/roles/base/vars/Linux Mint.yml @@ -10,6 +10,8 @@ required_packages: - nemo-image-converter - nemo-nextcloud - nemo-audio-tab + - gpaste-2 + - gir1.2-gpaste-2 remove_packages: - mintchat diff --git a/config.yml b/config.yml index bcc8148..9441782 100644 --- a/config.yml +++ b/config.yml @@ -33,11 +33,13 @@ ansible_playbooks: update: menu_title: Exécuter la mise à jour des paquets + tags: [ firstboot ] argument_help: Ne présente pas le menu et exécute mise à jour des paquets playbook: update.yml conformite: menu_title: Exécuter la configuration du poste + tags: [ firstboot ] argument_help: Ne présente pas le menu et exécute la conformité playbook: conformite.yml diff --git a/linuxinstaller-firstboot.sh b/linuxinstaller-firstboot.sh index 60ed09f..1ac9d7e 100755 --- a/linuxinstaller-firstboot.sh +++ b/linuxinstaller-firstboot.sh @@ -2,5 +2,5 @@ set -eo pipefail logger -t linuxinstaller "Exécution de linuxinstaller-firstboot.sh" until host git.libretic.fr; do sleep 5; done -/opt/linuxinstaller/linuxinstaller.sh --update --conformite \ +/opt/linuxinstaller/linuxinstaller.sh --tags firstboot \ && systemctl disable linuxinstaller-firstboot.service diff --git a/linuxinstaller/AnsibleActions.py b/linuxinstaller/AnsibleActions.py index 6042864..8540298 100644 --- a/linuxinstaller/AnsibleActions.py +++ b/linuxinstaller/AnsibleActions.py @@ -3,22 +3,38 @@ import json from InquirerPy import inquirer from InquirerPy import prompt -def runAnsiblePlaybook(config, key): - extra_vars={} +def runAnsiblePlaybook(config, key, extra_vars={}): + extra_vars = extra_vars.copy() if 'questions' in config['ansible_playbooks'][key]: - for question in config['ansible_playbooks'][key]['questions']: + for question in config['ansible_playbooks'][key]['questions'].copy(): var_name = question.pop('var_name') extra_vars[var_name] = prompt(question)[0] if 'extra_vars' in config['ansible_playbooks'][key]: extra_vars = extra_vars | config['ansible_playbooks'][key]['extra_vars'] - if len(extra_vars) > 0: - subprocess.check_call(['ansible-playbook', config['ansible_playbooks'][key]['playbook'], - '--extra-vars', json.dumps(extra_vars) ], - cwd=config['base_path']) + if 'playbook_url' in config['ansible_playbooks'][key]: + runPulledAnsiblePlaybook(config['ansible_playbooks'][key]['playbook'], + config['ansible_playbooks'][key]['playbook_url'], + extra_vars) else: - subprocess.check_call(['ansible-playbook', config['ansible_playbooks'][key]['playbook']], cwd=config['base_path']) + runLocalAnsiblePlaybook(config['ansible_playbooks'][key]['playbook'], + config['base_path'], + extra_vars) -def pullAnsiblePlaybook(url, playbook): - subprocess.check_call(['ansible-pull', '-U', url, playbook]) + +def runLocalAnsiblePlaybook(playbook, chdir, extra_vars={}): + if len(extra_vars) > 0: + subprocess.check_call(['ansible-playbook', playbook, + '--extra-vars', json.dumps(extra_vars) ], + cwd=chdir) + else: + subprocess.check_call(['ansible-playbook', playbook], cwd=chdir) + +def runPulledAnsiblePlaybook(playbook, url, extra_vars={}): + if len(extra_vars) > 0: + subprocess.check_call(['ansible-playbook', playbook, + '--extra-vars', json.dumps(extra_vars) ], + cwd=chdir) + else: + subprocess.check_call(['ansible-pull', '-U', url, playbook]) diff --git a/linuxinstaller/linuxinstaller.py b/linuxinstaller/linuxinstaller.py index eaf6e19..edba6af 100644 --- a/linuxinstaller/linuxinstaller.py +++ b/linuxinstaller/linuxinstaller.py @@ -91,7 +91,7 @@ def extPlaybookChoice(): confirm_letter = "o", default=False).execute() if proceed: - AnsibleActions.pullAnsiblePlaybook(url, playbook) + AnsibleActions.runPulledAnsiblePlaybook(playbook, url) else: print("Annulation") @@ -125,8 +125,10 @@ def branchChoice(): def main(): config = readConfig("config.yml") currentBranch = GitActions.getBranch() - - parser = argparse.ArgumentParser(prog="linuxinstaller.py", description="Menu de configuration Libretic") + + description="Menu de configuration linuxinstaller" + parser = argparse.ArgumentParser(description=description) + parser.add_argument("--tags", help="Exécute les playbooks portant ces étiquettes", action="extend", nargs="+", type=str) for entry in config['ansible_playbooks']: if 'argument_help' in config['ansible_playbooks'][entry]: parser.add_argument("--" + entry, help=config['ansible_playbooks'][entry]['argument_help'], action='store_true') @@ -142,7 +144,7 @@ def main(): menu_main = menu_main | { config['ansible_playbooks'][entry]['menu_title']: lambda e=entry: AnsibleActions.runAnsiblePlaybook(config, e) } - + menu_main = menu_main | { "Exécuter un playbook d'un projet spécifique": extPlaybookChoice, "Changer de branche": branchChoice, @@ -152,7 +154,17 @@ def main(): # Exécute les options passées sur la ligne de commande cmdlineOptionPassed = False for entry in config['ansible_playbooks']: + mustRun = False + + # Teste si la clé est dans la liste des arguments if entry in list(arg for arg in vars(args) if getattr(args, arg)): + mustRun = True + + # Teste si un élément commun entre les tags mentionnés dans la conf et les tags en argument + if len(args.tags.intersection(entry['tags'])) > 0: + mustRun = True + + if mustRun == True: AnsibleActions.runAnsiblePlaybook(config, entry) cmdlineOptionPassed = True