using a list

13 views
Skip to first unread message

brad.v...@gmail.com

unread,
Nov 1, 2022, 11:04:23 AM11/1/22
to Salt-users
We have some salt states to set up iSCSI against some 3par storeservs.  Most of the storeservs have 4 iSCSI interfaces.  A few have 2.  We have the IP networks hardcoded into the jinja logic.  If we add another 2 interface storeserv, have to modify the logic.  I'd like to modify the state file to use a jinja list.  Set the IP subnets of all 2 interface storeservs in this list.  Then iterate over the list and test if the current server subnet matches.  That way, the only thing to change if another one gets added is the list.  I was thinking something like this:

<pre class="python" style="font-family:monospace;"><span style="color: black;">&#123;</span>% <span style="color: #008000;">set</span> IP1 <span style="color: #66cc66;">=</span> salt<span style="color: black;">&#91;</span><span style="color: #483d8b;">'pillar.get'</span><span style="color: black;">&#93;</span><span style="color: black;">&#40;</span>grains<span style="color: black;">&#91;</span><span style="color: #483d8b;">'host'</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">~</span> <span style="color: #483d8b;">':IP1'</span><span style="color: black;">&#41;</span> %<span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span>% <span style="color: #008000;">set</span> IP2 <span style="color: #66cc66;">=</span> salt<span style="color: black;">&#91;</span><span style="color: #483d8b;">'pillar.get'</span><span style="color: black;">&#93;</span><span style="color: black;">&#40;</span>grains<span style="color: black;">&#91;</span><span style="color: #483d8b;">'host'</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">~</span> <span style="color: #483d8b;">':IP2'</span><span style="color: black;">&#41;</span> %<span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span>% <span style="color: #008000;">set</span> SPLIT1 <span style="color: #66cc66;">=</span> IP1.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span> %<span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span>% <span style="color: #008000;">set</span> SPLIT2 <span style="color: #66cc66;">=</span> IP2.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span> %<span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span>% <span style="color: #008000;">set</span> SUBNET1 <span style="color: #66cc66;">=</span> SPLIT1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">~</span><span style="color: #483d8b;">'.'</span>SPLIT1<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">~</span><span style="color: #483d8b;">'.'</span><span style="color: #66cc66;">~</span>SPLIT1<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> %<span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span>% <span style="color: #008000;">set</span> SUBNET2 <span style="color: #66cc66;">=</span> SPLIT2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">~</span><span style="color: #483d8b;">'.'</span>SPLIT2<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">~</span><span style="color: #483d8b;">'.'</span><span style="color: #66cc66;">~</span>SPLIT2<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> %<span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span>% <span style="color: #008000;">set</span> SUBNETS <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span> <span style="color: #483d8b;">'192.168.1'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'192.168.12'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'192.168.72'</span> <span style="color: black;">&#93;</span>
<span style="color: black;">&#123;</span>% <span style="color: #ff7700;font-weight:bold;">for</span> SUB <span style="color: #ff7700;font-weight:bold;">in</span> SUBNETS %<span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span>% <span style="color: #ff7700;font-weight:bold;">if</span> SUBNET1 <span style="color: #66cc66;">==</span> SUB %<span style="color: black;">&#125;</span>
<span style="color: #808080; font-style: italic;"># process for a two interface storeserv</span>
<span style="color: black;">&#123;</span>% <span style="color: #ff7700;font-weight:bold;">else</span> %<span style="color: black;">&#125;</span>
<span style="color: #808080; font-style: italic;"># process for a four interface storeserv</span>
<span style="color: black;">&#123;</span>% endif %<span style="color: black;">&#125;</span>
<span style="color: black;">&#123;</span>% endfor %<span style="color: black;">&#125;</span></pre>

Does that look correct?

brad.v...@gmail.com

unread,
Nov 1, 2022, 11:06:24 AM11/1/22
to Salt-users
Well, the HTML highlighting did not work!

