Discussion:
[salt-users] Filter ipv4 by subnet in map.jinja. How ?!
Anton A
2015-10-31 22:08:03 UTC
Permalink
Hi people!

I'm trying to create map.jinja to math ipv4 by subnet and assign server
variable related to subnet.

Here is map.jinja:

{% set zabbix_agent = salt['grains.filter_by']({
'10.0.1*': {
'server': '10.0.1.24'
},
'10.0.2*': {
'server': '10.0.2.24'
}
},
grain='ip4_interfaces:eth0:0',
merge=salt['pillar.get']('zabbix_agent:lookup')
)
%}

It doesn't work because mask here is useless.

Anybody, do you have ideas how to make it work ?

Is there only way to create own grains.filter_by_subnet function ?

Thanks.
--
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.
For more options, visit https://groups.google.com/d/optout.
Loren Gordon
2015-10-31 22:55:30 UTC
Permalink
When I needed something similar, I had to write a filter function in a
custom module.

```
import fnmatch


def filter_by_glob(lookup_table, match):
for key in lookup_table.keys():
if fnmatch.fnmatch(match, key):
return lookup_table[key]
return None
```
Post by Anton A
Hi people!
I'm trying to create map.jinja to math ipv4 by subnet and assign server
variable related to subnet.
{% set zabbix_agent = salt['grains.filter_by']({
'10.0.1*': {
'server': '10.0.1.24'
},
'10.0.2*': {
'server': '10.0.2.24'
}
},
grain='ip4_interfaces:eth0:0',
merge=salt['pillar.get']('zabbix_agent:lookup')
)
%}
It doesn't work because mask here is useless.
Anybody, do you have ideas how to make it work ?
Is there only way to create own grains.filter_by_subnet function ?
Thanks.
--
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.
For more options, visit https://groups.google.com/d/optout.
Florian Ermisch
2015-11-01 07:29:14 UTC
Permalink
Hi Anton,

is this for states or for pillar?
Can't you just use the subnet matcher in your top.sls to set the value? You could use include with context to pass the value to your state/pillar.

That said the custom module should be easy to write by just wrapping existing code from Salt.
I've done some IPv4 math in a py-renderer state [0] myself. I got to admit I didn't switch to salt.utils.network yet because their version of the function is useless for me*.

Regards, Florian

[0] https://github.com/saltstack-formulas/openvswitch-formula/blob/master/networking/config.sls

*) can't just add 1 to an IPv4 when one gets a string of 1s and 0s instead of an integer…
Post by Loren Gordon
When I needed something similar, I had to write a filter function in a
custom module.
```
import fnmatch
return lookup_table[key]
return None
```
Post by Anton A
Hi people!
I'm trying to create map.jinja to math ipv4 by subnet and assign
server
Post by Anton A
variable related to subnet.
{% set zabbix_agent = salt['grains.filter_by']({
'10.0.1*': {
'server': '10.0.1.24'
},
'10.0.2*': {
'server': '10.0.2.24'
}
},
grain='ip4_interfaces:eth0:0',
merge=salt['pillar.get']('zabbix_agent:lookup')
)
%}
It doesn't work because mask here is useless.
Anybody, do you have ideas how to make it work ?
Is there only way to create own grains.filter_by_subnet function ?
Thanks.
--
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.
For more options, visit https://groups.google.com/d/optout.
Anton A
2015-11-02 12:41:34 UTC
Permalink
Hi Florian,

It's for pillar. I have web servers in different subnets, each subnet has
own monitoring proxy.
Web servers matched in top.sls as webserver*.domain.com
In template monitoring.conf.jinja I need to set ip address of monitoring
proxy, it depends on subnet.
It's why I used map.jinja

I expected to have regex filter in filter_by, it's logical and I guess
would be very useful.

So, it's still unclear what is "Best practice" for my situation.

