【问题标题】:Ansible Inventory Not Ordering AlphabeticallyAnsible Inventory 不按字母顺序排序
【发布时间】:2023-01-26 23:40:10
【问题描述】:

我有以下 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

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

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

  roles:
    - {role: common, tags: ['host', 'system']}

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

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [n1.my-host.com]
ok: [n2.my-host.com]
ok: [m1.my-host.com]

我宁愿期望它是 m1、n1,然后是 n2。有没有关于为什么不遵守排序顺序的想法?

【问题讨论】:

    标签: ansible hosts


    【解决方案1】:

    排序顺序受到尊重,但因为 Ansible 对多个主机执行任务在平行下,您会按照任务完成的顺序查看结果。

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

    - hosts: all
      order: sorted
      serial: 1
      gather_facts: false
      tasks:
        - ping:
    

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 2012-11-08
      • 2011-01-30
      • 2019-04-05
      • 2012-04-08
      • 2022-01-06
      相关资源
      最近更新 更多