Ansible-运行剧本时如何控制主机的顺序 [英] Ansible - how to control the order of the hosts when playbook is ran

查看:1074
本文介绍了Ansible-运行剧本时如何控制主机的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在清单文件中定义了两台计算机:

Let's say that we have defined two machines in our inventory file:

[db-server-preprod] 172.16.0.1 172.16.0.2

[db-server-preprod] 172.16.0.1 172.16.0.2

如果我针对该组运行一本剧本,则在序列号为0或顺序为1且顺序似乎是在组中定义IP的顺序的情况下,它们将同时在两台计算机上运行.

If I run a playbook against this group will run in the same time on both machines if serial is 0 or sequentially if is 1 and the order seems to be the one in which the IPs are defined in the group.

但是问题是我是否可以控制已定义机器上的剧本执行顺序?

同一角色对于其中一台计算机(主机)的行为略有不同,但对于我要成为主机的计算机将成为执行该剧本的第一台计算机而言,这一点很重要.

The same role behaves slightly different for one of the machines (the master) as opposed to all the others but is important that the machine that I want to be the master will be the first one on which the playbook executes.

到目前为止我的想法:

  • 创建不同的组名,其中IP的顺序颠倒并且根据在一台主机或另一台主机上运行的需求而定,但是缺点是我想我需要复制group_vars,因此每个db-server-preprod*变体都应该有一个+所有其他环境.
  • 将安装分为两个不同的角色-一个用于主服务器和备用服务器,根据需要,我将在想要的机器上运行一个我想要的角色.
  • create different group names in which the order of the IPs is reversed and based on needs run on one host or another but the disadvantage is that I think I would need to duplicate group_vars so there will be one for each db-server-preprod* variant + all the other environments.
  • separate the installation on two different roles - one for master and for standby servers and based on the needs I will run the one that I want on which machine I want.

推荐答案

您可以将主控主机放置在单独的组中,然后一次将通用角色应用于所有服务器,然后仅将主角色应用于主服务器,而从属服务器-除模式外,对除主服务器以外的所有服务器都具有角色.

You may place your master host into separate group, then apply common role to all servers at once, then apply master-roles only to master server, and slave-roles to all servers except master using excluding pattern.

库存:

[all-servers]
host1
host2
host3
host4

[master-server]
host2

剧本:

---
- hosts: all-servers
  gather_facts: no
  tasks:
    - debug: msg=role-common

- hosts: master-server
  gather_facts: no
  tasks:
    - debug: msg=role-master

- hosts: all-servers:!master-server
  gather_facts: no
  tasks:
    - debug: msg=role-slave

这篇关于Ansible-运行剧本时如何控制主机的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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