INI構成ファイルを更新したい。
今日、私は自分の情報を次のようにvarファイル(group_vars内)に保存します。
# Identity configuration information
identity_servers_conf:
DEFAULT:
admin_token: "{{identity_admin_token}}"
verbose: True
database:
connection: "mysql://{{ identity_db_user }:{{ identity_db_password }}@{{ db_lb_name }}/{{ identity_db }}"
token:
provider: keystone.token.providers.uuid.Provider
driver: keystone.token.persistence.backends.sql.Token
私のAnsibleタスクでは、これらの情報を次のように使用します。
- name: configuration / modify keystone.conf ini file DEFAULT section
ini_file:
section: DEFAULT
dest: /etc/keystone/keystone.conf
option: "{{item.key}}"
value: "{{item.value}}"
with_dict: identity_servers_conf['DEFAULT']
各「セクション」パラメーター、つまりDEFAULT、データベース、トークンを使用してdictファイルを反復処理する方法はありますか。実際、私はwith_itemsループにネストされたwith_dictを実行する方法を見つけようとしています。
.iniファイルの変数を整理するこの方法は非常に興味深いと思います。
自分で使いたかったので、inifileモジュールを使って.iniファイルのすべてのキーを1回のパスで生成できるプラグインを開発しました。
それは正常に動作し、OpenStack構成ファイルの管理に使用します。
私は開発のスペシャリストではありませんが、このプラグインはすべての人に役立つと思います。そのため、誰かがそれを維持してansibleに統合したい場合は、彼を歓迎します。
プラグインは、以下のように、inifileモジュールwith_inidataで直接使用できるように、リスト(セクション、キー、値)の階層データを変換します。
varsファイル:
...
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/
...
プラグインコード:
# (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ファイルの管理を簡素化できることを願っています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加