大网数据广告活动
服务器运维问答

Ansible loops and template... again

· 52CE 我爱测

I am trying setup a playbook to generalise the setup of logfile centralisation and templates. I want to write rsyslog.d/ config files using ansible vars through a generic template. The vars : vars: loglist: - app_id: 'dsm' rsyslo…

问题原文

原帖链接 · Tomasito · CC BY-SA 4.0

I am trying setup a playbook to generalise the setup of logfile centralisation and templates. I want to write rsyslog.d/ config files using ansible vars through a generic template.

The vars :

  vars:
    loglist:
      - app_id: 'dsm'
        rsyslog_conf_file: '13-dsm.conf'
        ruleset: 'Centralize'
        logfiles:
          - name: /var/opt/ds_agent/diag/amEvent.log
            regex: 'zzz'
          - name: /XXXX/amEvent.log
            regex: 'xxx'
      - app_id: 'audit'
        rsyslog_conf_file: '06-auditd.conf'
        ruleset: 'Centralize'
        logfiles:
          - name: /var/log/audit/audit.log
            regex: ''

the task :

  - name: Rsyslog logs config
    template:
      src: ./templates/rsyslog.d/99-generic-template.conf.j2
      dest: /tmp/{{ item.0.rsyslog_conf_file }}
      owner: root
      group: root
      mode: '0644'
    with_subelements:
      - "{{ loglist }}"
      - logfiles

the template :

{% for val in loglist | subelements('logfiles') %}

input(type="imfile"
    File="{{ val.1.name }}"
    Tag="{{ val.0.app_id }}__"
{% if val.1.regex|length %}
    startmsg.regex="{{ val.1.regex }}"
{% endif %}
    Ruleset="{{ val.0.ruleset }}"
    addMetadata="on")

{% endfor %}

I'm getting close, but the template is going through the entire list, iterating all logfilename each time, instead of going through the current item only.

The result is 2 files with the same content : 13-dsm.conf and 06-auditd.conf :

input(type="imfile"
    File="/var/opt/ds_agent/diag/amEvent.log"
    Tag="dsm__"
    startmsg.regex="a"
    Ruleset="Centralizehorsprod"
    addMetadata="on")


input(type="imfile"
    File="/XXXX/amEvent.log"
    Tag="dsm__"
    startmsg.regex="X"
    Ruleset="Centralizehorsprod"
    addMetadata="on")


input(type="imfile"
    File="/var/log/audit/audit.log"
    Tag="audit__"
    startmsg.regex="hqhq"
    Ruleset="Centralizehorsprod"
    addMetadata="on")

Expected result is 2 files with their respective logfilename.

06-audit.conf

input(type="imfile"
    File="/var/log/audit/audit.log"
    Tag="audit__"
    startmsg.regex="hqhq"
    Ruleset="Centralizehorsprod"
    addMetadata="on")

and 13-dsm.conf

input(type="imfile"
    File="/var/opt/ds_agent/diag/amEvent.log"
    Tag="dsm__"
    startmsg.regex="a"
    Ruleset="Centralizehorsprod"
    addMetadata="on")


input(type="imfile"
    File="/XXXX/amEvent.log"
    Tag="dsm__"
    startmsg.regex="X"
    Ruleset="Centralizehorsprod"
    addMetadata="on")

Thanks

已采纳解答

原帖链接 · Alex Duzsardi · CC BY-SA 4.0

Try this

task

  - name: Rsyslog logs config
    template:
      src: ./templates/99-generic-template.conf.j2
      dest: /tmp/{{ item.rsyslog_conf_file }}
    with_items:
    - "{{ loglist }}"

template

{% for val in item.logfiles %}
input(type="imfile"
    File="{{ val.name }}"
    Tag="{{ item.app_id }}__"
{% if val.regex|length %}
    startmsg.regex="{{ val.regex }}"
{% endif %}
    Ruleset="{{ item.ruleset }}"
    addMetadata="on")
{% endfor %}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-05-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。