使用ansible管理rabbitmq

如果您考虑通过RabbitMQ实现更大规模的AMQP基础架构,那么肯定会尝试使用Ansible作为基础架构部署者,因为Ansible的RabbitMQ模块非常容易使用,让我们举个例子。

先编辑一个roles:

#cat roles/mq/tasks/main.yml
- rabbitmq_vhost:
    name: "{{ vhost }}"
    state: present
  register: create_vhost

- name: set_vhost_permission
  shell: "rabbitmqctl  set_permissions -p '{{ vhost }}' admin '.*' '.*' '.*'"
  when: create_vhost.changed

- rabbitmq_exchange:
    name: "{{ exchange }}"
    type: "{{ exchange_type }}"
    vhost: "{{ vhost }}"
    login_host: localhost
    login_user: "{{ user }}"
    login_password: "{{ pwd }}"
  when: exchange is defined

- rabbitmq_queue:
    name: "{{ queue }}"
    vhost: "{{ vhost }}"
    login_host: localhost
    login_user: "{{ user }}"
    login_password: "{{ pwd }}"

- rabbitmq_binding:
    name: "{{ exchange }}"
    destination: "{{ queue }}"
    type: queue
    routing_key: "{{ routing_key }}"
    vhost: "{{ vhost }}"
    login_host: localhost
    login_user: "{{ user }}"
    login_password: "{{ pwd }}"
  when: routing_key is defined and exchange is defined

- rabbitmq_binding:
    name: "{{ exchange }}"
    destination: "{{ queue }}"
    type: queue
    vhost: "{{ vhost }}"
    login_host: localhost
    login_user: "{{ user }}"
    login_password: "{{ pwd }}"
  when: routing_key is not defined and exchange is defined

然后创建一个workflow作为playbook的脚本:

#cat workflow/mq/rabbitmq_pr.yml
---
- hosts: 192.168.1.1
  roles:
    - mq
  sudo: yes
  vars:
    user: admin
    pwd: admin

对了,被控制的端需要有rabbitmqctl

然后就可以在ansible上添加mq、exchange并进行绑定了

 ansible-playbook /etc/ansible/workflow/mq/rabbitmq_pr.yml --extra-vars "vhost=test exchange=test_exchange exchange_type=direct queue=test_queue routing_key=test_key"

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This blog is kept spam free by WP-SpamFree.