Here is what I am doing.
When looking below this is the line where it will need fixing, I am also open to other ways to write this as well
{% set shopt_option_value = salt['pillar.get']('user_management:bash_configurations:global:bashrc:{{ shopt_option }}') %}
There is a ticket on stackoverflow where they nest a variable in a variable which come close, but doesnt working when inside a call to a salt function. That stackoverflow url is Python (Jinja2) variable inside a variable
Here is the loop:
{# Loop through all global shopt option key names and set values accordingly #}
{% for shopt_option in salt['pillar.keys']('user_management:bash_configurations:global:bashrc') %}
{% if 'shopt_' in shopt_option %}
{% set shopt_option_value = salt['pillar.get']('user_management:bash_configurations:global:bashrc:{{ shopt_option }}') %}
{% if shopt_option_value == 'True' %}
shopt -s {{ shopt_option|replace("shopt_","") }}
{% elif shopt_option_value == 'False' %}
shopt -u {{ shopt_option|replace("shopt_","") }}
{% elif shopt_option_value == 'default' %}
# {{ shopt_option|replace("shopt_","") }} OS Default implied
{% endif %}
shopt_option_value is {{ shopt_option_value }} for debugging this
{% endif %}
{% endfor %}
Here is the pillar yaml data structure snippet:
user_management:
bash_configurations:
global:
bashrc:
system_reserved_uid: 199
system_reserved_umask: '077'
non_system_reserved_umask: '077'
shopt_autocd: default
shopt_cdable_vars: default
shopt_cdspell: False
shopt_checkhash: default
I found a better way to iterate through the pillars key names and or values.
Solution:
{%- for shopt_option_key in pillar['user_management']['bash_configurations']['global']['bashrc'] %}
seems is alternate way to say:
Original attempt:
{% for shopt_option in salt['pillar.keys']('user_management:bash_configurations:global:bashrc') %}
However in this way literal values are taken with single quotes and without the quotes in a jinja2 statement as you would expect it references the value of the jinja2 variable.
There were many other ways to solve this, I was looking at, this was just one of the way and maybe not the most elegant, so I would consider this a good solution (not the worst I found and not the best).
Here was the final solution:
{#- Loop through all global shopt option key names and set values accordingly #}
{%- for shopt_option_key in pillar['user_management']['bash_configurations']['global']['bashrc'] %}
{%- set shopt_option_value = pillar['user_management']['bash_configurations']['global']['bashrc'][shopt_option_key] %}
{%- if 'shopt_' in shopt_option_key %}
{%- if shopt_option_value %}
shopt -s {{ shopt_option_key|replace("shopt_","") }}
{%- elif shopt_option_value == False %}
shopt -u {{ shopt_option_key|replace("shopt_","") }}
{%- elif shopt_option_value == 'default' %}
# {{ shopt_option_key|replace("shopt_","") }} OS Default implied
{%- endif %}
{%- endif %}
{%- endfor %}
Some additional notes of improvement here: