Outils pour utilisateurs

Outils du site


blog

Ansible inventaire script - inventaire dynamique vers statique - Conversion JSON YAML INI

INI vers JSON

Pour convertir un inventaire Ansible INI vers JSON

ansible-inventory -i inventory.ini --list > inventory.json

INI vers YAML

Pour convertir un inventaire Ansible de INI vers YAML

ansible-inventory -i inventory.ini -y --list > inventory.yml

JSON vers YAML

cat.py

#! /usr/bin/env python3
 
import os
 
filename = os.environ["FILENAME"]
 
with open(filename, "r") as f:
    print(f.read())
env FILENAME=inventaire.json ansible-inventory -i cat.py -y --list> inventaire.yml

JSON vers INI

Reformater l'inventaire JSON

env FILENAME=inventaire.json ansible-inventory -i cat.py --list> inventaire2.json

Script de conversion json_to_ini.py

json_to_ini.py

#! /usr/bin/env python3
 
import sys
import json
import configparser
 
import argparse
 
parser = argparse.ArgumentParser(
    epilog="""Convert Ansible JSON inventory file to INI.
JSON inventory must be in ansible-inventory format.
"""
)
parser.add_argument("-i", help="Input JSON file")
parser.add_argument("-o", help="Output INI file")
args = parser.parse_args()
 
 
def convert_json2ini():
    config_object = configparser.ConfigParser(delimiters=" ")
 
    with open(args.i, "r") as json_file:
        python_dictionary = json.load(json_file)
 
    python_dictionary["all"]["children"].remove("ungrouped")
 
    for section in python_dictionary["all"]["children"]:
        config_object.add_section(section)
        for options in python_dictionary[section]["hosts"]:
            suboptions = []
            for subsection in python_dictionary["_meta"]["hostvars"][options]:
                subkey = str(subsection)
                subvalue = (
                    '"'
                    + str(python_dictionary["_meta"]["hostvars"][options][subkey])
                    + '"'
                )
                suboptions.append("=".join([subkey, subvalue]))
                key = " ".join(suboptions)
            config_object.set(section, options, key)
 
    with open(args.o, "w") as ini_file:
        config_object.write(ini_file)
 
 
def main():
    if args.i and args.o:
        convert_json2ini()
        sys.exit(0)
 
 
if __name__ == "__main__":
    main()
./json_to_ini.py -i inventaire2.json -o inventaire.ini
2025/03/24 15:06

Grub mot de passe

Protéger GRUB par un mot de passe

RedHat
grub2-set-password

Ce script bash va appeler grub2-mkpasswd-pbkdf2

Ce qui crée le fichier suivant

/boot/grub2/user.cfg

GRUB2_PASSWORD=grub.pbkdf2.sha512.10000.92613724ED40696F3D7917C8F257901BDB39790E6F7DB78830E094D8A60EA6E1DAE7366A64866BCBCD8F9A8FAA6A508DAA560DB3B89A682F58E8D8EB56FC5DE7.95FD391CF51F57EA5CCDC6143B5F9262785DA96857368E40FE4C4710D19D07F513333568B7897B75EF9375FA13395335A823AAEE35A89964022810A72446679C

L'utilisateur est : root

Note : pbkdf2 voir ⇒ https://mjg59.dreamwidth.org/66429.html If the PBKDF is either “pbkdf2” or “argon2i” you should convert to “argon2id”

Debian Jessie (8)

Générer le hash du mot de passe

grub-mkpasswd-pbkdf2
Entrez le mot de passe :
Entrez de nouveau le mot de passe :
Le hachage PBKDF2 du mot de passe est grub.pbkdf2.sha512.10000.F272C7B98DD8F942A27F30637F34D75B9EBC97157E9DB19679D12C22C627DEAE383B2A5AFFC3432457C52EAFA99ACF539C470B1B3631DDA3658F565FCB90B461.8455EB485F5AA06864E7632C698D4B363457B7DD0FC8733F23AF39B90589D0ABAA9D87B134C91C8201AA567F5D8030E2BEA9D4717AA9C6E575253BB099335931

Ajouter les lignes suivantes

/etc/grub.d/40_custom

set superusers="admin"
password_pbkdf2 admin grub.pbkdf2.sha512.10000.F272C7B98DD8F942A27F30637F34D75B9EBC97157E9DB19679D12C22C627DEAE383B2A5AFFC3432457C52EAFA99ACF539C470B1B3631DDA3658F565FCB90B461.8455EB485F5AA06864E7632C698D4B363457B7DD0FC8733F23AF39B90589D0ABAA9D87B134C91C8201AA567F5D8030E2BEA9D4717AA9C6E575253BB099335931

Mise à jour la conf

update-grub
Debian Wheezy (7)

A tester la même manip que pour Debian Jessie. Mais apparemment la manip diffère.

/etc/grub.d/00_header

cat << EOF
set superusers="admin"
password_pbkdf2 admin grub.pbkdf2.sha512.10000.F272C7B98DD8F942A27F30637F34D75B9EBC97157E9DB19679D12C22C627DEAE383B2A5AFFC3432457C52EAFA99ACF539C470B1B3631DDA3658F565FCB90B461.8455EB485F5AA06864E7632C698D4B363457B7DD0FC8733F23AF39B90589D0ABAA9D87B134C91C8201AA567F5D8030E2BEA9D4717AA9C6E575253BB099335931
EOF

