2014-11-25 7 views
6

मैं आईएनआई कॉन्फ़िगरेशन फ़ाइलों को अपडेट करना चाहता हूं।लूप में with_dict का उपयोग करके एक उत्तरदायी कार्य कैसे लिखें (with_items)

आज, मैं अपने जानकारियां एक वर फ़ाइल में इस तरह की दुकान (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 फ़ाइल के माध्यम से पुन: प्रयास करने का तरीका। वास्तव में, मैं a_items लूप में nested with_dict करने का तरीका खोजने का प्रयास करता हूं।

+0

उम, ऐसा लगता है कि आपको जिन्जा में फिर से शुरू करने की आवश्यकता है, जवाब में नहीं। – tedder42

+0

इस मामले में मैं एक आईएनआई फ़ाइल टेम्पलेट का उपयोग करूंगा ('टेम्पलेट मॉड्यूल' देखें)। यहां तक ​​कि यदि आप जो करने की योजना बना रहे हैं, वह भी बहुत भ्रमित दिखता है। 'आईएनआई मॉड्यूल' ज्यादातर टेम्पलेट मॉड्यूल के लिए एक शॉर्टकट है, इसलिए आपको केवल बहुत ही सरल कार्यों के लिए इसका उपयोग करना चाहिए – ProfHase85

+0

आपकी टिप्पणी tedder42 और ProfHase85 के लिए धन्यवाद। दरअसल, मैं पहले टेम्पलेट फ़ाइल का उपयोग कर रहा था, लेकिन मैं कॉन्फ़िगरेशन फ़ाइल को इंस्टॉल करना चाहता था क्योंकि यह इंस्टॉलर द्वारा स्थापित किया गया था और ini_file का उपयोग कर इसमें कुछ मूल्यों को बदलने के लिए। जब आप टेम्पलेट फ़ाइलों का उपयोग करते हैं तो आपको एक समस्या हो सकती है जब कॉन्फ़िगरेशन फ़ाइल सॉफ़्टवेयर के नए संस्करण की वजह से बदलती है और आप रिमोट होस्ट को कॉन्फ़िगरेशन फ़ाइल के पिछले संस्करण पर रखना जारी रखते हैं। –

उत्तर

4

मुझे .ini फ़ाइलों के लिए चर का आयोजन करने का यह तरीका बहुत दिलचस्प लगता है।

मैं इसे स्वयं उपयोग करना चाहता था, इसलिए मैंने एक प्लगइन पर काम किया जो एक फ़ाइल में सभी फाइलों को उत्पन्न करने की अनुमति देता है।

यह ठीक काम करता है और मैं अपनी ओपनस्टैक कॉन्फ़िगरेशन फ़ाइलों को प्रबंधित करने के लिए उपयोग करता हूं।

मैं विकास में विशेषज्ञ नहीं हूं, लेकिन मुझे लगता है कि यह प्लगइन हर किसी के लिए उपयोगी हो सकता है, इसलिए अगर कोई इसे उत्तर में बनाए रखने और एकीकृत करने के लिए लेना चाहता है, तो उसका स्वागत है।

वार्स फ़ाइल:

... 
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" नाम के साथ प्लगइन कोड कॉपी करें।

यह उबंटू वितरण के लिए/usr/share/ansible_plugins/lookup_plugins होना चाहिए और मेरे उदाहरणों के रूप में अपने कार्यों को लिखना चाहिए।

मुझे आशा है कि यह प्लगइन आपको अपने आईएनआई फाइल प्रबंधन को सरल बनाने की अनुमति देगा।

+0

धन्यवाद पियरे-यवेस, यह वही है जो मैं चाहता हूं। मैं आपके कोड एएसएपी कोशिश करूंगा। मैं आपको तैनात रखूंगा। –

+0

फिर से पियरे-यवेस धन्यवाद। मैंने आपके कोड की कोशिश की और यह बहुत अच्छी तरह से काम करता है। –

संबंधित मुद्दे