Skip to main content

Worked example: replacing a real plugin

templates/df_used.yml sustituyereplaces al plugin de agentethe pandora_df_used_go. Elagent patrónplugin. aThe aprender:pattern to learn: unaone ejecuciónexecution → array deof objetosobjects → N módulosmodules.

tasks:
  - name: df
    type: local
    local: { command: "df -kTP" }
    filter:
      type: regexp
      # GruposNamed congroups: nombre: cada fila deeach df serow convierte enbecomes {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

LaThe regexp capturacaptures todasall lasdf filasrows; dethe df; la lista permitida dedefault fstype porallow-list defectolives vive enin when: paraso queruntime losparams parámetroscan enoverride tiempoit. deNote ejecuciónthe puedanfolded sobrescribirla. Observa el escalar plegadoscalar (when: >) yand lathe barradoubled invertida duplicadabackslash (\\d): los literales de cadena de expr procesanstring literals process escapes, así queso \\d entregadelivers una verbatim \d literalto althe motorregexp de regexp.engine.

EjemploPositional decaptures capturas posicionalesexample

ÚsaloUse cuandothis quieraswhen elyou ordenwant decapture capturaorder eninstead lugarof defield nombres de campo:names:

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 deHTTP API HTTPexample

templates/http_api_example.yml demuestrademonstrates eltoken-auth encadenamientochaining: con autenticación por token: una peticióna login almacenarequest elstores tokenthe desession sesión,token, yand laslater peticionesrequests posterioresembed loit incorporan mediantevia Authorization: "Bearer {{ token }}".