Mise à jour la conf

update-grub

Reset password

RedHat / CentOS
  1. Rebooter le serveur.
  2. Sélectionner la ligne du grub souhaité en la mettant en surbrillance.
  3. Éditer le grub en tapant e.
  4. Modifier la ligne de contenant Linux16, ajouter rd.break enforcing=0 à la fin de la ligne
  5. Lancer l’exécution de la ligne modifié en tapant ctrl + x
  6. A l'invite root, remonter /sysroot en lecture/ecriture : mount -o remount,rw /sysroot
  7. chroot /sysroot
  8. passwd root (pour changer le mot de passe)
  9. touch /.autorelabel (créer autorelabel caché pour reetiqueter les contextes selinux correctement).
  10. /sbin/reboot -f
2025/03/24 15:06

Grub efi2bios boot RedHat7 UEFI EFI migration vers BIOS P2V

Voilà que je “déghost” ma belle image système dans une VM et ca ne boot pas.

La machine était en UEFI avec des partitions GPT.

Solution : Garder GPT (ne pas passer en MBR), install Grub2 (en plus de Grug2-efi)

Faire l'inverse : (passer à UEFI) :

Depuis une machine en UEFI

yum install grub2
#cp -p /boot/efi/EFI/redhat/grub.cfg /boot/grub2/grub.cfg
grub2-mkconfig -o /boot/grub2/grub.cfg
sed -i -e 's/linuxefi/linux/g' /boot/grub2/grub.cfg
sed -i -e 's/initrdefi/initrd/g' /boot/grub2/grub.cfg
grub2-install --modules=part_gpt --target=i386-pc --recheck --boot-directory=/boot /dev/sda --force

Depuis une machine en mode BIOS

Par exemple, sur la VM en bootant sur le DVD de RedHat en mode rescue

rpm -Uvh grub2-2.02-0.29.el7.x86_64.rpm
#cp -p /boot/efi/EFI/redhat/grub.cfg /boot/grub2/grub.cfg
grub2-mkconfig -o /boot/grub2/grub.cfg
grub2-install --modules=part_gpt --target=i386-pc --recheck --boot-directory=/boot /dev/sda --force
2025/03/24 15:06

Grub boot migration BIOS vers UEFI EFI en MBR

Voir :

Voir aussi :

  • systemd-boot (Remplace Grub pour les machines compatibles EFI)

Avant toute chose sauvegarder !

Il existe une version de Grub pour DOS/MBR : grub-pc
Il existe une version de Grub pour EFI/MBR : grub-efi
Les deux peuvent être installés sur la même machine.

Testé sous Debian

Création d'une partition /boot/efi en FAT de type “ef” Sur une partition primaire (je n'ai pas testé sur une partition logique) (Taille minimum recommandée : 1MB)

Installation prérequis

#apt-get install efivar syslinux-efi
apt-get install efibootmgr grub-efi dosfstools

Pour Debian Wheezy, une façon de faire est de mettre à jour Grub en prenant les paquets de Jessie :

  • grub2-common
  • grub-common
  • grub-efi
  • grub-efi-amd64
  • grub-efi-amd64-bin

Partitionnement

Créer une partion de VFAT de 20M (1M minimum conseillé) de type “ef” Pour cela il peut être nécessaire de réduire la partition principale à l'aide de GParted (inclus dans systemrescuecd, taper startx pour lancer l’interface graphique)

Ou avec fdisk

fdisk /dev/sda
# Part type id (Hex code) : "ef"  EFI (FAT-12/16/

Formatage / Montage partition EFI

mkfs.vfat /dev/sda4
mkdir -p /boot/efi

Trouver le UUID de le partition EFI.

fdisk -l |grep EFI
blkid /dev/sda4

Remplacer B24E-9BC6 par votre UUID

/etc/fstab
#/dev/sda4
#UUID=B24E-9BC6    /boot/efi            vfat    defaults,uid=0,gid=0,umask=0077,shortname=winnt 0 0
UUID=8343-3610  /boot/efi       vfat    umask=0077      0       1

Montage de la partition EFI crée.

mount /boot/efi

Installation Grub

grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck /dev/sda

Boot auto

Pour booter automatiquement sur Grub

Par défaut le système s'attend à démarrer sur :

  • /boot/efi/EFI/boot/bootx64.efi
  • /boot/efi/EFI/BOOT/BOOTX64.efi
cd /boot/efi/EFI
mkdir boot
cp -p debian/grubx64.efi boot/bootx64.efi

Autres

A tester

mkdir -p /boot/grub/locale
cp /usr/share/locale/en\@quot/LC_MESSAGES/grub.mo /boot/grub/locale/en.mo

Pb

Err failed to register the EFI boot entry: Invalid argument.
# grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck /dev/sda 
Installing for x86_64-efi platform.
grub-install: warning: Cannot read EFI Boot* variables.
grub-install: warning: efivarfs_get_variable: read failed: Invalid argument.
grub-install: warning: efi_get_variable: ops->get_variable failed: Invalid argument.
grub-install: error: failed to register the EFI boot entry: Invalid argument.
Solution

Ajouter --no-nvram ou --removable à grub-install

grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck /dev/sda --removable
2025/03/24 15:06

Grep trouver tous les fichiers ne contenant pas un motif

find . -type f -iname "*.j2" -exec grep -L -e "ansible_managed" {} \;
2025/03/24 15:06
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki