Ansible: Comandos básicos
En otras entradas ya os hemos explicado como configurar o instalar Ansible, hoy vamos a mostrar los comandos básicos para manejaros si estáis empezando desde cero.
Lo que tenéis que tener claro, como ya hemos explicado otras veces, que necesitáis tener definidos los hosts a “atacar” en el fichero host de Ansible (/etc/ansible/hosts).
Una vez definidos, podemos lanzar comandos tipo:
ansible -m ping all
- Con este comando verificamos que todos los hosts responden al ping
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[root@TERRAFORM ~]# ansible -m ping all [WARNING]: Platform linux on host pve2 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve2 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } [WARNING]: Platform linux on host pve1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } [WARNING]: Platform linux on host pve3 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve3 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } |
ansible all -a "COMANDO A LANZAR" -f 1
- Con este comando lanzaremos en todos los hosts definidos el comando que coloquemos entre las comillas
- El valor “-f”, permite definir a cuantos hosts a la vez vamos a lanzarlo a la vez. Imaginaros que tenéis 3 como yo, ese valor no es significativo, pero cuando tienes 2000, que es donde tienen sentido las automatizaciones, podemos saturar nuestra red, o la de nuestros clientes.
Os dejo algún ejemplo:
ansible all -a "df -h" -f 1
- Con este comando revisamos el espacio en disco de todos los hosts, y lo haremos de uno en uno, con el parámetro “-f 1”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
[root@TERRAFORM playbooks]# ansible all -a "df -h" -f 1 [WARNING]: Platform linux on host pve1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve1 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on udev 32G 0 32G 0% /dev tmpfs 6.3G 98M 6.2G 2% /run /dev/mapper/pve-root 55G 5.6G 47G 11% / tmpfs 32G 63M 32G 1% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 32G 0 32G 0% /sys/fs/cgroup tmpfs 32G 24K 32G 1% /var/lib/ceph/osd/ceph-0 tmpfs 32G 24K 32G 1% /var/lib/ceph/osd/ceph-2 tmpfs 32G 24K 32G 1% /var/lib/ceph/osd/ceph-1 /dev/fuse 30M 40K 30M 1% /etc/pve 192.168.2.109:/volume1/PROXMOX 11T 7.9T 2.7T 75% /mnt/pve/NAS tmpfs 6.3G 0 6.3G 0% /run/user/0 [WARNING]: Platform linux on host pve2 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve2 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on udev 32G 0 32G 0% /dev tmpfs 6.3G 50M 6.3G 1% /run /dev/mapper/pve-root 55G 5.6G 47G 11% / tmpfs 32G 63M 32G 1% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 32G 0 32G 0% /sys/fs/cgroup tmpfs 32G 24K 32G 1% /var/lib/ceph/osd/ceph-6 tmpfs 32G 24K 32G 1% /var/lib/ceph/osd/ceph-8 tmpfs 32G 24K 32G 1% /var/lib/ceph/osd/ceph-7 /dev/fuse 30M 40K 30M 1% /etc/pve 192.168.2.109:/volume1/PROXMOX 11T 7.9T 2.7T 75% /mnt/pve/NAS tmpfs 6.3G 0 6.3G 0% /run/user/0 [WARNING]: Platform linux on host pve3 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve3 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on udev 7.8G 0 7.8G 0% /dev tmpfs 1.6G 81M 1.5G 6% /run /dev/mapper/pve-root 28G 5.6G 21G 22% / tmpfs 7.8G 63M 7.7G 1% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup /dev/fuse 30M 40K 30M 1% /etc/pve tmpfs 7.8G 24K 7.8G 1% /var/lib/ceph/osd/ceph-3 tmpfs 7.8G 24K 7.8G 1% /var/lib/ceph/osd/ceph-5 tmpfs 7.8G 24K 7.8G 1% /var/lib/ceph/osd/ceph-4 192.168.2.109:/volume1/PROXMOX 11T 7.9T 2.7T 75% /mnt/pve/NAS tmpfs 1.6G 0 1.6G 0% /run/user/0 |
ansible all -a "df -h" -f 2
- Con este comando revisamos el hostname de los hosts, y lo haremos de dos en dos, con el parámetro “-f 2”
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@TERRAFORM playbooks]# ansible all -a "hostname" -f 2 [WARNING]: Platform linux on host pve2 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve2 | CHANGED | rc=0 >> pve2 [WARNING]: Platform linux on host pve1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve1 | CHANGED | rc=0 >> pve1 [WARNING]: Platform linux on host pve3 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve3 | CHANGED | rc=0 >> pve3 |
ansible pve1 -m setup
- Con este comando podemos extraer toda la info de un host, donde pve1 es el nombre del host
- Si queremos toda la información de todos los hosts:
ansible all -m setup -f 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
[root@TERRAFORM playbooks]# ansible pve1 -m setup -f 1 | more [WARNING]: Platform linux on host pve1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve1 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "10.20.0.1", "192.168.2.51" ], "ansible_all_ipv6_addresses": [ "fe80::6eb3:11ff:fe1b:a706", "fe80::82ee:73ff:fedb:4cef", "fe80::6eb3:11ff:fe1b:a707" ], "ansible_apparmor": { "status": "enabled" }, "ansible_architecture": "x86_64", "ansible_bios_date": "01/09/2018", "ansible_bios_version": "1.09", "ansible_cmdline": { "BOOT_IMAGE": "/boot/vmlinuz-5.4.65-1-pve", "quiet": true, "ro": true, "root": "/dev/mapper/pve-root" }, "ansible_date_time": { "date": "2020-10-10", "day": "10", "epoch": "1602322277", "hour": "11", "iso8601": "2020-10-10T09:31:17Z", "iso8601_basic": "20201010T113117021279", "iso8601_basic_short": "20201010T113117", "iso8601_micro": "2020-10-10T09:31:17.021331Z", "minute": "31", "month": "10", --More-- |
- Módulo Ansible “cron”:
- Se utiliza para poder programar tareas a través de cron
- Ejemplo (DOCUMENTACION OFICIAL ANSIBLE):
ansible all -u root -m cron -a "name='tarea-cron' hour=5 job='/script-tarea.sh'"
- Módulo Ansible “file”:
- Se utiliza para crear/borrar masivamente directorios, ficheros o enlaces simbólicos. Incluso podemos gestionar permisos
- Ejemplo (DOCUMENTACION OFICIAL ANSIBLE):
- Crear directorio:
ansible all -m file -a "dest=/var/www/prueba mode=644 state=directory"
- Crear fichero:
ansible all -m file -a "dest=/var/www/prueba.html mode=644 state=touch"
- Crear directorio:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@TERRAFORM playbooks]# ansible pve1 -m file -a "dest=/etc/prueba.txt mode=644 state=touch" [WARNING]: Platform linux on host pve1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve1 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/etc/prueba.txt", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0 } |
-
-
- Borrar fichero:
ansible all -m file -a "dest=/var/www/prueba.html mode=644 state=absent"
- Borrar fichero:
-
1 2 3 4 5 6 7 8 9 10 11 |
[root@TERRAFORM playbooks]# ansible pve1 -m file -a "dest=/etc/prueba.txt mode=644 state=absent" [WARNING]: Platform linux on host pve1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve1 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "/etc/prueba.txt", "state": "absent" } |
-
-
- Enlace simbólico:
ansible all -m file -a "srv=/prueba dest=/tmp mode=644 state=link"
- Enlace simbólico:
-
- Módulo Ansible “stat”:
- Se utiliza para ver las estadísticas de ciertas rutas dentro de los servidores:
- Ejemplo (DOCUMENTACION OFICIAL ANSIBLE)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
[root@TERRAFORM playbooks]# ansible pve1 -m stat -a "path=/etc" [WARNING]: Platform linux on host pve1 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. pve1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "stat": { "atime": 1602253762.5567355, "attr_flags": "e", "attributes": [ "extents" ], "block_size": 4096, "blocks": 8, "charset": "binary", "ctime": 1602253683.2757099, "dev": 64772, "device_type": 0, "executable": true, "exists": true, "gid": 0, "gr_name": "root", "inode": 3276801, "isblk": false, "ischr": false, "isdir": true, "isfifo": false, "isgid": false, "islnk": false, "isreg": false, "issock": false, "isuid": false, "mimetype": "inode/directory", "mode": "0755", "mtime": 1602253683.2757099, "nlink": 93, "path": "/etc", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 4096, "uid": 0, "version": "2230499079", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": true, "xoth": true, "xusr": true } } |
- Módulo Ansible “copy”:
- Nos permite copiar ficheros y directorios en los servidores
- Ejemplo:
ansible all -m copy -a "src=/tmp/fichero dest=/root/hosts"
- Módulo Ansible “fetch”:
- Nos permite copiar ficheros y directorios de los servidores a nuestro server de ejecución ansible. Hace lo contrario que Copy, al menos en el origen y destino.
- Ejemplo:
ansible all -m fetch -a "src=/root/hosts dest=/tmp/fichero"
- Módulo Ansible “apt”:
- Nos permite instalar aplicaciones en nuestros servidores
- Ejemplo:
ansible all -u root -m apt -a "name=net-tools state=installed"
- Módulo Ansible “group”:
- Nos permite crear grupos de usuarios en los servidores
- Ejemplo:
ansible all -u root -m group -a "name=proxmoxadmins state=present"
- Módulo Ansible “user”:
- Nos permite crear usuarios en los servidores
- Ejemplo:
ansible all -u root -m user -a "name=elblogdenegu
group=proxmoxadmins createhome=yes"
¿Te ha gustado la entrada SÍGUENOS EN TWITTER?
Te ha gustado la entrada SGUENOS EN TWITTER O INVITANOS A UN CAFE?