如何使用ANSIBLE在远程主机上创建RabbitMQ


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

任务:
使用名称:ansible_exchange创建DIRECT交换,ansible_exchange绑定两个队列myRemoteQueue1 和myRemoteQueue2,路由键为key1和key2。

让我们为这个任务来创建用于基础设施的部署容易ansible角色:

../roles/create_infra/tasks/main.yml

# Create exchange on remote host
- rabbitmq_exchange:
    name: ansible_exchange
    type: direct
    login_user: guest
    login_password: guest
    login_port: 15672

# Create a queues on remote host
- rabbitmq_queue:
    name: "{{item}}"
    login_user: guest
    login_password: guest
    login_host: localhost
    login_port: 15672
with_items:
    - myRemoteQueue1
    - myRemoteQueue2

- rabbitmq_binding:
    name: ansible_exchange
    destination: myRemoteQueue1
    type: queue
    routing_key: key1

- rabbitmq_binding:
    name: ansible_exchange
    destination: myRemoteQueue2
    type: queue
    routing_key: key2

并使用简单的基本剧本启动此角色:

infra.yml:

-  hosts:localhost 
    gather_facts:false 
    roles:
        -  create_infra

我相信create_infra的任务代码是selfexplanatory,有关更多信息,请参阅以下内容:

http://docs.ansible.com/ansible/latest/rabbitmq_exchange_module.html http://docs.ansible.com/ansible/latest/rabbitmq_binding_module.html http://docs.ansible.com/ansible/latest/rabbitmq_queue_module.html

现在让我们来启动infra.yml playbook:

$ ansible-playbook infra.yml

PLAY [localhost] ********************************************************************************************************************************************************************

TASK [create_infra : rabbitmq_exchange] *********************************************************************************************************************************************
changed: [localhost]

TASK [create_infra : rabbitmq_queue] ************************************************************************************************************************************************
ok: [localhost] => (item=myRemoteQueue1)
ok: [localhost] => (item=myRemoteQueue2)

TASK [create_infra : rabbitmq_binding] **********************************************************************************************************************************************
changed: [localhost]

TASK [create_infra : rabbitmq_binding] **********************************************************************************************************************************************
changed: [localhost]

PLAY RECAP **************************************************************************************************************************************************************************
localhost                  : ok=4    changed=3    unreachable=0    failed=0


并检查剧本是否符合我们的要求:

exchange创建验证:

$ ./rabbitmqctl list_exchanges | grep "ansible_"
ansible_exchange    direct

队列创建验证:

$ ./rabbitmqctl list_queues | grep myRemoteQueue*
myRemoteQueue1  0
myRemoteQueue2  0

绑定创建验证:

$ ./rabbitmqctl list_bindings | grep ansible_
ansible_exchange    exchange    myRemoteQueue1  queue   key1    []
ansible_exchange    exchange    myRemoteQueue2  queue   key2    []

伙计们,使用Ansible的AMQP模块并享受乐趣!