FAQ
Hello,

I'm writing #!py code for state.sls. This code start to duplicate from
a_state.sls to an_other_state.sls and so on. Is there a way to push this
code in (let's say) a module that we could import on some_state.sls ?

cED

for example: i have the following alias.sls

#!py
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4


def get_lemail_from_lusername(luser_name):
lemail=[]
for user_name in luser_name:
lemail += __pillar__["duser"].get(user_name,{}).get("lemail",[])
return lemail

def get_lemail_from_lusergroup(lusergroup_name):
lemail=[]
luser_name=[]
for usergroup_name in lusergroup_name:
ltmpemail =
__pillar__["dusergroup"].get(usergroup_name,{}).get("lemail",[])
if len(ltmpemail) != 0:
lemail+=ltmpemail
else:
luser_name +=
__pillar__["dusergroup"].get(usergroup_name,{}).get("luser_name",[])
lemail += get_lemail_from_lusername(luser_name)
return lemail

def run():
luser_name=set()
hostname=__grains__['fqdn']
hostconfig=__pillar__["dhost"].get(hostname,{}).get("hostconfig",
"unix")

luog=__pillar__["dhostconfig"].get(hostconfig,{}).get("laliases_root",[])
if not isinstance(luog, list):
luog=[luog]
lemail=[]
#
# get the lemail
luser_name=[ uog[len("user_"):] for uog in luog if 0 ==
uog.find("user_") ]
lusergroup_name=[ uog[len("group_"):] for uog in luog if 0 ==
uog.find("group_") ]
# normal case

lemail=get_lemail_from_lusergroup(lusergroup_name)+get_lemail_from_lusername(luser_name)
# default case
if len(lemail) == 0:
lemail=get_lemail_from_lusergroup(["unix"])
# remove the duplicate
lemail=list(set(lemail))
#
# write the alias_root
lemail.sort()
emails=",".join(lemail)
return
{"alias_root":{"alias.present":[{"name":"root"},{"target":emails}]}}
How could I do something like
#!py
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import my_own_module

  def run():
luser_name=set()
hostname=__grains__['fqdn']
hostconfig=__pillar__["dhost"].get(hostname,{}).get("hostconfig",
"unix")

luog=__pillar__["dhostconfig"].get(hostconfig,{}).get("laliases_root",[])
if not isinstance(luog, list):
luog=[luog]
lemail=[]
#
# get the lemail
luser_name=[ uog[len("user_"):] for uog in luog if 0 ==
uog.find("user_") ]
lusergroup_name=[ uog[len("group_"):] for uog in luog if 0 ==
uog.find("group_") ]
# normal case

lemail=my_own_module.get_lemail_from_lusergroup(lusergroup_name)+get_lemail_from_lusername(luser_name)
# default case
if len(lemail) == 0:
         lemail=my_own_module.get_lemail_from_lusergroup(["unix"])
# remove the duplicate
lemail=list(set(lemail))
#
# write the alias_root
lemail.sort()
emails=",".join(lemail)
return
{"alias_root":{"alias.present":[{"name":"root"},{"target":emails}]}}









I'm wondering if I take the good approa

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

  • Colton Myers at Oct 28, 2014 at 9:46 pm
    Hey Cedric,

    I would recommend syncing a custom module down to your minion. That module will become available from the __salt__ dictionary inside of your #!py file.

    http://docs.saltstack.com/en/latest/ref/modules/ <http://docs.saltstack.com/en/latest/ref/modules/>

    Hope that helps!

    --
    Colton Myers
    Platform Engineer, SaltStack
    On Sep 18, 2014, at 1:41 AM, Cédric Briner wrote:

    Hello,

    I'm writing #!py code for state.sls. This code start to duplicate from a_state.sls to an_other_state.sls and so on. Is there a way to push this code in (let's say) a module that we could import on some_state.sls ?

    cED

    for example: i have the following alias.sls

    #!py
    # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4


    def get_lemail_from_lusername(luser_name):
    lemail=[]
    for user_name in luser_name:
    lemail += __pillar__["duser"].get(user_name,{}).get("lemail",[])
    return lemail

    def get_lemail_from_lusergroup(lusergroup_name):
    lemail=[]
    luser_name=[]
    for usergroup_name in lusergroup_name:
    ltmpemail = __pillar__["dusergroup"].get(usergroup_name,{}).get("lemail",[])
    if len(ltmpemail) != 0:
    lemail+=ltmpemail
    else:
    luser_name += __pillar__["dusergroup"].get(usergroup_name,{}).get("luser_name",[])
    lemail += get_lemail_from_lusername(luser_name)
    return lemail

    def run():
    luser_name=set()
    hostname=__grains__['fqdn']
    hostconfig=__pillar__["dhost"].get(hostname,{}).get("hostconfig", "unix")
    luog=__pillar__["dhostconfig"].get(hostconfig,{}).get("laliases_root",[])
    if not isinstance(luog, list):
    luog=[luog]
    lemail=[]
    #
    # get the lemail
    luser_name=[ uog[len("user_"):] for uog in luog if 0 == uog.find("user_") ]
    lusergroup_name=[ uog[len("group_"):] for uog in luog if 0 == uog.find("group_") ]
    # normal case
    lemail=get_lemail_from_lusergroup(lusergroup_name)+get_lemail_from_lusername(luser_name)
    # default case
    if len(lemail) == 0:
    lemail=get_lemail_from_lusergroup(["unix"])
    # remove the duplicate
    lemail=list(set(lemail))
    #
    # write the alias_root
    lemail.sort()
    emails=",".join(lemail)
    return {"alias_root":{"alias.present":[{"name":"root"},{"target":emails}]}}

    How could I do something like
    #!py
    # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

    import my_own_module

    def run():
    luser_name=set()
    hostname=__grains__['fqdn']
    hostconfig=__pillar__["dhost"].get(hostname,{}).get("hostconfig", "unix")
    luog=__pillar__["dhostconfig"].get(hostconfig,{}).get("laliases_root",[])
    if not isinstance(luog, list):
    luog=[luog]
    lemail=[]
    #
    # get the lemail
    luser_name=[ uog[len("user_"):] for uog in luog if 0 == uog.find("user_") ]
    lusergroup_name=[ uog[len("group_"):] for uog in luog if 0 == uog.find("group_") ]
    # normal case
    lemail=my_own_module.get_lemail_from_lusergroup(lusergroup_name)+get_lemail_from_lusername(luser_name)
    # default case
    if len(lemail) == 0:
    lemail=my_own_module.get_lemail_from_lusergroup(["unix"])
    # remove the duplicate
    lemail=list(set(lemail))
    #
    # write the alias_root
    lemail.sort()
    emails=",".join(lemail)
    return {"alias_root":{"alias.present":[{"name":"root"},{"target":emails}]}}










    I'm wondering if I take the good approa

    --
    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 <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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupsalt-users @
postedSep 18, '14 at 7:41a
activeOct 28, '14 at 9:46p
posts2
users2

2 users in discussion

Colton Myers: 1 post Cédric Briner: 1 post

People

Translate

site design / logo © 2022 Grokbase