Ansible 库存不按字母顺序排序

spa*_*rkr 0 hosts ansible

我有以下 Ansible 主机文件:

# Contains the host mappings
[master]
m1.my-host.com ansible_host=192.168.0.100

[node]
n1.my-host.com ansible_host=192.168.0.102
n2.my-host.com ansible_host=192.168.0.103

[k3s_cluster:children]
master
node

[all:vars]
ansible_python_interpreter=/usr/bin/python3
Run Code Online (Sandbox Code Playgroud)

在我的剧本中,我说我需要像这样的有序执行:

---
- name: Setup hostname, users and groups
  hosts: all
  order: sorted
  gather_facts: true
  remote_user: myuser
  become: yes

  roles:
    - {role: common, tags: ['host', 'system']}
Run Code Online (Sandbox Code Playgroud)

但当我运行它时,我看到以下内容:

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [n1.my-host.com]
ok: [n2.my-host.com]
ok: [m1.my-host.com]
Run Code Online (Sandbox Code Playgroud)

我宁愿期望它是 m1、n1,然后是 n2。是否知道为什么不遵守排序顺序?

lar*_*sks 5

排序顺序受到尊重,但由于 Ansible 对多个主机并行执行任务,因此您会按照任务完成的顺序看到结果。

如果您要像这样序列化任务执行:

- hosts: all
  order: sorted
  serial: 1
  gather_facts: false
  tasks:
    - ping:
Run Code Online (Sandbox Code Playgroud)

您会看到执行顺序是一致的:

  1. m1.my-host.com
  2. n1.my-host.com
  3. n2.my-host.com

一般来说,您不希望像这样序列化执行,因为它会显着增加 playbook 的运行时间,但如果您有大量主机,有时使用数字>1 来批量执行任务是有意义的。