Ansible - 按照我的意愿与主机一起玩 [英] Ansible - Play with hosts in order I desire
问题描述
当运行剧本时,Ansible 随机将节点设置为第一、第二和第三.
When running a playbook Ansible randomly sets a node as first, second and third.
TASK [setup] ********
ok: [node-p02]
ok: [node-p03]
ok: [node-p01]
如何配置 Ansible 以确保它按排序顺序与主机一起执行?示例:
How can I configure Ansible to ensure it executes with the hosts in sorted order? Example:
TASK [setup] ********
ok: [node-p01]
ok: [node-p02]
ok: [node-p03]
Serial: 1
不是一个选项,因为它会减慢播放速度,而且我的剧本适用于单次播放中的 3 个节点.
Serial: 1
is not an option, since it slows down the play, and my playbook is meant for 3 nodes in a single play.
推荐答案
适用于 Ansible 2.4 及更高版本:
这是现在的默认行为,ansible 将按照 inventory
文件中提到的 order 播放主机.Ansible 还提供了一些内置方式,您可以使用 order
来控制它:
Applicable for Ansible 2.4 and higher:
This is now the default behaviour, ansible will play the hosts in the order they were mentioned in the inventory
file. Ansible also provides a few built in ways you can control it with order
:
- hosts: all
order: sorted
gather_facts: False
tasks:
- debug:
var: inventory_hostname
order
可能的值是:
- 库存:默认值.订单是由库存提供"
- reverse_inventory:顾名思义,这颠倒了库存按提供"的顺序
- sorted:主机按名称的字母顺序排序
- reverse_sorted:主机按名称按字母逆序排序
- shuffle:每次运行时主机随机排序
来源:https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#hosts-and-users
这篇关于Ansible - 按照我的意愿与主机一起玩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!