Proxmox: Gestión con scrypt Python Proxmoxer
Os explicamos como automatizar procesos en Proxmox a través de Python y del proyecto Proxmoxer.
Dejo el enlace del mismo: https://pypi.org/project/proxmoxer/#description
Reutilizaré la máquina de Terraform que me permite ejecutar en remoto comandos igualmente, y que hasta ahora lanzaba objetos sobre un clúster VMware vSphere. Como ya he comentado alguna vez dispongo adicionalmente de un clúster Proxmox de 3 nodos físicos. La idea es poder lanzar desde una máquina virtual del propio clúster automatizaciones de máquinas virtuales y LXC.
En este caso, lo haremos con Python, pero lo intentaremos con Terraform, Ansible, y otros proyectos viendo cual es el mejor para lo que queremos hacer.
Instalación Proxmoxer (PIP) en Centos 7
Comprobamos que no tenemos pip instalado (que será lo normal):
[root@TERRAFORM ~]# pi
pidof pinentry-curses ping6 pivot_root
pinentry ping pinky
Os muestro mi versión de mi LXC, en Centos 7:
[root@TERRAFORM ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
Necesitamos instalar el repo EPEL, así que lo instalamos si no lo tenemos ya:
[root@TERRAFORM ~]# yum install epel-release
Loaded plugins: fastestmirror
Determining fastest mirrors
epel/x86_64/metalink | 27 kB 00:00:03
* base: mirror.airenetworks.es
* epel: mirror.uv.es
* extras: mirror.gadix.com
* updates: mirror.airenetworks.es
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/7): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/7): epel/x86_64/group_gz | 95 kB 00:00:00
(3/7): base/7/x86_64/primary_db | 6.1 MB 00:00:00
(4/7): epel/x86_64/updateinfo | 1.0 MB 00:00:01
(5/7): updates/7/x86_64/primary_db | 176 kB 00:00:00
(6/7): extras/7/x86_64/primary_db | 190 kB 00:00:00
(7/7): epel/x86_64/primary_db | 6.8 MB 00:00:02
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be updated
---> Package epel-release.noarch 0:7-12 will be an update
--> Finished Dependency Resolution
...
Lanzo la instalación de PIP para Centos 7:
[root@TERRAFORM ~]# yum -y install python-pip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.airenetworks.es
* epel: mirror.uv.es
* extras: mirror.gadix.com
* updates: mirror.airenetworks.es
Resolving Dependencies
--> Running transaction check
---> Package python2-pip.noarch 0:8.1.2-12.el7 will be installed
--> Processing Dependency: python-setuptools for package: python2-pip-8.1.2-12.el7.noarch
--> Running transaction check
---> Package python-setuptools.noarch 0:0.9.8-7.el7 will be installed
--> Processing Dependency: python-backports-ssl_match_hostname for package: python-setuptools-0.9.8-7.el7.noarch
--> Running transaction check
...
Complete!
Intento instalar proxmoxer:
[root@TERRAFORM ~]# pip install proxmoxer
Collecting proxmoxer
Downloading https://files.pythonhosted.org/packages/fa/3e/1a0cc0fd8c676e0b3156d92f7e69420e6d1d25a01db5057899461e83bf88/proxmoxer-1.0.4.tar.gz
Installing collected packages: proxmoxer
Running setup.py install for proxmoxer ... done
Successfully installed proxmoxer-1.0.4
You are using pip version 8.1.2, however version 20.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Me pide considerar hacer un upgrade de pip (y donde hay patrón…):
[root@TERRAFORM ~]# pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/54/2e/df11ea7e23e7e761d484ed3740285a34e38548cf2bad2bed3dd5768ec8b9/pip-20.1-py2.py3-none-any.whl (1.5MB)
100% |################################| 1.5MB 1.0MB/s
Installing collected packages: pip
Found existing installation: pip 8.1.2
Uninstalling pip-8.1.2:
Successfully uninstalled pip-8.1.2
Successfully installed pip-20.1
Hacemos un upgrade de paquetes:
[root@TERRAFORM ~]# yum -y update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.airenetworks.es
* epel: mirror.uv.es
* extras: mirror.gadix.com
* updates: mirror.airenetworks.es
Resolving Dependencies
--> Running transaction check
---> Package acl.x86_64 0:2.2.51-14.el7 will be updated
....
Complete!
Intentamos instalar proxmoxer:
[root@TERRAFORM ~]# pip install proxmoxer
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Requirement already satisfied: proxmoxer in /usr/lib/python2.7/site-packages (1.0.4)
Could not build wheels for proxmoxer, since package 'wheel' is not installed.
En mi caso, tengo que instalar antes wheel:
[root@TERRAFORM ~]# pip install wheel
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting wheel
Downloading wheel-0.34.2-py2.py3-none-any.whl (26 kB)
Installing collected packages: wheel
Successfully installed wheel-0.34.2
Ya podemos instalar proxmoxer:
[root@TERRAFORM ~]# pip install proxmoxer
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Requirement already satisfied: proxmoxer in /usr/lib/python2.7/site-packages (1.0.4)
[root@TERRAFORM ~]# pip install requests
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting requests
Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB)
|################################| 58 kB 2.5 MB/s
Collecting certifi>=2017.4.17
Downloading certifi-2020.4.5.1-py2.py3-none-any.whl (157 kB)
|################################| 157 kB 10.9 MB/s
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
Downloading urllib3-1.25.9-py2.py3-none-any.whl (126 kB)
|################################| 126 kB 90.8 MB/s
Collecting idna<3,>=2.5
Downloading idna-2.9-py2.py3-none-any.whl (58 kB)
|################################| 58 kB 11.8 MB/s
Collecting chardet<4,>=3.0.2
Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
|################################| 133 kB 93.8 MB/s
Installing collected packages: certifi, urllib3, idna, chardet, requests
Successfully installed certifi-2020.4.5.1 chardet-3.0.4 idna-2.9 requests-2.23.0 urllib3-1.25.9
Instalamos paramiko:
[root@TERRAFORM ~]# pip install paramiko
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Collecting paramiko
Downloading paramiko-2.7.1-py2.py3-none-any.whl (206 kB)
|################################| 206 kB 3.5 MB/s
Collecting cryptography>=2.5
Downloading cryptography-2.9.2-cp27-cp27mu-manylinux2010_x86_64.whl (2.7 MB)
|################################| 2.7 MB 11.2 MB/s
Collecting pynacl>=1.0.1
Downloading PyNaCl-1.3.0-cp27-cp27mu-manylinux1_x86_64.whl (762 kB)
|################################| 762 kB 38.2 MB/s
Collecting bcrypt>=3.1.3
Downloading bcrypt-3.1.7-cp27-cp27mu-manylinux1_x86_64.whl (59 kB)
|################################| 59 kB 14.1 MB/s
Collecting six>=1.4.1
Downloading six-1.14.0-py2.py3-none-any.whl (10 kB)
Collecting cffi!=1.11.3,>=1.8
Downloading cffi-1.14.0-cp27-cp27mu-manylinux1_x86_64.whl (387 kB)
|################################| 387 kB 63.4 MB/s
Requirement already satisfied: ipaddress; python_version < "3" in /usr/lib/python2.7/site-packages (from cryptography>=2.5->paramiko) (1.0.16)
Collecting enum34; python_version < "3"
Downloading enum34-1.1.10-py2-none-any.whl (11 kB)
Collecting pycparser
Downloading pycparser-2.20-py2.py3-none-any.whl (112 kB)
|################################| 112 kB 46.0 MB/s
Installing collected packages: six, pycparser, cffi, enum34, cryptography, pynacl, bcrypt, paramiko
Successfully installed bcrypt-3.1.7 cffi-1.14.0 cryptography-2.9.2 enum34-1.1.10 paramiko-2.7.1 pycparser-2.20 pynacl-1.3.0 six-1.14.0
Podéis comprobar los módulos cargados:
[root@TERRAFORM ~]# pip freeze
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
backports.ssl-match-hostname==3.5.0.1
bcrypt==3.1.7
certifi==2020.4.5.1
cffi==1.14.0
chardet==3.0.4
cryptography==2.9.2
enum34==1.1.10
idna==2.9
iniparse==0.4
ipaddress==1.0.16
paramiko==2.7.1
proxmoxer==1.0.4
pycparser==2.20
pycurl==7.19.0
pygpgme==0.3
pyliblzma==0.5.3
PyNaCl==1.3.0
pyxattr==0.5.1
requests==2.23.0
six==1.14.0
urlgrabber==3.10
urllib3==1.25.9
yum-metadata-parser==1.1.4
Obtener máquinas virtuales por script en Proxmox
Si queremos usar nuestros usuarios linux:
[root@TERRAFORM ~]# nano proxmoxer-getnodes.py
1 2 3 4 5 6 7 8 9 |
from proxmoxer import ProxmoxAPI proxmox = ProxmoxAPI('192.168.2.51', user='root@pam', password='CONTRASEÑA', verify_ssl=False) for vm in proxmox.cluster.resources.get(type='vm'): print("{0}. {1} => {2}" .format(vm['vmid'], vm['name'], vm['status'])) |
1 |
from proxmoxer import ProxmoxAPI |
1 2 3 4 5 6 7 |
proxmox = ProxmoxAPI('192.168.2.51', user='raulunzue@pve', password='CONTRASEÑA', verify_ssl=False) for vm in proxmox.cluster.resources.get(type='vm'): print("{0}. {1} => {2}" .format(vm['vmid'], vm['name'], vm['status'])) |
Y para lanzarlo ejecutamos:
[root@TERRAFORM ~]# python proxmoxer-getnodes.py117. ESX702 => stopped
102. CITRIXDC01 => running
116. ESX703 => stopped
103. KUBERNETES00 => stopped
100. CITRIXVDA01 => running
114. ESX701 => stopped
101. BROKER => stopped
112. SOPHOS01 => stopped
107. RDS => stopped
106. NEGUAD01 => running
110. KUBERNETES01 => running
109. KBMASTER => running
113. KBMASTER02 => stopped
108. RDS02 => stopped
105. GUACAMOLE => running
104. TERRAFORM => running
111. KUBERNETES02 => running
Vamos a ver como generar máquinas, apagarlas, encenderlas,…y otras operaciones, en siguientes entradas. Espero os parezca interesante…
¿Te ha gustado la entrada SÍGUENOS EN TWITTER?
Te ha gustado la entrada SGUENOS EN TWITTER O INVITANOS A UN CAFE?