Best regards,
Anton
Post by Florian Ermisch
Hi Anton,
is this for states or for pillar?
Can't you just use the subnet matcher in your top.sls to set the value?
You could use include with context to pass the value to your state/pillar.
That said the custom module should be easy to write by just wrapping
existing code from Salt.
I've done some IPv4 math in a py-renderer state [0] myself. I got to admit
I didn't switch to salt.utils.network yet because their version of the
function is useless for me*.
Regards, Florian
[0]
https://github.com/saltstack-formulas/openvswitch-formula/blob/master/networking/config.sls
<https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fsaltstack-formulas%2Fopenvswitch-formula%2Fblob%2Fmaster%2Fnetworking%2Fconfig.sls&sa=D&sntz=1&usg=AFQjCNGxC776AUz79B_853SnDKE426LfEA>
*) can't just add 1 to an IPv4 when one gets a string of 1s and 0s instead
of an integer

Post by Loren Gordon
When I needed something similar, I had to write a filter function in a
custom module.
```
import fnmatch
return lookup_table[key]
return None
```
Post by Anton A
Hi people!
I'm trying to create map.jinja to math ipv4 by subnet and assign
server
Post by Anton A
variable related to subnet.
{% set zabbix_agent = salt['grains.filter_by']({
'10.0.1*': {
'server': '10.0.1.24'
},
'10.0.2*': {
'server': '10.0.2.24'
}
},
grain='ip4_interfaces:eth0:0',
merge=salt['pillar.get']('zabbix_agent:lookup')
)
%}
It doesn't work because mask here is useless.
Anybody, do you have ideas how to make it work ?
Is there only way to create own grains.filter_by_subnet function ?
Thanks.
--
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.
For more options, visit https://groups.google.com/d/optout.
Seth House
2015-11-02 00:15:35 UTC
Permalink
Post by Anton A
Is there only way to create own grains.filter_by_subnet function ?
I think you want the `match.filter_by` function instead. You can
specify `expr_form='ipcidr'` and reuse the same CIDR matching notation
at Salt's CLI.

https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.match.html#salt.modules.match.filter_by

It doesn't support the `merge` kwarg (would be a good addition). But
that's easy enough to roll as a separate step:

{% do zabbix_agent.update(salt.pillar.get('zabbix_agent:lookup')) %}
--
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.
For more options, visit https://groups.google.com/d/optout.
Anton A
2015-11-02 12:47:11 UTC
Permalink
Hi Seth,

Do you mean such syntax ?

{% set zabbix_agent = salt['grains.filter_by(`expr_form='ipcidr'`)']({

Best regards,
Anton
Post by Seth House
Post by Anton A
Is there only way to create own grains.filter_by_subnet function ?
I think you want the `match.filter_by` function instead. You can
specify `expr_form='ipcidr'` and reuse the same CIDR matching notation
at Salt's CLI.
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.match.html#salt.modules.match.filter_by
It doesn't support the `merge` kwarg (would be a good addition). But
{% do zabbix_agent.update(salt.pillar.get('zabbix_agent:lookup')) %}
--
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.
For more options, visit https://groups.google.com/d/optout.
Seth House
2015-11-02 16:42:11 UTC
Permalink
Looks like there's a subtle bug in that function. I just submitted the
following fix.

https://github.com/saltstack/salt/pull/28494

That said, the compound matcher works. I tested the following syntax;
this should do what you're after:

{% set zabbix_agent = salt['match.filter_by']({
'***@10.0.1.0/24': {
'server': '10.0.1.24',
},
'***@10.0.2.0/24': {
'server': '10.0.2.24',
},
}) %}

lookup_barker:
cmd.run:
- name: |
echo 'Got server: {{ zabbix_agent.server }}.'

Oh, and make sure you call `match.filter_by` and not `grains.filter_by`.
--
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.
For more options, visit https://groups.google.com/d/optout.
Anton A
2015-11-02 17:55:36 UTC
Permalink
Hi Seth,
Thank you very much. It works!

Seth rocks! :-)
Post by Seth House
Looks like there's a subtle bug in that function. I just submitted the
following fix.
https://github.com/saltstack/salt/pull/28494
That said, the compound matcher works. I tested the following syntax;
{% set zabbix_agent = salt['match.filter_by']({
'server': '10.0.1.24',
},
'server': '10.0.2.24',
},
}) %}
- name: |
echo 'Got server: {{ zabbix_agent.server }}.'
Oh, and make sure you call `match.filter_by` and not `grains.filter_by`.
--
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.
For more options, visit https://groups.google.com/d/optout.
Loading...