0
以下是我正在做的事情。如何在jinja2語句中調用salt函數時將jinja2變量用作arg
- 我通過支柱鍵值循環和分配變量shopt_option
- 如果字符串「shopt_」是可變shopt_option的價值,它運行一個嵌套的if語句
- 然後裏面的Jinja2封裝語句我需要設置一個變量來等於調用salt函數的輸出。
- 該函數的其中一個參數也需要是來自循環的變量。
- 我已經嘗試了2個小時的不同方法來編寫這個,我將在下面粘貼的東西當然是錯誤的語法,但有助於說明我想要做什麼,因爲我知道在jinja2語句中引用一個變量時,你不會封裝它在雙重曲別針。
- 然而,作爲括號內的arg似乎搞砸了使用jinja2變量的任何方式。
當尋找下面這是它需要固定的路線,我也開放給其他的方式來寫這也
{% set shopt_option_value = salt['pillar.get']('user_management:bash_configurations:global:bashrc:{{ shopt_option }}') %}
上有計算器一票,他們窩變量在接近的變量中,但在調用鹽函數時不起作用。 這計算器網址是Python (Jinja2) variable inside a variable
這裏是循環:
{# 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 %}
這裏是支柱YAML數據結構片段:
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
我不認爲設置您的店鋪字段之一'默認'將按預期工作。使用您的解決方案,即使系統默認設置被禁用,也會始終啓用選項設置爲「默認」,因爲如果支柱shopt_option爲「shopt_option_value」將設置爲「default」並且「if'default」評估爲「True」 – Travv15
如果shopt_option%}的計算結果爲true,{%if shopt_option == True%}的計算結果爲true,但似乎{%if shopt_option =='True'%}將其視爲字符串True並非boolean True – Dresden
啊,你可能在這一行{% - 如果shopt_option_value%}有一個觀點,任何定義的支柱將評估爲真,所以默認if語句永遠不會被擊中,我可能會在上週編輯該線,我會看到爲了完美,我決定併發布更正。謝謝你的收穫。 – Dresden