我正在写自定义模块使用以下方法在 Ubuntu 上创建本地镜像debmirror
。这是我的代码:
'''
Build a local mirror with debmirror.
'''
import logging
import os
import salt.utils
from salt.exceptions import CommandExecutionError
def created(arch,
section,
server,
release,
in_path,
proto,
out_path,
**kwargs):
'''
'''
__salt__['file.makedirs'](out_path)
if 'gnupghome' in kwargs:
gnupghome = kwargs.pop('gnupghome', None)
__salt__['file.makedirs'](gnupghome)
else:
gnupghome = '/root/.gnupg'
env = { 'GNUPGHOME': gnupghome }
if 'keyid' in kwargs:
keyid = kwargs.pop('keyid', None)
ks = kwargs.pop('keyserver', None)
if not keyid or not ks:
error_str = 'both keyserver and keyid options required.'
raise NameError(error_str)
cmd = 'apt-key export {0}'.format(keyid)
output = __salt__['cmd.run_stdout'](cmd, **kwargs)
imported = output.startswith('-----BEGIN PGP')
if ks:
if not imported:
cmd = ('gpg --no-default-keyring --keyring {0}/trustedkeys.gpg --keyserver {1} --recv-keys {2}')
ret = __salt__['cmd.run_all'](cmd.format(gnupghome, ks, keyid),
**kwargs)
if ret['retcode'] != 0:
raise CommandExecutionError(
'Error: key retrieval failed: {0}'
.format(ret['stdout'])
)
elif 'key_url' in kwargs:
key_url = kwargs['key_url']
cmd = 'wget -q -O- "{0}" | gpg --no-default-keyring --keyring {1}/trustedkeys.gpg --import'.format(key_url, gnupghome)
ret = __salt__['cmd.run_all'](cmd, **kwargs)
if ret['retcode'] != 0:
raise CommandExecutionError(
'Error: key retrieval failed: {0}'.format(ret['stderr'])
)
return __salt__['cmd.run_all']('debmirror -a {0} --no-source -s {1} -h {2} -d {3} -r {4} --progress -e {5} {6}'.format(arch, section, server, release, in_path, proto, out_path), env=env)
此模块在 minion 上测试时运行良好:
salt-call debmirror.created /home/mirrorkeyring/ \
i386,amd64 \
main,restricted,universe,multiverse \
ppa.launchpad.net \
precise \
/saltstack/salt/ubuntu \
http \
/saltmirror \
key_url='http://keyserver.ubuntu.com:11371/pks/lookup?op=get&search=0x4759FA960E27C0A6'
但我想知道:如何kwargs
在状态文件中定义?
{% for package in ('debmirror', 'wget', 'gnupg') %}
{{ package }}:
pkg:
- installed
{% endfor %}
salt-mirror:
module:
- run
- name: debmirror.created
- arch: i386,amd64
- section: main,restricted,universe,multiverse
- release: precise
- server: ppa.launchpad.net
- in_path: /saltstack/salt/ubuntu
- proto: http
- out_path: /saltmirror
- <WHAT SHOULD I PUT HERE?>
- require:
- pkg: debmirror
答案1
您可以kwargs
使用函数参数声明在状态文件中,如下所示:
{% for package in ('debmirror', 'wget', 'gnupg') %}
{{ package }}:
pkg:
- installed
{% endfor %}
salt-mirror:
module:
- run
- name: debmirror.created
- arch: i386,amd64
- section: main,restricted,universe,multiverse
- release: precise
- server: ppa.launchpad.net
- in_path: /saltstack/salt/ubuntu
- proto: http
- out_path: /saltmirror
- kwargs: {
key_url: 'http://keyserver.ubuntu.com:11371/pks/lookup?op=get&search=0x4759FA960E27C0A6BLAH',
gnupghome: /home/mirrorkeyring
}
- require:
- pkg: debmirror
来源:https://groups.google.com/forum/#!topic/salt-users/_hrsQd5CLvM