【问题标题】:Ansible check hosts fileAnsible 检查主机文件
【发布时间】:2021-12-16 18:22:51
【问题描述】:

Ansible:2.9

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

我解释一下:

  • 我的主机文件:
[all]
N01
N02
S01
S02

[zone1]
N01
N02

[zone2]
S01
S02

[zone3]
Z01
Z02

  • Group_vars 树 FS:
PlayBook_dir
\__group_vars:
   - zone1.yml
   - zone2.yml
  • 希望,使用此检查进行测试:

当我启动我的 Playbook 时,他会显示类似“group_var dir tree 中的错误 No zone3”。

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

谢谢!

【问题讨论】:

    标签: ansible automated-tests ansible-inventory


    【解决方案1】:

    您可以在 localhost 上使用stat module 检查group_vars 文件是否存在。这个结果可以通过assert来检查。

        # 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 }}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多