Question
How do I get the desired output?
Code
yaml
nodeStatusUpdateFrequency:
{% if nodeStatusUpdateFrequency is defined -%}
{{ nodeStatusUpdateFrequency }}
{% else -%}
{%- if nodeStatusUpdate == 'Fast' -%}
4s
{%- elif nodeStatusUpdate == 'Medium' -%}
20s
{%- elif nodeStatusUpdate == 'Low' -%}
1m
{% else -%}
10s
{% endif %}
{%- endif %}
oomScoreAdj: -999
Output:
My current output is:
nodeStatusUpdateFrequency: $x
oomScoreAdj: -999
Desired Output:
My expected output is:
nodeStatusUpdateFrequency: $xoomScoreAdj: -999
Your template is OK. The play below with copy&paste of your template
vars:
nodeStatusUpdateFrequency: "$x"
nodeStatusUpdate: "NONE"
tasks:
- template:
src: test-template.j2
dest: /scratch/test.txt
gives:
# cat /scratch/test.txt
nodeStatusUpdateFrequency: $x
oomScoreAdj: -999
You are simply missing the minus (-) signs on some of your endif control structures. This is doing the job as you expect:
nodeStatusUpdateFrequency:
{%- if nodeStatusUpdateFrequency is defined -%}
{{ nodeStatusUpdateFrequency }}
{%- else -%}
{%- if nodeStatusUpdate == 'Fast' -%}
4s
{%- elif nodeStatusUpdate == 'Medium' -%}
20s
{%- elif nodeStatusUpdate == 'Low' -%}
1m
{%- else -%}
10s
{%- endif -%}
{%- endif -%}
oomScoreAdj: -999
Related
I have these hosts in my hosts.yml file:
all:
hosts:
host1:
name: name1
port: port1
host2:
name: name2
port: port2
host3:
name: name3
port: port3
I want a string with all of my hosts in the hosts.yml file with ports like:
"http://name2:port2, http://name3:port3"
Without the current host (in my example, http://name1:port1).
I want this to be generated for each host when the ansible-playbook is run, and I want to pass it to my app in the startup process like (for host1):
$entrypoint.sh otherUrls="http://name2:port2, http://name3:port3"
I use this code to make the string:
otherUrls: "{%- for host in groups['all'] -%}
{%- if hostvars[host]['name'] is defined -%}
{%- if name != hostvars[host]['name'] -%}http://{{ hostvars[host]['name'] }}:{{ hostvars[host]['port'] }}
{%- if not loop.last -%}\", \"{% endif -%}
{% endif -%}
{% endif -%}
{% endfor -%}"
I made this with jinja2. But I still have an extra ',' at the end of the string for the last host. Are there any ideas to get rid of the problem?
I found a way to my problem without json_query:
{%- set groups_without_this = [] -%}
{% for host in groups['all'] -%}
{% if node_name != hostvars[host]['name'] -%}
{% do groups_without_this.append(host) -%}
{% endif -%}
{% endfor -%}
{%- for host in groups_without_this -%}
{%- if hostvars[host]['nname'] is defined -%}
{%- if name != hostvars[host]['name'] -%}http://{{ hostvars[host]['name'] }}:{{ hostvars[host]['port'] }}
{%- if not loop.last -%}\", \"{% endif -%}
{% endif -%}
{% endif -%}
{% endfor -%}
This code creates a new list without the current node and then constructs the "otherUrls" string.
- set_fact:
{%- for IP in Neighbor_Info.keys() -%}
{%- if item['items']['key'] | string is IP | string -%}
Neighbor_Info[IP]: {{ Neighbor_Info[IP] + item[fetch][0][0] | regex_search(' [0-9]+') }}
{%- endif -%}
{%- endfor -%}
when: fetch in item.keys()
loop: "{{Prefix_Limit1['results']}}"
Error:
fatal: ]: FAILED! =>
msg: 'template error while templating string: Encountered unknown tag ''endif''. Jinja was looking for the following tags: ''endfor'' or ''else''. The innermost block that needs to be closed is ''for''.. String: {% for IP in Neighbor_Info.keys() if item[''items''][''key''] == IP -%} Neighbor_Info[IP]: {{ Neighbor_Info[IP] + item[fetch][0][0] | regex_search('' [0-9]+'') }} {% endif %} {% endfor -%}'
I want to update the value in a dictionary after matching the key value.
Once the match against the key is found, the value is appended in the list.
Dictionary
(item={'key': '192.168.123.9', 'value': ['xxx: 1', 'YYY: 0', 'ABCD']})
i want to set inside a variable interface name if is equals to a variable pass on command line so i'm using jinja like this :
- name: Inizializzo la rete
hosts: all
gather_facts: true
become: yes
become_user: root
tasks:
- name: stampo interfacce
set_fact:
privata: "{% for interfacce in ansible_interfaces %} {% set int_tmp = 'ansible_facts.' ~ interfacce ~ '.macaddress' %} {% if {{int_tmp}}==mac_privata %} {{interfacce}} {% endif %} {% endfor %}"
- name: Stampo
debug:
msg: "{{privata}}"
but it doesn't works
i call this :
ansible-playbook test.yml --extra-vars "mac_privata=00:50:56:b7:bc:f1"
i do this because i've more than three interface
where i'm wrong?
EDIT:
#mdaniel
Thanks for your answer but it seems that it doesn't substitute hostvars['ansible_' ~ interfacce ~ '.macaddress'] with content of ansible_ens32.macaddress.
For example i do this :
- name: Stampo
debug:
msg: "{{ ansible_ens32.macaddress }}"
i've this output :
TASK [Stampo] **********************************************************************************************************************************************
ok: [10.150.20.130] => {
"msg": "00:50:56:b7:bb:f1"
}
but if i do this :
- name: stampo interfacce
set_fact:
privata: >-
{%- for interfacce in ansible_interfaces -%}
{%- if interfacce != 'lo' -%}
{%- set int_tmp = hostvars['ansible_' ~ interfacce ~ '.macaddress'] -%}
{%- if int_tmp == mac_privata -%}
{{ interfacce }}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
- name: Stampo
debug:
msg: "{{privata}}"
I'm expecting name of interface with mac_privata macaddress in privata variable if i do this :
ansible-playbook test.yml --extra-vars "mac_privata=00:50:56:b7:bb:f1"
i've ens_32 but it's empty:
TASK [stampo interfacce] ***********************************************************************************************************************************
task path: /mnt/c/Users/francesco.ferraro/ansible_test/test.yml:7
ok: [10.150.20.130] => {
"ansible_facts": {
"privata": ""
},
"changed": false
}
Any time one sees {{ inside another jinja2 context, it's almost certainly not what you what to happen. To dynamically look up a variable, use either vars[""] or hostvars[""], or if you prefer the lookup("vars", "")
privata: >-
{%- for interfacce in ansible_interfaces -%}
{%- set int_tmp = vars['ansible_facts.' ~ interfacce ~ '.macaddress'] -%}
{%- if int_tmp == mac_privata -%}
{{ interfacce }}
{%- endif -%}
{%- endfor -%}
Most cases support the conditional for loop, which can make the snippet a little shorter
privata: >-
{%- for interfacce in ansible_interfaces
if mac_privata == vars['ansible_facts.' ~ interfacce ~ '.macaddress'] -%}
{{ interfacce }}
{%- endfor -%}
We use dbt cloud to run our dbt project. On CI runs, dbt Cloud uses a schema name related to the PR number, e.g. dbt_cloud_pr_5205_543.
Is there a way to override this behavior?
Update: we've updated our macro as below.
generate_schema_name.sql
% macro generate_schema_name(custom_schema_name, node) -%}
{%- set default_schema = target.schema -%}
{%- if target.name[-3:] == 'dev' -%}
{{ target.schema }}_{{ custom_schema_name | trim }}
{%- elif target.schema[:9] == 'dbt_cloud' -%}
{{ target.schema }}_{{ custom_schema_name | trim }}
{%- elif custom_schema_name is none -%}
{{ default_schema }}
{%- else -%}
{{ custom_schema_name | trim }}
{%- endif -%}
{%- endmacro %}
This macro resolved the problem:
generate_schema_name.sql
{% macro generate_schema_name(custom_schema_name, node) -%}
{%- set default_schema = target.schema -%}
{%- if target.name[-3:] == 'dev' -%}
{{ target.schema }}_{{ custom_schema_name | trim }}
{%- elif target.schema[:9] == 'dbt_cloud' -%}
{{ target.schema }}_{{ custom_schema_name | trim }}
{%- elif custom_schema_name is none -%}
{{ default_schema }}
{%- else -%}
{{ custom_schema_name | trim }}
{%- endif -%}
{%- endmacro %}
i am working on a Shopify Store here and need to display the product count for each collection in the drop-down sub-menu and somehow I can not figure it out. The documentation is a little bit light on this issue.
I need the product count to be shown only in the sub-menus.
I tried adding the code below but it snot working:
{% if childlink.type == 'collection_link' %}
({{ childlink.object.products_count }})
{%endif%}
Can anyone help me out with this? Below is my code:
<div class="dropdown-menu">
<div class="row tt-col-list">
<div class="col">
<ul class="tt-megamenu-submenu tt-megamenu-preview">
{%- for child_level_2 in level_2.links -%}
<li><span>{{ child_level_2.title }} </span>
{%- capture return -%}{%- include "get_linklist_dropdown", link: child_level_2 -%}{%- endcapture -%}{%- assign return = return | split: "%%" -%}{%- assign has_drop_down = return | first | strip -%}{%- assign child_list_handle = return | last | strip -%}
{%- assign level_3 = linklists[child_list_handle] -%}
{%- if level_3.empty? -%}
{%- assign child_list_handle = child_level_2.title | handle -%}
{%- assign level_3 = linklists[child_list_handle] -%}
{%- endif -%}
{%- if level_3.links != blank -%}
<ul>
{%- for child_level_3 in level_3.links -%}
<li>
<span>{{ child_level_3.title }}</span>
{%- capture return -%}{%- include "get_linklist_dropdown", link: child_level_3 -%}{%- endcapture -%}{%- assign return = return | split: "%%" -%}{%- assign has_drop_down = return | first | strip -%}{%- assign child_list_handle = return | last | strip -%}
{%- assign level_4 = linklists[child_list_handle] -%}
{%- if level_4.empty? -%}
{%- assign child_list_handle = child_level_3.title | handle -%}
{%- assign level_4 = linklists[child_list_handle] -%}
{%- endif -%}
{%- if level_4.links != blank -%}
<ul>
{%- for child_level_4 in level_4.links -%}
<li>
<span>{{ child_level_4.title }}</span>
{%- capture return -%}{%- include "get_linklist_dropdown", link: child_level_4 -%}{%- endcapture -%}{%- assign return = return | split: "%%" -%}{%- assign has_drop_down = return | first | strip -%}{%- assign child_list_handle = return | last | strip -%}
{%- if has_drop_down == "true" -%}
{%- assign level_5 = linklists[child_list_handle] -%}
{%- unless level_5.empty? -%}
<ul>
{%- for child_level_5 in level_5.links -%}
<li>{{ child_level_5.title }}</li>
{%- endfor -%}
</ul>
{%- endunless -%}
{%- endif -%}
</li>
{%- endfor -%}
</ul>
{%- endif -%}
</li>
{%- endfor -%}
</ul>
{%- endif -%}
</li>
{%- endfor -%}
</ul>
</div>
</div>
</div>
</pre>