Ejemplo práctico: sustituir un plugin real
templates/df_used.yml sustituye al plugin de agente pandora_df_used_go. El patrón a aprender: una ejecución → array de objetos → N módulos.
tasks:
- name: df
type: local
local: { command: "df -kTP" }
filter:
type: regexp
# Grupos con nombre: cada fila de df se convierte en {fs, fstype, total, used, pct, mount}
expression: '(?m)^(?P<fs>\S+)\s+(?P<fstype>\S+)\s+(?P<total>\d+)\s+(?P<used>\d+)\s+\d+\s+(?P<pct>\d+)%\s+(?P<mount>.+)$'
variable: filesystems
modules:
- for_each: filesystems
when: >
(value.fstype matches "^(?i:adfs|affs|autofs|btrfs|cifs|coda|coherent|efs|ext\\d?|hfs|hfsplus|hpfs|jfs|minix|msdos|ncpfs|nfs4?|ntfs|proc|qnx4|reiserfs|smbfs|sysv|ubifs|udf|ufs|umsdos|usbfs|vfat|xenix|xfs|xiafs)$"
|| value.mount in split(params.include ?? "", ","))
&& not (value.mount in split(params.exclude ?? "", ","))
name: "DiskUsed_{{ value.mount }}"
type: generic_data
data: "{{ value.pct }}"
unit: "%"
- for_each: filesystems
when: >
(value.fstype matches "^(?i:adfs|affs|autofs|btrfs|cifs|coda|coherent|efs|ext\\d?|hfs|hfsplus|hpfs|jfs|minix|msdos|ncpfs|nfs4?|ntfs|proc|qnx4|reiserfs|smbfs|sysv|ubifs|udf|ufs|umsdos|usbfs|vfat|xenix|xfs|xiafs)$"
|| value.mount in split(params.include ?? "", ","))
&& not (value.mount in split(params.exclude ?? "", ","))
name: "DiskUsed_{{ value.mount }} bytes"
type: generic_data
data: "{{ value.used }}"
unit: "bytes"
transfer:
mode: agent_plugin
La regexp captura todas las filas de df; la lista permitida de fstype por defecto vive en when: para que los parámetros en tiempo de ejecución puedan sobrescribirla. Observa el escalar plegado (when: >) y la barra invertida duplicada (\\d): los literales de cadena de expr procesan escapes, así que \\d entrega un \d literal al motor de regexp.
Ejemplo de capturas posicionales
Úsalo cuando quieras el orden de captura en lugar de nombres de campo:
tasks:
- name: fs
type: local
local:
command: |
printf '/dev/sda1 ext4 40 81 /\n/dev/sdb1 xfs 90 12 /data\n'
filter:
type: regexp
expression: '(?m)^(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(.+)$'
variable: filesystems
modules:
- for_each: filesystems
name: "FS {{ value[0] }}"
type: generic_data
data: "{{ value[3] }}"
unit: "%"
transfer:
mode: agent_plugin
Ejemplo de API HTTP
templates/http_api_example.yml demuestra el encadenamiento con autenticación por token: una petición login almacena el token de sesión, y las peticiones posteriores lo incorporan mediante Authorization: "Bearer {{ token }}".