FAQ
Is there a way to access a function from salt.utils from SLS (Jinja
substitution)? There is a couple of useful functions under "utils.network"
which I'd like to use.

Specifically, I have a network range in CIDR format as a value of pillar
"my_ip_range". So I want to be able to split a range like 192.168.1.0/24
into address and netmask in decimal format, in this case 192.168.1.0 and
255.255.255.0. There are functions in salt/utils/network.py that do exactly
this, so I wonder if I can use them. I have tried something like this:

{{ salt.utils.IPNetwork(pillar.get['my_ip_range']).netmask }}
but it didn't work.

Can I do that at all? If not, what is the best way to approach my problem?

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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Arnold Bechtoldt at Sep 1, 2014 at 12:09 pm
    I would be also interested in have access to more helper functions in
    the SLS (state/pillar/template) files (Jinja is lacking so much
    features, direct python access would be great).

    My way of solving this is to create custom extension modules:

    <https://github.com/bechtoldt/salt-modules>

    It works nice but IMHO it won't be the right solution in every case.


    --
    Arnold Bechtoldt

    Karlsruhe, Germany
    On 01.09.14 14:02, ruslan.mustakov@xored.com wrote:
    Is there a way to access a function from salt.utils from SLS (Jinja
    substitution)? There is a couple of useful functions under
    "utils.network" which I'd like to use.

    Specifically, I have a network range in CIDR format as a value of pillar
    "my_ip_range". So I want to be able to split a range like 192.168.1.0/24
    into address and netmask in decimal format, in this case 192.168.1.0 and
    255.255.255.0. There are functions in salt/utils/network.py that do
    exactly this, so I wonder if I can use them. I have tried something like
    this:

    {{salt.utils.IPNetwork(pillar.get['my_ip_range']).netmask }}
    but it didn't work.

    Can I do that at all? If not, what is the best way to approach my problem?

    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+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.
  • Jason M at Sep 1, 2014 at 1:24 pm
    I just released yamlscript yesterday and you should be able to do what you
    want with it.

    I will list an example to implement it. You need to add the !yamlscript
    shebang at top. You can chain with jinja as well, so long as yamlscript
    comes last. You can mix the python code within the regular yaml like this:

    #!yamlscript

    $python: |
         import salt.utils.ipaddr
         netmask = salt.utils.ipaddr.IPNetwork(pillar('my_ip_range')).netmask

    # The name of the file would be the value of netmask variable set above
    netmask_file:
         file:
         - name: $netmask
         - user: user
         - group: user

    If you are interested in trying it out and have problems, send me your sls
    so I can test with it.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jason M at Sep 1, 2014 at 1:45 pm
    I did not realize that the IPNetwork().netmask function returned an object,
    so I am assuming you want the string value to use, so wrap in in str() like
    this:

    netmask = str(salt.utils.ipaddr.IPNetwork(pillar('my_ip_range')).netmask)

    I added this example to the yamlscript tests so I know it works.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mrten at Sep 1, 2014 at 9:04 pm

    On 1/9/2014 14:02 , ruslan.mustakov@xored.com wrote:
    Is there a way to access a function from salt.utils from SLS (Jinja
    substitution)? There is a couple of useful functions under
    "utils.network" which I'd like to use.

    Specifically, I have a network range in CIDR format as a value of pillar
    "my_ip_range". So I want to be able to split a range like 192.168.1.0/24
    into address and netmask in decimal format, in this case 192.168.1.0 and
    255.255.255.0. There are functions in salt/utils/network.py that do
    exactly this, so I wonder if I can use them. I have tried something like
    this:

    {{salt.utils.IPNetwork(pillar.get['my_ip_range']).netmask }}
    but it didn't work.

    Can I do that at all? If not, what is the best way to approach my problem?
    Jinja is a pretty limited renderer for config files (and designed to be
    just that, limited, imho).

    As for alternatives; check out mako in which you can escape to python
    with <% %>, or have a look at the !py renderer (in which you write pure
    python and return a dict).

    For me, I started with mako but ended up rewriting almost everything
    that I write with mako escapes in !py but YMMV.

    M.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ryan Lane at Sep 2, 2014 at 5:53 pm

    On Mon, Sep 1, 2014 at 5:02 AM, wrote:

    Is there a way to access a function from salt.utils from SLS (Jinja
    substitution)? There is a couple of useful functions under "utils.network"
    which I'd like to use.

    Specifically, I have a network range in CIDR format as a value of pillar
    "my_ip_range". So I want to be able to split a range like 192.168.1.0/24
    into address and netmask in decimal format, in this case 192.168.1.0 and
    255.255.255.0. There are functions in salt/utils/network.py that do exactly
    this, so I wonder if I can use them. I have tried something like this:

    {{ salt.utils.IPNetwork(pillar.get['my_ip_range']).netmask }}
    but it didn't work.

    Can I do that at all? If not, what is the best way to approach my problem?

    I'd be careful relying on internals in your SLS. Modules are generally
    kept backwards compatible, but I'd imagine that internals could change
    without warning. It may be good to add a function to an upstream execution
    module that uses the helper function. If you add unit tests for the
    function you can ensure that it won't break on you in future versions. If
    you don't want to wait for a release for the added function you can add it
    as a custom module.

    If you add an issue for this a community member may even do this for you:
    https://github.com/saltstack/salt/issues/new

    - Ryan

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Florian Ermisch at Sep 5, 2014 at 5:51 am
    Hi,

    I think I've wrote such a function twice, one relying on the netaddr python-module (might be found under [0] later today) and a more minimal one which can be found in my fork of the salt-repo on github [1]...
    OK, gotta admit the later one was the other way around (`network.netmask_to_prefixlen`) but it should be easy to turn the algorithm around to create `network.cidr_2_ip_netmask`.

    Kind regards,Florian

    PS: I have getting s/t like this into the official salt-modules on my todo-list but I'm rather busy ;)

    [0] https://github.com/0xf10e/openvswitch-formula (for now a 404)
    [1] https://github.com/0xf10e/salt/blob/develop/salt/modules/network.py
    On 2. September 2014 19:53:29 MESZ, Ryan Lane wrote:
    On Mon, Sep 1, 2014 at 5:02 AM, wrote:

    Is there a way to access a function from salt.utils from SLS (Jinja
    substitution)? There is a couple of useful functions under
    "utils.network"
    which I'd like to use.

    Specifically, I have a network range in CIDR format as a value of pillar
    "my_ip_range". So I want to be able to split a range like
    192.168.1.0/24
    into address and netmask in decimal format, in this case 192.168.1.0 and
    255.255.255.0. There are functions in salt/utils/network.py that do exactly
    this, so I wonder if I can use them. I have tried something like this:
    {{ salt.utils.IPNetwork(pillar.get['my_ip_range']).netmask }}
    but it didn't work.

    Can I do that at all? If not, what is the best way to approach my problem?
    I'd be careful relying on internals in your SLS. Modules are
    generally
    kept backwards compatible, but I'd imagine that internals could change
    without warning. It may be good to add a function to an upstream
    execution
    module that uses the helper function. If you add unit tests for the
    function you can ensure that it won't break on you in future versions.
    If
    you don't want to wait for a release for the added function you can add
    it
    as a custom module.

    If you add an issue for this a community member may even do this for
    you:
    https://github.com/saltstack/salt/issues/new

    - Ryan

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ruslan Mustakov at Sep 5, 2014 at 8:25 am
    Thanks for all the suggestions. I ended up simply making custom module
    *net_utils.py*:
    import salt.utils.ipaddr as ipaddr

    __virtualname__ = 'net_utils'

    def __virtual__():
         return __virtualname__

    def gateway(ip_range):
         return ipaddr.IPAddress(1 + int(ipaddr.IPNetwork(ip_range)))

    def netmask(ip_range):
         return ipaddr.IPNetwork(ip_range).netmask

    An example of a state that uses it:
    br-ex:
       network.managed:
         - enabled: True
         - type: eth
         - proto: none
         - bypassfirewall: True
         - ipaddr: {{ salt['net_utils.gateway'](pillar.openstack.neutron.
    floating_ip_range) }}
         - netmask: {{ salt['net_utils.netmask'](pillar.openstack.neutron.
    floating_ip_range) }}

    yamlscript looks good, but I'm not sure about an easy way to reuse the same
    python code from different sls files. Also, for simplicity of use, I don't
    want to force people install something additional to use the states, and,
    besides, the states are used in masterless mode with single file root,
    while the doc states you need two file roots. salt-call seems to be
    accepting only one as a "--file-root" parameter.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupsalt-users @
postedSep 1, '14 at 12:02p
activeSep 5, '14 at 8:25a
posts8
users6

People

Translate

site design / logo © 2022 Grokbase