Configuration Ansible相似角色重构

Configuration Ansible相似角色重构,configuration,ansible,Configuration,Ansible,安装munin插件非常类似:创建一些符号链接+模板配置文件 例如: 角色munin\u插件\u nginx: --- - name: create symlink for plugin file: src="/usr/share/munin/plugins/{{ item }}" dest="/etc/munin/plugins/{{ item }}" state=link with_items: - "nginx_request" - "ngi

安装
munin
插件非常类似:创建一些符号链接+模板配置文件

例如:

角色
munin\u插件\u 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\u插件\u 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_插件也有类似的步骤

如何重构此角色以避免“复制粘贴”代码?

可能的方法之一:

添加
/roles/munin_plugin/vars/main.yml

---
munin_plugins_list:
  nginx:
    symlinks:
      - nginx_request
      - nginx_status
  httpd:
    symlinks:
      - apache_accesses
      - apache_processes
      - apache_volume
---
- 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/munin_plugin/tasks/main.yml

---
munin_plugins_list:
  nginx:
    symlinks:
      - nginx_request
      - nginx_status
  httpd:
    symlinks:
      - apache_accesses
      - apache_processes
      - apache_volume
---
- 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