Ansible-任务序列1反向顺序 [英] Ansible - task serial 1 reverse order

查看:105
本文介绍了Ansible-任务序列1反向顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建两本剧本,一本用于停止环境,另一本用于启动环境.

部分环境是RabbitMQ集群,对于该集群而言,停止/启动顺序非常重要,特别是最后一个停止的节点必须是第一个启动的节点.

我想知道是否有一种方法可以指定针对某个组运行任务的相反顺序. 这样,我可以在序列号1处应用止损,在序列号1处应用起始和反向分组顺序.

我还没有找到一种方法,但是以相反的顺序两次定义了rabbitmq主机组(使用不同的名称),这似乎有点令人讨厌.

还尝试了以下操作:

- hosts: "{ myhostsgroup | sort(reverse=False) }"
  serial: 1

还有

- hosts: "{ myhostsgroup | reverse }"
  serial: 1

但是无论尝试哪种情况及其变化形式(reverse = True,reverse | list),结果都保持不变

任何帮助将不胜感激.

解决方案

您可以在运行时创建动态组:

---
- hosts: localhost
  gather_facts: no
  tasks:
    - add_host:
        name: "{{ item }}"
        group: forward
      with_items: "{{ groups['mygroup'] }}"

    - add_host:
        name: "{{ item }}"
        group: backward
      with_items: "{{ groups['mygroup'] | reverse | list }}"

- hosts: forward
  gather_facts: no
  serial: 1
  tasks:
    - debug:

- hosts: backward
  gather_facts: no
  serial: 1
  tasks:
    - debug:

I'd like to create two playbooks, one to stop an environment, another to start it.

Part of the environment is a RabbitMQ cluster, for which stop/start order is quite important, specifically the last node stopped needs to be the first node started.

I was wondering if there is a way to specify a reverse order for running a task against a group. That way I could apply the stop with serial 1, and the start with serial 1 and reverse group order.

I haven't found a way to do that but to define the rabbitmq host group twice (under different names), in inverted order, which seems a bit distasteful.

Also attempted following:

- hosts: "{ myhostsgroup | sort(reverse=False) }"
  serial: 1

And

- hosts: "{ myhostsgroup | reverse }"
  serial: 1

But result stays the same, whichever case and its variation (reverse=True, reverse|list) is attempted

Any help would be greatly appreciated.

解决方案

You can create dynamic groups in runtime:

---
- hosts: localhost
  gather_facts: no
  tasks:
    - add_host:
        name: "{{ item }}"
        group: forward
      with_items: "{{ groups['mygroup'] }}"

    - add_host:
        name: "{{ item }}"
        group: backward
      with_items: "{{ groups['mygroup'] | reverse | list }}"

- hosts: forward
  gather_facts: no
  serial: 1
  tasks:
    - debug:

- hosts: backward
  gather_facts: no
  serial: 1
  tasks:
    - debug:

这篇关于Ansible-任务序列1反向顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