2016-02-15 26 views
0

以下是我正在做的事情。如何在jinja2語句中調用salt函數時將jinja2變量用作arg

  1. 我通過支柱鍵值循環和分配變量shopt_option
  2. 如果字符串「shopt_」是可變shopt_option的價值,它運行一個嵌套的if語句
  3. 然後裏面的Jinja2封裝語句我需要設置一個變量來等於調用salt函數的輸出。
  4. 該函數的其中一個參數也需要是來自循環的變量。
  5. 我已經嘗試了2個小時的不同方法來編寫這個,我將在下面粘貼的東西當然是錯誤的語法,但有助於說明我想要做什麼,因爲我知道在jinja2語句中引用一個變量時,你不會封裝它在雙重曲別針。
  6. 然而,作爲括號內的arg似乎搞砸了使用jinja2變量的任何方式。
  7. 當尋找下面這是它需要固定的路線,我也開放給其他的方式來寫這也

    {% 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 

回答

0

我找到了一個更好的方式,通過支柱鍵名迭代和或值。

解決方案:

{%- for shopt_option_key in pillar['user_management']['bash_configurations']['global']['bashrc'] %} 

似乎是說另一種方法:

原始的嘗試:

{% for shopt_option in salt['pillar.keys']('user_management:bash_configurations:global:bashrc') %} 

然而,在這樣的文字值都採取單引號和沒有引號如你所期望的,jinja2語句引用了jinja2變量的值。

還有很多其他方法可以解決這個問題,我在看,這只是其中一種方式,也許不是最優雅的方式,所以我會認爲這是一個很好的解決方案(不是我發現的最壞的,也不是最好的)。

這裏是最終的解決方案:

{#- 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 %} 

的改善提供了一些說明這裏:

  1. 這裏的減號,{% - 修剪的前導空格,所以我不會有空白行在創建的文件中
  2. 在jinja2中,如果鍵的值設置爲布爾值,則不能在「True」或「False」比較周圍使用引號,因爲它會將比較值視爲字符串,I必須刪除引用來處理它作爲布爾
+0

我不認爲設置您的店鋪字段之一'默認'將按預期工作。使用您的解決方案,即使系統默認設置被禁用,也會始終啓用選項設置爲「默認」,因爲如果支柱shopt_option爲「shopt_option_value」將設置爲「default」並且「if'default」評估爲「True」 – Travv15

+0

如果shopt_option%}的計算結果爲true,{%if shopt_option == True%}的計算結果爲true,但似乎{%if shopt_option =='True'%}將其視爲字符串True並非boolean True – Dresden

+0

啊,你可能在這一行{% - 如果shopt_option_value%}有一個觀點,任何定義的支柱將評估爲真,所以默認if語句永遠不會被擊中,我可能會在上週編輯該線,我會看到爲了完美,我決定併發布更正。謝謝你的收穫。 – Dresden