2014-11-25 4 views
6

INI 구성 파일을 업데이트하려고합니다.루프에서 with_dict를 사용하여 작업을 수행하는 방법 (with_items)

오늘은 (group_vars에서)이 var 파일이 방법은 내 정보를 저장 :

- 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'] 

인가 거기에 내 Ansible 작업에서는

# 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 

을, 나는이 정보를이 방법을 사용 방법은 각 "섹션"매개 변수, 즉 DEFAULT, 데이터베이스, 토큰을 사용하여 내 사전 파일을 반복합니다. 사실, with_items 루프에 중첩 된 with_dict를 수행하는 방법을 찾으려고합니다.

+0

음, 당신은 진저에서 반복 할 필요가있는 것으로 보입니다. – tedder42

+0

이 경우 필자는 ini 파일 템플릿을 사용하고 싶습니다 ('템플릿 모듈'참고). 비록 당신이하려고하는 것이 가능했다 할지라도 그것은 매우 혼란스럽게 보일 것입니다. 'ini 모듈 '은 대부분 템플릿 모듈의 바로 가기이므로 매우 간단한 작업에만 사용해야합니다. – ProfHase85

+0

tedder42 및 ProfHase85에 감사드립니다. 실제로 템플릿 파일을 사용하고 있었지만 설치 프로그램에서 설치 한 config 파일을 사용하고 ini_file을 사용하여 일부 값을 변경하는 것을 선호합니다. 템플릿 파일을 사용하면 소프트웨어의 새 버전으로 인해 구성 파일이 변경되고 이전 버전의 구성 파일을 계속 원격 호스트에 보관할 때 문제가 발생할 수 있습니다. –

답변

4

.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/ 
... 

플러그인 코드 :

파일을 바르 :

플러그인은 아래와 with_inidata inifile을 모듈에 직접 사용하기 위해 (부, 키 값) 목록의 계층 데이터를 변환

# (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이어야하며 필자의 예와 같이 작업을 작성해야합니다.

이 플러그인을 사용하면 ini 파일 관리를 간소화 할 수 있기를 바랍니다.

+0

피에르 이브 감사합니다. 정확히 내가 원하는 것입니다. 나는 당신의 코드를 최대한 빨리 시도 할 것이다. 나는 계속 너를 게시 할 것이다. –

+0

다시 한번 Pierre-Yves에게 감사드립니다. 난 당신의 코드를 시도하고 아주 잘 작동합니다. –

관련 문제