{% set IP1 = salt['pillar.get'](grains['host'] ~ ':IP1') %}
{% set IP2 = salt['pillar.get'](grains['host'] ~ ':IP2') %}
{% set SPLIT1 = IP1.split('.') %}
{% set SPLIT2 = IP2.split('.') %}
{% set SUBNET1 = SPLIT1[0]~'.'SPLIT1[1]~'.'~SPLIT1[2] %}
{% set SUBNET2 = SPLIT2[0]~'.'SPLIT2[1]~'.'~SPLIT2[2] %}
{% set SUBNETS = [ '192.168.1', '192.168.12', '192.168.72' ] %}
 {% for SUB in SUBNETS %}
{% if SUBNET1 == SUB %}
# process for a two interface storeserv
{% else %}
# process for a four interface storeserv
{% endif %}
{% endfor %}

Phipps, Thomas

unread,
Nov 1, 2022, 11:28:59 AM11/1/22
to salt-...@googlegroups.com

your original logic won’t work. it will both set the three interface logic once AND the four interface logic twice for each “subnet”
this is because you are looping through the subnets and you can’t match all three ever.

{% set IP1 = salt['pillar.get'](grains['host'] ~ ':IP1') %}
{% set IP2 = salt['pillar.get'](grains['host'] ~ ':IP2') %}
{% set SUBNET1 = IP1.split('.')[0:3]|join('.') %}
{% set SUBNET2 = IP2.split('.')[0:3]|join('.') %}
{% set SUBNETS = [ '192.168.1', '192.168.12', '192.168.72' ] %}
{% if SUBNET1 in SUBNETS %}
 # process for a two interface storeserv  
{% else %}
 # process for a four interface storeserv  
{% endif %}

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/e0f72efa-641d-4443-ade6-990ee3288c43n%40googlegroups.com.

brad.v...@gmail.com

unread,
Nov 2, 2022, 9:34:47 AM11/2/22
to Salt-users
No, I think it will work.  I'm not trying to match all three at once.  I'm just trying to match if the iSCSI subnet on server x happens to be one of those subnets.  If so, then I only want to check for a two node storeserv.  If it does not match, then it will be a four node storeserv.  I just ran the shell (without any real commands in it ) on a server connected to a four node storeserv and another connected to a two node storeserv and it worked correctly in both locations.

Phipps, Thomas

unread,
Nov 2, 2022, 1:30:44 PM11/2/22
to salt-...@googlegroups.com

as you presented each subnet will trigger 3 results

either 1 two interfaces and 2 four interfaces, or 3 four interface

so if you are not getting the three results you have something else blocking the non standard result.

in doesn’t check all three at once. it just checks if item a is in the list b.
The other thing i don’t get. you look to be pulling the ip from pillar.why not pull the ip from the server? or even just check if the server is in a subnet directly?

Here is what I’m talking about in what I wrote vs what you wrote.

root@salt00:/srv/salt/tests# salt-call slsutil.renderer salt://tests/jinja.sls default_renderer=jinja
local:

    192.168.1

    192.168.1

    192.168.72

    vs

    192.168.72

root@salt00:/srv/salt/tests# cat jinja.sls
{% set IP1 = '192.168.72.11' %}
{% set IP2 = '192.168.1.3' %}
{% set SUBNET1 = IP1.split('.')[0:3]|join('.') %}
{% set SUBNET2 = IP2.split('.')[0:3]|join('.') %}
{% set SUBNETS = [ '192.168.1', '192.168.12', '192.168.72' ] %}
{% for SUBNET in SUBNETS %}
{% if SUBNET1 == SUBNET %}
{{SUBNET1}}
{% else %}
{{SUBNET2}}
{% endif %}
{%endfor%}

vs

{% if SUBNET1 in SUBNETS %}
{{SUBNET1}}
{% else %}
{{SUBNET2}}
{% endif %}

Reply all
Reply to author
Forward
0 new messages