Ansible - 如何在 playbook 运行时控制主机的顺序 [英] Ansible - how to control the order of the hosts when playbook is ran
问题描述
假设我们在库存文件中定义了两台机器:
Let's say that we have defined two machines in our inventory file:
[db-server-preprod]172.16.0.1172.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.
但问题是我是否可以控制在定义的机器上执行 playbook 的顺序?
同一角色对于其中一台机器(主机)的行为与其他所有机器略有不同,但重要的是,我想成为主机的机器将是剧本执行的第一个机器.
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 - 如何在 playbook 运行时控制主机的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!