2016-07-08 7 views
1

私は、スワッチconfファイルをいくつか構築して、さまざまな動作を可能にするためにできるだけ柔軟にしようとしています。私は.j2ファイルのネストされたループに立ち往生しています。タスクが実際にファイルを作成するん可能性のあるJinjaテンプレートの入れ子ループ

swatch_files: 
    - name: 'syslog' 
    tail: '/var/log/syslog' 
    watchfor: 
     - 
     string: 'Stupid FIrst String' 
     actions: 
      - 
      action: "1/2 Slack blah blah action" 
      threshold: "Threshold For This first Action" 
     actions: 
      - 
      action: "2/2 Slack blah blah action" 
      threshold: "Threshold For This second Action" 
     - 
     string: 'Crappy Second String' 
     actions: 
      - 
      action: "1/2 Slack blah blah action" 
      threshold: "Threshold For This 1 Action" 
     actions: 
      - 
      action: "2/2 Slack blah blah action" 
      threshold: "Threshold For This 2 Action" 

- name: Swatch | Create the Monit swatch conf files template: 
    src="swatch.monit.j2" 
    dest="/etc/monit/conf.d/{{ item.name }}.conf" 
    owner=root 
    group=root 
    mode=0700 with_items: swatch_files tags: 
    - monit 

をそして、私のswatch.conf.j2ファイルは次のようになります。私はAnsibleはとても似ていvarsの

{% for watchfor in item.watchfor recursive %} 
    watchfor /{{ watchfor.string }}/ 
{% for actions in watchfor.actions %} 
     Action: {{ actions.action }} 
     Threshold: {{ actions.threshold }} 
{% endfor %} 
{% endfor % 

しかし、私の/etc/swatch/syslog.confは次のようになります:

watchfor /Stupid FIrst String/ 
    Action: 2/2 Slack blah blah action 
    Threshold: Threshold For This second Action 

    watchfor /Crappy Second String/ 
    Action: 2/2 Slack blah blah action 
    Threshold: Threshold For This 2 Action 

これはitem.watchforwatchfor recursive%}ループの{watchforfor%}を通過しますが、watchfor.actions%内のアクションの%は何とか間違っています。それは、第2のアクションおよび閾値を書き込むだけで終わる。私はそれが最初に上書きされると思いますか?

答えて

1

純粋なYAML問題のように見えます。あなたは辞書内のキーactionsを上書きされています

watchfor: 
    - 
    string: 'Stupid FIrst String' 
    actions: 
     - 
     action: "1/2 Slack blah blah action" 
     threshold: "Threshold For This first Action" 
    actions: 
     - 
     action: "2/2 Slack blah blah action" 
     threshold: "Threshold For This second Action" 

あなたactionsリストは、それは次のようになります複数のアイテムが必要です場合:確かに私の問題だった

watchfor: 
    - string: 'Stupid FIrst String' 
    actions: 
     - action: "1/2 Slack blah blah action" 
     threshold: "Threshold For This first Action" 
     - action: "2/2 Slack blah blah action" 
     threshold: "Threshold For This second Action" 
+0

おかげで、@udondanを、。 – Blake

関連する問題