Saltstack [SOLVED]: Saltstack – Parsing YAML properties file to set Jinja variables – For loop

Saltstack [SOLVED]: Saltstack – Parsing YAML properties file to set Jinja variables – For loop

Home Forums Automation Tools Saltstack Tutorials Saltstack [SOLVED]: Saltstack – Parsing YAML properties file to set Jinja variables – For loop

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #161742

    Cloudy Point
    Keymaster

    QuestionQuestion

    I try to parse a propertie file with Jinja for setting variables.

    I have mutiple categories and sub-categories in this file like :

    • jboss_container
      • some properties
      • foobar
    • jboss_datasource_install
      • datasource1
        • prop1
        • prop2
      • datasource2
    • jboss_app_deploy
      • app1
      • app2

    The goal is to set variables from each categories and reuse them in for loop.
    –> For Example, i want to install 2 jboss datasources and loop on each sub-categories

    foobar.properties.yml :

    jboss_container:
        jboss_container_values:
          jboss_container_install: 'True'
          jboss_app_name: 'foobar'
          jboss_version: 'x.x.x'
          ...
    
    jboss_datasource_install:
        datasource1:
          jboss_datasource_install: 'True'
          jboss_datasource_name: 'foobar'
          jboss_datasource_jta: 'true'
          ...
        datasource2:
          jboss_datasource_install: 'True'
          ...
    

    init.sls :

    {% import_yaml 'foobar_properties.yml' as jboss_apps %}
    
    % for jboss_container, jboss_container_values in jboss_apps.iteritems() %}
          {% for key, value in jboss_container_values.items() %}
                      {% if value.get('jboss_container_install') != 'False' %}
    
    # Do stuff - {{ value.jboss_app_name }}
    
                      {% endif %}
          {% endfor %}
    {% endfor %}
    
    {% for jboss_datasource_install, jboss_datasource_install_name in jboss_apps.iteritems() %}
          {% for key, value in jboss_datasource_install_name.items() %}
                      {% if value.get('jboss_datasource_install') != 'False' %}
    
    # Do stuff - {{ value.jboss_datasource_install }}
    
                      {% endif %}
          {% endfor %}
    {% endfor %}
    

    Actually occurs this error in Salt :

    Jinja variable ‘dict object’ has no attribute ‘jboss_app_name’

    Any Ideas How to do this?

    Many thanks 🙂

    #161743

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    Your outer loop iterates through jboss_datasource_install and your inner loop through datasource1. Your if then asks for

    value.get('jboss_container_install')
    

    The dict does not contain a key named jboss_container_install, so it returns None. And since that does not equal 'False', Jinja then attempts to insert value.jboss_app_name, which is where the error comes from.

    A simple fix would be to rewrite the if-Statement to:

    {% if value.get('jboss_container_install') == 'True' %}
    

    But your outer loop seems strange anyway. If you only want to iterate through jboss_container, you can replace

    {% for jboss_container, jboss_container_values in jboss_apps.iteritems() %}
      {% for key, value in jboss_container_values.items() %}
    

    with

    {% for key, value in jboss_apps['jboss_container']['jboss_container_values'].iteritems() %}
    

    Source: https://stackoverflow.com/questions/45374961/saltstack-parsing-yaml-properties-file-to-set-jinja-variables-for-loop
    Author: flyx
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.