2016-07-15 4 views
0

munin 플러그인을 매우 비슷하게 설치하십시오 : 일부 심볼릭 링크 + 템플릿 구성 파일을 만드십시오. 예를 들어가능성있는 유사한 역할 리팩토링

:

역할 munin_plugin_nginx :

--- 

- name: create symlink for plugin 
    file: 
    src="/usr/share/munin/plugins/{{ item }}" 
    dest="/etc/munin/plugins/{{ item }}" 
    state=link 
    with_items: 
    - "nginx_request" 
    - "nginx_status" 

- name: template /etc/munin/plugin-conf.d/nginx 
    template: 
    src: etc/munin/plugin-conf.d/nginx.j2 
    dest: /etc/munin/plugin-conf.d/nginx 
    owner: root 
    group: root 
    mode: 0644 
    notify: restart munin-node 

역할 munin_plugin_httpd :

--- 

- name: create symlink for plugin 
    file: 
    src="/usr/share/munin/plugins/{{ item }}" 
    dest="/etc/munin/plugins/{{ item }}" 
    state=link 
    with_items: 
    - "apache_accesses" 
    - "apache_processes" 
    - "apache_volume" 

- name: template /etc/munin/plugin-conf.d/httpd 
    template: 
    src: etc/munin/plugin-conf.d/httpd.j2 
    dest: /etc/munin/plugin-conf.d/httpd 
    owner: root 
    group: root 
    mode: 0644 
    notify: restart munin-node 

다른 munin_plugins도 유사한 단계가 있습니다.

어떻게 '복사 붙여 넣기'코드를 피하기 위해이 역할을 리팩터링 할 수 있습니까? 가능한 방법의

답변

3

하나 :

/roles/munin_plugin/vars/main.yml을 추가

--- 
munin_plugins_list: 
    nginx: 
    symlinks: 
     - nginx_request 
     - nginx_status 
    httpd: 
    symlinks: 
     - apache_accesses 
     - apache_processes 
     - apache_volume 

그리고 /roles/munin_plugin/tasks/main.yml을 :

--- 
- name: check server type 
    fail: 
    msg: "Unknown server type \"{{ server_type }}\" – should be one of {{ munin_plugins_list.keys() }}" 
    when: munin_plugins_list[server_type] is not defined 

- name: create symlinks for plugin 
    file: 
    src: "/usr/share/munin/plugins/{{ item }}" 
    dest: "/etc/munin/plugins/{{ item }}" 
    state: link 
    with_items: "{{ munin_plugins_list[server_type]['symlinks'] }}" 

- name: template config file 
    template: 
    src: "etc/munin/plugin-conf.d/{{ server_type }}.j2" 
    dest: "/etc/munin/plugin-conf.d/{{ server_type }}" 
    owner: root 
    group: root 
    mode: 0644 
    notify: restart munin-node 

그래서이 같은 역할을 적용 할 수 있습니다

roles: 
    - role: munin_plugin 
     server_type: nginx 
    - role: munin_plugin 
     server_type: httpd 
관련 문제