From 013e55c8437134401aed4abdef2ca18d69f8e559 Mon Sep 17 00:00:00 2001 From: Thomas Constans Date: Tue, 15 Nov 2022 15:20:14 +0100 Subject: [PATCH] simplification et rajout install git et vim --- Readme.md | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/Readme.md b/Readme.md index 17ba12a..1b9a754 100644 --- a/Readme.md +++ b/Readme.md @@ -2,23 +2,21 @@ ## Remarques préliminaires: -le fichier hosts est un fichier d'inventaire ansible contenant mes cibles - ## Télécharger des fichiers sur la cible avec get_url Récupérer dans /tmp le fichier : https://starship.rs/install.sh ```bash -ansible -i hosts -o -u root all -m get_url -a "url=https://starship.rs/install.sh dest=/tmp mode=0700" +ansible -o all -m get_url -a 'url=https://starship.rs/install.sh dest=/tmp mode=0700' ``` ## Exécuter des scripts à distance avec command Exécuter le script précédemment récupéré, uniquement si le fichier /usr/local/bin/starship n'existe pas déjà ```bash -ansible -i hosts -o -u root all -m command -a 'cmd="/tmp/install.sh --yes" creates="/usr/local/bin/starship"' +ansible -o all -m command -a '/tmp/install.sh --yes creates="/usr/local/bin/starship"' #ou, si on n'a pas pensé à rendre le fichier exécutable précédemment: -ansible -i hosts -o -u root all -m command -a 'cmd="sh /tmp/install.sh --yes" creates="/usr/local/bin/starship"' +ansible -o all -m command -a 'sh tmp/install.sh --yes creates="/usr/local/bin/starship"' ``` ## Déployer un fichier avec copy @@ -32,18 +30,30 @@ S'assurer de la présence du fichier /etc/profile.d/zstarship.sh avec le contenu Pour un contenu aussi léger, pas besoin d'avoir à maintenir un fichier local comme source, on va plutôt utiliser l'argument _content_: ```bash -ansible -i hosts -o -u root all -m copy -a 'content="eval \"$(/usr/local/bin/starship init bash)\"" dest=/etc/profile.d/zstarship.sh' +ansible -o all -m copy -a 'content="eval \"$(/usr/local/bin/starship init bash)\"" dest=/etc/profile.d/zstarship.sh' ``` ⚠️ aux quotes ! +## S'assurer de la présende de git sur les cibles + +```bash +ansible -o all -m package -a 'name=git state=present' +``` + ## Cloner des dépôts avec git Cloner le dépôt https://infra.opendoor.fr/git/tom/vim_formation.git dans /opt/vim/ ```bash -ansible -i hosts -o -u root all -m git -a "repo=https://infra.opendoor.fr/git/tom/vim_formation.git dest=/opt/vim" +ansible -o all -m git -a "repo=https://infra.opendoor.fr/git/tom/vim_formation.git dest=/opt/vim" +``` + +## S'assurer de la présende de vim sur les cibles + +```bash +ansible -o all -m package -a 'name=vim state=present' ``` ## S'assurer de l'absence de fichier avec file @@ -51,7 +61,7 @@ ansible -i hosts -o -u root all -m git -a "repo=https://infra.opendoor.fr/git/t Supprimer le fichier /etc/vimrc existant ```bash -ansible -i hosts -o -u root all -m file -a "path=/etc/vim state=absent" +ansible -o all -m file -a "path=/etc/vim state=absent" ``` ## Gérer des liens symboliques, toujours avec file @@ -59,7 +69,7 @@ ansible -i hosts -o -u root all -m file -a "path=/etc/vim state=absent" Faire un lien symbolique /etc/vimrc vers /opt/vim/vimrc ```bash -ansible -i hosts -o -u root all -m file -a "path=/etc/vimrc state=link src=/opt/vim/vimrc" +ansible -o all -m file -a "path=/etc/vimrc state=link src=/opt/vim/vimrc" ``` ## Modifier des fichiers avec lineinfile @@ -67,26 +77,26 @@ ansible -i hosts -o -u root all -m file -a "path=/etc/vimrc state=link src=/opt dans /etc/vimrc, assurez-vous que la ligne 62 (qui commence par set list listchars...) soit _décommentée_ ```bash -ansible -i hosts -o -u root all -m lineinfile -a 'path=/etc/vimrc regex="^\"(set list listchars.*)" line=\1 backrefs=true' +ansible -o all -m lineinfile -a 'path=/etc/vimrc regex="^\"(set list listchars.*)" line=\1 backrefs=true' #ou en recopiant la ligne entière -ansible -i hosts -o -u root all -m lineinfile -a 'path=/etc/vimrc regex="^\"set list listchars.*" line=set list listchars=nbsp:▶,tab:··,trail:¤,extends:▶,precedes:◀' +ansible -o all -m lineinfile -a 'path=/etc/vimrc regex="^\"set list listchars.*" line=set list listchars=nbsp:▶,tab:··,trail:¤,extends:▶,precedes:◀' ``` ## Planifier l'exécution de la tâche "_Exécuter des scripts à distance avec command_" toutes les semaines le dimanche soir avec cron ```bash -ansible -i hosts -o -u root -m cron -a 'name="update starship" cron_file="starship" hour="12" minute="01" weekday="sun" job="sh /tmp/install.sh -y" user="root' +ansible -o -m cron -a 'name="update starship" cron_file="starship" hour="12" minute="01" weekday="sun" job="sh /tmp/install.sh -y" user="root' ``` ```bash # avec distribution horaire aléatoire ⚠️ principe d'indempotence non respecté -ansible -i hosts -o -u root -m cron -a 'name="update starship" cron_file="starship" hour="12" minute="{{ 59 | random() }}" weekday="sun" job="sh /tmp/install.sh -y" user="root"' +ansible -o -m cron -a 'name="update starship" cron_file="starship" hour="12" minute="{{ 59 | random() }}" weekday="sun" job="sh /tmp/install.sh -y" user="root"' ``` ```bash # avec distribution horaire aléatoire principe d'indempotence respecté -ansible -i hosts -o -u root -m cron -a 'name="update starship" cron_file="starship" hour="12" minute="{{ 59 | random(seed=inventory_hostname) }}" weekday="sun" job="sh /tmp/install.sh -y" user="root"' +ansible -o -m cron -a 'name="update starship" cron_file="starship" hour="12" minute="{{ 59 | random(seed=inventory_hostname) }}" weekday="sun" job="sh /tmp/install.sh -y" user="root"' ```