Creating your own templates
UnaA plantillatemplate esis solojust YAML. ConstrúyelaBuild deit arriba a abajo:top-down: decide quéwhat medirto measure (tareas)tasks), cómohow extraerto losextract númerosthe numbers (filtros)filters), quéwhich módulosmodules exponerto expose (agentes/módulos)agents/modules), yand cómohow entregarto deliver (transferencia)transfer).
PasoStep 1 — ParteStart defrom una ejemploshipped incluidoexample
templates/local_commands_example.yml esis lathe plantillashortest completacomplete mástemplate cortaand ydemonstrates demuestraall lasthree tresregexp formasresult deshapes. resultadoCopy deit regexp.and Cópialareplace ythe sustituye las tareas:tasks:
cp templates/local_commands_example.yml templates/my_template.yml
PasoStep 2 — EscribeWrite lasthe tareastasks
CadaEach tareatask ejecutaruns unone comandocommand oor una peticiónone HTTP yrequest almacenaand sustores salidaits (opcionalmenteoptionally filtrada)filtered) enoutput unain a variable.
tasks:
# UnA escalar:scalar: unone valor,value, sinno filtrofilter (setrimmed almacenaraw laoutput salidais bruta recortada)stored).
- name: uptime_seconds
type: local
local: { command: "awk '{print int($1)}' /proc/uptime" }
variable: uptime
# UnA escalarscalar mediantevia unaa regexpsingle-capture con una sola captura.regexp.
- name: load_avg
type: local
local: { command: "cat /proc/loadavg" }
filter:
type: regexp
expression: '^(\d+\.\d+)'
variable: load1
# UnAn array deof objetosobjects mediantevia gruposnamed decapture captura con nombre.groups.
- name: df
type: local
local: { command: "df -kTP" }
filter:
type: regexp
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
PasoStep 3 — ConvierteTurn variables eninto módulosmodules
UnaA variable escalarthat alimentais directamentea unscalar módulo;feeds unone module directly; an array alimenta bloquesfeeds for_each blocks (unone módulomodule porper elemento)element).
modules:
- name: "Uptime seconds"
type: generic_data
data: "{{ uptime }}"
- name: "Load average 1m"
type: generic_data
data: "{{ load1 }}"
# UnOne módulomodule porper sistema de archivos;filesystem; value.* leereads losthe gruposnamed decapture captura con nombre.groups.
- for_each: filesystems
when: 'value.fstype != "tmpfs"'
name: "DiskUsed_{{ value.mount }}"
type: generic_data
data: "{{ value.pct }}"
unit: "%"
DentroInside de un bloquea for_each: block:
-
{{ value }}esiseltheelementocurrentactual,element,{{ index }}suitsposición0-based(base 0).position. LosNamedgruposgroupsconmakenombreeachhacenelementque cada elemento sea unanobjetoobject →accedeaccessafieldslos campos comoas{{ value.mount }}.- 2+
gruposunnamedsingroupsnombremakehaceneachqueelementcada elemento sea una positional arrayposicional→accedeaccesscomoas{{ value[0] }},{{ value[1] }}, ...;escribewritevariosseveralbloquesblocksconwithelthemismosamefor_eachparatoemitiremitvariosseveralmódulosmodulesporperelemento.element.
PasoStep 4 — EligePick elthe modotransfer de transferenciamode
transfer:
mode: agent_plugin # salidamodules-only solooutput; derun módulos;as ejecutara comosoftware-agent plugin de agente de software
transfer:
mode: tentacle # full agent XML de agente completoto a unPandora servidor Pandoraserver
tentacle: { address: "pandora.example.com" }
transfer:
mode: local # full agent XML dewritten agenteto completoa escrito en un directoriodirectory
local: { directory: "/var/spool/pandora/data_in" }
PasoStep 5 — ValidaValidate antesbefore de entregarshipping
pandora-plugin_exec -t templates/my_template.yml --dry-run -v
--dry-run imprimeprints elthe generated XML generadowithout sin transferir;transferring; -v muestrashows quéwhich variables sewere almacenaronstored yand quéwhich módulosmodules sewere omitieron.skipped. ElThe parser esis estricto,strict, asíso queany cualquiertypo erratain ena unfield nombrename defails campo falla inmediatamente.immediately.
ErroresCommon comunespitfalls
-
LosFlagsflagsafterdespuésargumentsdedon'tlos argumentos no funcionan.work.ElGoparseoflagdeparsingflagsstopsseatdetienetheenfirstelnon-flagprimertoken:token que no es flag: ponput--dry-runantesbeforedeanycualquierpositionalargumento posicional.argument. -
LasUndefined variablesnoskipdefinidastheomiten el módulomodule,notheyrenderizandon'tvacío.renderUsaempty. Use{{ params.x ?? "default" }}oorwhen: "'x' in params"paraforparámetrosoptionalopcionales.parameters. -
filter.expressionnoisesnot expr.EsIt is jqoorunaan RE2 regexpRE2dependingsegúnonfilter.type. exprsoloisseonlyusauseddentro deinside{{ }}yandeninwhen:. -
DuplicaDoublelathebarrabackslashinvertida en literalesin regexpdentroliteralsdeinsidewhen:—los literales de cadena deexprprocesanstring literals process escapes,así queso\\dentregadelivers\daltomotorthederegexpregexp.engine. -
RE2 has no
tienelookahead/backtracking —usausewhen:paraforexclusionesexclusions (p. ej.e.g.when: 'not (value.mount matches "^/DB")').