Ansible 检查主机文件

Ansible check hosts file

Ansible:2.9

我搜索命令或选项如何检查我的“主机”清单文件与 group_vars 树 fs 之间的一致性。

我解释一下:

[all]
N01
N02
S01
S02

[zone1]
N01
N02

[zone2]
S01
S02

[zone3]
Z01
Z02

PlayBook_dir
\__group_vars:
   - zone1.yml
   - zone2.yml

当我启动我的 Playbook 时,他显示类似“group_var 目录树中没有 zone3 错误”。

你知道这个或其他测试吗?

谢谢!

您可以在本地主机上使用 stat module. The results of this can be checked with assert.

检查 group_vars 文件是否存在。
    # First stat the path for file existence by looping over inventory groups
    - stat:
        path: "{{ playbook_dir }}/group_vars/{{ item.key }}.yml"
      loop: "{{ groups | dict2items }}"
      register: gvars

    # Loop over the registered variable and fail assertion if path doesn't exist
    - assert:
        that:
        - item.stat.exists
        fail_msg: "{{ item.invocation.module_args.path }} does not exist"
      when:
        - item.item.key not in ["all", "ungrouped"]
      loop: "{{ gvars.results }}"
      loop_control:
        label: "{{ item.item.key }}"