From a67dde50f26e0f76d91f0744e8e8341dac95dae2 Mon Sep 17 00:00:00 2001 From: Larry Smith Jr Date: Sat, 20 May 2017 21:19:34 -0400 Subject: [PATCH] Cleaned up code as part of issue #10 Signed-off-by: Larry Smith Jr --- README.md | 92 +++++++----------------------------- defaults/main.yml | 47 +++++++++++++----- handlers/main.yml | 1 + tasks/debian.yml | 6 +++ tasks/fedora.yml | 7 +++ tasks/main.yml | 17 +++++-- tasks/rabbitmq_ha_config.yml | 27 +++++++---- tasks/rabbitmq_users.yml | 39 ++++++++------- tasks/redhat.yml | 7 +++ 9 files changed, 126 insertions(+), 117 deletions(-) diff --git a/README.md b/README.md index 99c019d..b3b03ad 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ Role Name ========= -Installs rabbitmq https://www.rabbitmq.com/ (Configurable...HA and Clustering ready) +An [Ansible] role to install/configure [RabbitMQ] Build Status ------------ @@ -11,67 +11,30 @@ Build Status Requirements ------------ -Ensure hostnames are resolvable prior to clustering...either update /etc/hosts or ensure DNS is working. +Ensure hostnames are resolvable prior to clustering...either update /etc/hosts +or ensure DNS is working. Vagrant ------- Spin up a 3 node HA Cluster for testing... Install Ansible role on your host: -```` +``` sudo ansible-galaxy install -r requirements.yml -f -```` +``` Now spin up your environment... -```` +``` vagrant up -```` +``` When you are done testing, tear it all down... -```` +``` ./cleanup.sh -```` +``` Role Variables -------------- -```` ---- -# defaults file for ansible-rabbitmq -rabbitmq_config_ha: false #defines if rabbitmq ha should be configured...define here or in group_vars/group -rabbitmq_enable_clustering: false #defines if setting up a rabbitmq cluster...define here or in group_vars/group -rabbitmq_erlang_cookie: 'LSKNKBELKPSTDBBCHETL' #define erlang cookie for cluster...define here or in group_vars/group -rabbitmq_erlang_cookie_file: '/var/lib/rabbitmq/.erlang.cookie' -rabbitmq_config: - - queue_name: logstash - durable: true - exchange_name: logstash - type: direct - routing_key: logstash - tags: 'ha-mode=all,ha-sync-mode=automatic' -rabbitmq_debian_repo: 'deb http://www.rabbitmq.com/debian/ testing main' -rabbitmq_debian_repo_key: 'http://www.rabbitmq.com/rabbitmq-signing-key-public.asc' -rabbitmq_master: [] #defines the inventory host that should be considered master...define here or in group_vars/group -rabbitmq_redhat_repo_key: 'https://www.rabbitmq.com/rabbitmq-signing-key-public.asc' -rabbitmq_redhat_package: 'rabbitmq-server-{{ rabbitmq_redhat_version }}-1.noarch.rpm' -rabbitmq_redhat_url: 'http://www.rabbitmq.com/releases/rabbitmq-server/v{{ rabbitmq_redhat_version }}' -rabbitmq_redhat_version: '3.6.1' -rabbitmq_users: #define admin user to create in order to login to WebUI - - name: rabbitmqadmin - password: rabbitmqadmin - vhost: / - configure_priv: '.*' - read_priv: '.*' - write_priv: '.*' - tags: 'administrator' #define comma separated list of tags to assign to user....management,policymaker,monitoring,administrator...required for management plugin. https://www.rabbitmq.com/management.html -```` - -example... -group_vars/rabbitmq-cluster-nodes -```` ---- -rabbitmq_enable_clustering: true -rabbitmq_config_ha: false -rabbitmq_master: ans-test-1 -```` +[Role Defaults](./defaults/main.yml) Dependencies ------------ @@ -81,33 +44,7 @@ None Example Playbook ---------------- -```` ---- -- hosts: all - become: true - vars: - - pri_domain_name: 'test.vagrant.local' - roles: - tasks: - - name: updating /etc/hosts - lineinfile: - dest: /etc/hosts - regexp: "^{{ hostvars[item].ansible_ssh_host }} {{ item }} {{ item }}.{{ pri_domain_name }}" - line: "{{ hostvars[item].ansible_ssh_host }} {{ item }} {{ item }}.{{ pri_domain_name }}" - state: present - with_items: "{{ groups['all'] }}" - -- hosts: all - become: true - vars: - - rabbitmq_config_ha: true - - rabbitmq_enable_clustering: true - - pri_domain_name: 'test.vagrant.local' - - rabbitmq_master: 'node0' - roles: - - role: ansible-rabbitmq - tasks: -```` +[Example Playbook](./playbook.yml) License ------- @@ -118,6 +55,11 @@ Author Information ------------------ Larry Smith Jr. -- @mrlesmithjr +- [@mrlesmithjr] - http://everythingshouldbevirtual.com - mrlesmithjr [at] gmail.com + +[@mrlesmithjr]: + +[Ansible]: +[RabbitMQ]: diff --git a/defaults/main.yml b/defaults/main.yml index ad22ead..90d7fb5 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,28 +1,49 @@ --- # defaults file for ansible-rabbitmq -rabbitmq_config: - - queue_name: 'logstash' - durable: true - exchange_name: logstash - type: 'direct' - routing_key: 'logstash' - tags: 'ha-mode=all,ha-sync-mode=automatic' -rabbitmq_config_ha: false #defines if rabbitmq ha should be configured...define here or in group_vars/group +rabbitmq_config: [] + # - queue_name: 'logstash' + # durable: true + # exchange_name: logstash + # type: 'direct' + # routing_key: 'logstash' + # tags: 'ha-mode=all,ha-sync-mode=automatic' + +# Defines if rabbitmq ha should be configured +rabbitmq_config_ha: false + +rabbitmq_config_service: false + rabbitmq_debian_repo: 'deb http://www.rabbitmq.com/debian/ testing main' rabbitmq_debian_repo_key: 'https://www.rabbitmq.com/rabbitmq-release-signing-key.asc' -rabbitmq_enable_clustering: false #defines if setting up a rabbitmq cluster...define here or in group_vars/group -rabbitmq_erlang_cookie: 'LSKNKBELKPSTDBBCHETL' #define erlang cookie for cluster...define here or in group_vars/group + +# Defines if setting up a rabbitmq cluster +rabbitmq_enable_clustering: false + rabbitmq_erlang_cookie_file: '/var/lib/rabbitmq/.erlang.cookie' -rabbitmq_master: [] #defines the inventory host that should be considered master...define here or in group_vars/group + +rabbitmq_listen_port: 5672 +rabbitmq_listeners: [] + # - '127.0.0.1' + # - '::1' + +# Defines the inventory host that should be considered master +rabbitmq_master: [] + rabbitmq_redhat_repo_key: 'https://www.rabbitmq.com/rabbitmq-signing-key-public.asc' rabbitmq_redhat_package: 'rabbitmq-server-{{ rabbitmq_redhat_version }}-1.noarch.rpm' rabbitmq_redhat_url: 'http://www.rabbitmq.com/releases/rabbitmq-server/v{{ rabbitmq_redhat_version }}' rabbitmq_redhat_version: '3.6.1' -rabbitmq_users: #define admin user to create in order to login to WebUI + +# Define admin user to create in order to login to WebUI +rabbitmq_users: - name: 'rabbitmqadmin' password: 'rabbitmqadmin' vhost: '/' configure_priv: '.*' read_priv: '.*' write_priv: '.*' - tags: 'administrator' #define comma separated list of tags to assign to user....management,policymaker,monitoring,administrator...required for management plugin. https://www.rabbitmq.com/management.html + # Define comma separated list of tags to assign to user: + # management,policymaker,monitoring,administrator + # required for management plugin. + # https://www.rabbitmq.com/management.html + tags: 'administrator' diff --git a/handlers/main.yml b/handlers/main.yml index 259ce1c..4fdafe5 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -4,3 +4,4 @@ service: name: "rabbitmq-server" state: restarted + become: true diff --git a/tasks/debian.yml b/tasks/debian.yml index a031ead..b25d39b 100644 --- a/tasks/debian.yml +++ b/tasks/debian.yml @@ -3,22 +3,26 @@ apt_key: url: "{{ rabbitmq_debian_repo_key }}" state: present + become: true - name: debian | adding RabbitMQ repo apt_repository: repo: "{{ rabbitmq_debian_repo }}" state: present register: "rabbitmq_repo_added" + become: true - name: debian | updating apt cache apt: update_cache: yes + become: true when: rabbitmq_repo_added.changed - name: debian | installing RabbitMQ server apt: name: "{{ item }}" state: present + become: true with_items: - rabbitmq-server @@ -26,6 +30,7 @@ rabbitmq_plugin: names: rabbitmq_management state: enabled + become: true notify: restart rabbitmq-server - name: debian | ensuring that the RabbitMQ service is running @@ -33,3 +38,4 @@ name: rabbitmq-server state: started enabled: yes + become: true diff --git a/tasks/fedora.yml b/tasks/fedora.yml index 3d92963..6ac9c03 100644 --- a/tasks/fedora.yml +++ b/tasks/fedora.yml @@ -3,6 +3,7 @@ dnf: name: "{{ item }}" state: present + become: true with_items: - wget @@ -10,30 +11,36 @@ dnf: name: "erlang" state: present + become: true - name: fedora | adding RabbitMQ public GPG key rpm_key: key: "{{ rabbitmq_redhat_repo_key }}" state: present + become: true - name: fedora | downloading RabbitMQ get_url: url: "{{ rabbitmq_redhat_url }}/{{ rabbitmq_redhat_package }}" dest: "/opt/{{ rabbitmq_redhat_package }}" + become: true - name: fedora | installing RabbitMQ dnf: name: "/opt/{{ rabbitmq_redhat_package }}" state: present + become: true - name: fedora | starting and enabling RabbitMQ service service: name: "rabbitmq-server" state: started enabled: yes + become: true - name: fedora | enabling the RabbitMQ Management Console rabbitmq_plugin: names: rabbitmq_management state: enabled notify: restart rabbitmq-server + become: true diff --git a/tasks/main.yml b/tasks/main.yml index bd82f93..1da06c4 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -3,19 +3,30 @@ when: ansible_os_family == "Debian" - include: redhat.yml - when: ansible_distribution == "CentOS" or ansible_distribution == "Red Hat Enterprise Linux" + when: > + ansible_distribution == "CentOS" or + ansible_distribution == "Red Hat Enterprise Linux" - include: fedora.yml when: ansible_distribution == "Fedora" +- include: config.yml + when: rabbitmq_config_service + - name: checking to see if already clustered stat: path=/etc/rabbitmq/clustered register: clustered - include: rabbitmq_clustering.yml - when: rabbitmq_enable_clustering and (clustered.stat.exists != True) + when: > + rabbitmq_enable_clustering and + not clustered['stat']['exists'] - include: rabbitmq_ha_config.yml - when: rabbitmq_config_ha and rabbitmq_enable_clustering + when: > + rabbitmq_config_ha and + rabbitmq_enable_clustering and + rabbitmq_config is defined - include: rabbitmq_users.yml + when: rabbitmq_users is defined diff --git a/tasks/rabbitmq_ha_config.yml b/tasks/rabbitmq_ha_config.yml index 8811b6d..bf74957 100644 --- a/tasks/rabbitmq_ha_config.yml +++ b/tasks/rabbitmq_ha_config.yml @@ -6,39 +6,46 @@ - name: rabbitmq_ha_config | install rabbitMQ admin shell: wget http://guest:guest@localhost:15672/cli/rabbitmqadmin - when: not rabbitmqadmin_check.stat.exists + become: true + when: not rabbitmqadmin_check['stat']['exists'] - name: rabbitmq_ha_config | moving the rabbitMQ Admin shell: mv rabbitmqadmin /usr/sbin - when: not rabbitmqadmin_check.stat.exists + become: true + when: not rabbitmqadmin_check['stat']['exists'] - name: rabbitmq_ha_config | making executable rabbitMQ Admin shell: chmod +x /usr/sbin/rabbitmqadmin notify: restart rabbitmq-server - when: not rabbitmqadmin_check.stat.exists + become: true + when: not rabbitmqadmin_check['stat']['exists'] - name: rabbitmq_ha_config | creating queue(s) - command: rabbitmqadmin declare queue name={{ item.queue_name }} durable={{ item.durable|lower }} + command: rabbitmqadmin declare queue name={{ item['queue_name'] }} durable={{ item['durable']|lower }} run_once: true + become: true with_items: "{{ rabbitmq_config }}" - name: rabbitmq_ha_config | setting up ha on queue(s) rabbitmq_policy: name: "ha-all" - pattern: "{{ item.queue_name }}" - tags: "{{ item.tags }}" + pattern: "{{ item['queue_name'] }}" + tags: "{{ item['tags'] }}" state: present run_once: true + become: true with_items: "{{ rabbitmq_config }}" - name: rabbitmq_ha_config | creating exchange(s) - command: rabbitmqadmin declare exchange name={{ item.exchange_name }} type={{ item.type }} + command: rabbitmqadmin declare exchange name={{ item['exchange_name'] }} type={{ item['type'] }} run_once: true + become: true with_items: "{{ rabbitmq_config }}" - when: item.exchange_name is defined + when: item['exchange_name'] is defined - name: rabbitmq_ha_config | creating binding(s) - command: rabbitmqadmin declare binding source={{ item.exchange_name }} destination_type="queue" destination={{ item.queue_name }} routing_key={{ item.routing_key }} + command: rabbitmqadmin declare binding source={{ item['exchange_name'] }} destination_type="queue" destination={{ item['queue_name'] }} routing_key={{ item['routing_key'] }} run_once: true + become: true with_items: "{{ rabbitmq_config }}" - when: item.exchange_name is defined + when: item['exchange_name'] is defined diff --git a/tasks/rabbitmq_users.yml b/tasks/rabbitmq_users.yml index 8bce8b2..98e45e2 100644 --- a/tasks/rabbitmq_users.yml +++ b/tasks/rabbitmq_users.yml @@ -1,27 +1,34 @@ --- - name: rabbitmq_users | creating rabbitmq users rabbitmq_user: - name: "{{ item.name }}" - password: "{{ item.password }}" - vhost: "{{ item.vhost }}" - configure_priv: "{{ item.configure_priv }}" - read_priv: "{{ item.read_priv }}" - write_priv: "{{ item.write_priv }}" - tags: "{{ item.tags }}" + name: "{{ item['name'] }}" + password: "{{ item['password'] }}" + vhost: "{{ item['vhost'] }}" + configure_priv: "{{ item['configure_priv'] }}" + read_priv: "{{ item['read_priv'] }}" + write_priv: "{{ item['write_priv'] }}" + tags: "{{ item['tags'] }}" state: present + become: true with_items: "{{ rabbitmq_users }}" - when: (rabbitmq_enable_clustering is defined and not rabbitmq_enable_clustering) or rabbitmq_enable_clustering is not defined + when: > + (rabbitmq_enable_clustering is defined and + not rabbitmq_enable_clustering) or + rabbitmq_enable_clustering is not defined - name: rabbitmq_users | creating rabbitmq users rabbitmq_user: - name: "{{ item.name }}" - password: "{{ item.password }}" - vhost: "{{ item.vhost }}" - configure_priv: "{{ item.configure_priv }}" - read_priv: "{{ item.read_priv }}" - write_priv: "{{ item.write_priv }}" - tags: "{{ item.tags }}" + name: "{{ item['name'] }}" + password: "{{ item['password'] }}" + vhost: "{{ item['vhost'] }}" + configure_priv: "{{ item['configure_priv'] }}" + read_priv: "{{ item['read_priv'] }}" + write_priv: "{{ item['write_priv'] }}" + tags: "{{ item['tags'] }}" state: present run_once: yes + become: true with_items: "{{ rabbitmq_users }}" - when: (rabbitmq_enable_clustering is defined and rabbitmq_enable_clustering) + when: > + rabbitmq_enable_clustering is defined and + rabbitmq_enable_clustering diff --git a/tasks/redhat.yml b/tasks/redhat.yml index 484597d..afa714c 100644 --- a/tasks/redhat.yml +++ b/tasks/redhat.yml @@ -3,6 +3,7 @@ yum: name: "{{ item }}" state: present + become: true with_items: - epel-release - wget @@ -11,30 +12,36 @@ yum: name: "erlang" state: present + become: true - name: redhat | adding RabbitMQ public GPG key rpm_key: key: "{{ rabbitmq_redhat_repo_key }}" state: present + become: true - name: redhat | downloading RabbitMQ get_url: url: "{{ rabbitmq_redhat_url }}/{{ rabbitmq_redhat_package }}" dest: "/opt/{{ rabbitmq_redhat_package }}" + become: true - name: redhat | installing RabbitMQ yum: name: "/opt/{{ rabbitmq_redhat_package }}" state: present + become: true - name: redhat | starting and enabling RabbitMQ service service: name: "rabbitmq-server" state: started enabled: yes + become: true - name: redhat | enabling the RabbitMQ Management Console rabbitmq_plugin: names: rabbitmq_management state: enabled notify: restart rabbitmq-server + become: true -- 2.41.0