2016-07-15 8 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 
関連する問題