configuration - Ansible similar role refactoring -


installing munin plugins similar: create symlinks + template config file.

for example:

role munin_plugin_nginx:

---  - name: create symlink 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 

role munin_plugin_httpd:

---  - name: create symlink 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 

other munin_plugins have similar steps too.

how can refactor roles avoid 'copy-pasted' code?

one of possible ways:

add /roles/munin_plugin/vars/main.yml:

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

and /roles/munin_plugin/tasks/main.yml:

--- - name: check server type   fail:     msg: "unknown server type \"{{ server_type }}\" – should 1 of {{ munin_plugins_list.keys() }}"   when: munin_plugins_list[server_type] not defined  - name: create symlinks 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 

so can apply role this:

  roles:     - role: munin_plugin       server_type: nginx     - role: munin_plugin       server_type: httpd 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -