我发现这种为.ini文件组织变量的方式非常有趣。
我想自己使用它,所以我开发了一个插件,允许通过inifile模块一次性生成.ini文件的所有密钥。
它工作正常,我用它来管理我的OpenStack配置文件。
我不是一个开发专家,但我认为这个插件可以对每个人都有用,所以如果有人想要接管维护并将其整合到可靠的,他是受欢迎的。
瓦尔文件:
...
glanceapi_conf:
DEFAULT:
verbose: "{{ image_log_verbose }}"
rabbit_host: "{{ amqp_host }}"
rabbit_port: "{{ amqp_port }}"
rabbit_userid: "{{ amqp_userid }}"
rabbit_password: "{{ amqp_password }}"
rabbit_ha_queues: "{{ amqp_ha_queues }}"
database:
connection: "mysql://{{ image_db_user }}:{{ image_db_password }}@{{ db_host }}/{{ image_db }}"
keystone_authtoken:
auth_uri: "http://{{ identity_admin_host }}:{{ identity_api_port }}/v2.0"
identity_uri: "http://{{ identity_admin_host }}:{{ identity_admin_port }}"
admin_tenant_name: "{{ image_ks_tenant }}"
admin_user: "{{ image_ks_user }}"
admin_password: "{{ image_ks_password }}"
paste_deploy:
flavor: keystone
glance_store:
default_store: file
filesystem_store_datadir: /var/lib/glance/images/
...
插件代码:
该插件与INIFILE模块with_inidata如下直接变换层级结构数据列表中的(部,键,值),用于使用
# (c) 2014, Pierre-Yves KERVIEL <[email protected]>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# inidata is used to manage ini
import ansible.utils as utils
import ansible.errors as errors
class LookupModule(object):
def __init__(self, basedir=None, **kwargs):
self.basedir = basedir
def run(self, terms, inject=None, **kwargs):
terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)
if not isinstance(terms, dict):
raise errors.AnsibleError("inidata lookup expects a dictionnary , got '%s'" %terms)
ret = []
for item0 in terms:
if not isinstance(terms[item0], dict):
raise errors.AnsibleError("inidata lookup expects a dictionary, got '%s'" %terms[item0])
for item1 in terms[item0]:
ret.append((item0, item1, terms[item0][item1]))
return ret
任务代码:
- name: configuration.modify_glance-api_conf_file/modify glance-api.conf ini file
ini_file:
section: "{{ item.0 }}"
dest: /etc/glance/glance-api.conf
option: "{{ item.1 }}"
value: "{{ item.2 }}"
backup: yes
with_inidata: glanceapi_conf
要使用它,只需在/etc/ansible.cfg中定义的目录中复制名为“dataini”的插件代码即可。
对于Ubuntu发行版,这应该是/ usr/share/ansible_plugins/lookup_plugins,并按照我的示例编写任务。
我希望这个插件可以让你简化你的ini文件管理。
恩,看起来你需要在jinja中进行迭代,而不是在Ansible中进行迭代。 – tedder42 2014-11-25 17:55:51
在这种情况下,我宁愿使用ini文件模板(请参阅'template module')。即使你打算做的事情是可能的,但看起来很混乱。 'ini模块'主要是模板模块的快捷方式,所以您应该只将它用于非常简单的任务 – ProfHase85 2014-11-26 12:41:20
感谢您的评论tedder42和ProfHase85。实际上,我之前使用过模板文件,但我更愿意让配置文件像安装程序所安装的一样,并通过使用ini_file来更改其中的某些值。当你使用模板文件时,当配置文件由于新版本的软件而改变时,你可能会遇到问题,并且你继续在远程主机上放置以前版本的配置文件。 – 2014-11-26 17:00:09