2015-10-13 58 views
1

我想通过在我的file_roots目录according to the Salt manual here的_grains子目录中添加一个python脚本来添加自定义谷物(盐堆)到爪牙。难以在Salt中添加custome谷物

我的方法是让脚本读入文本文件并将数据解析为字典中的列表并将它们装载为颗粒(在这种情况下称为角色)。

我roles_file看起来是这样的:

appserver:minion1.example,minion2.example,minion3.exapmle 
webserver:minion1.example 
dbserver:minion2.example,minion3.example 

,当读入一个dictioary看起来就像这样:

roles_list { 
    appserver: [minion1.example,minion2.example,minion3.example], 
    webserver: [minion1.example], 
    dbserver: [minino2.example, minion3.example] 
} 

set_roles()功能如下:

def set_roles(): 

    """ 
    Set the 'roles' grain based on the host name. 
    """ 

    roles_list = {} 
    with open ('roles_file' ,'r') as inf: 
    for line in inf: 
     role , servers = line.partition(":")[::2] 
     roles_list[role] = (servers.rstrip()).split(',') 


    grains = {'roles': []} 
    hostname = _get_hostname() # defined elsewhere in my file 
    logger.debug('{0}'.format(roles_list)) 

    for role, servers in roles_list.iteritems(): 

    for server in servers: 
     if server == hostname: 
     grains['roles'].append(role) 

    return grains 

这不起作用,角色不加载!我在这里错过了什么吗?

如何更好地调试?我知道set_roles()函数在执行highstate或调用saltutil.sync_grains时被调用。但我在哪里可以看到logger.debug的输出?

+0

首先,调用一个列表只是简单的混淆,所以我会重命名roles_list。我使用'minion1.example'作为主机名运行了一个快速测试,并且它工作正常。检查'主机名'的格式。 – RobertB

回答

0

我发现谷物没有加入,因为roels_file不存在于爪牙身上。我必须首先将文件复制到手中,然后运行saltutil.sync_grains然后运行。

我打算在我的环境中使用角色谷物来定位侍从。所以这显然是一个检查和鸡蛋的情况。我需要粮食来瞄准奴才。如果我对roles_file进行更改,我需要一个单独的状态文件(或顶级文件)来更新所有小众中的roles_file。从文件或列表中同步服装颗粒有没有更好的方法?

用于调试。我使用salt-minion -l debug命令在前台运行了小刀。