以下是一个使用Ansible来检查清单组和主机的代码示例:
---
- name: Check inventory groups and hosts
hosts: localhost
gather_facts: false
tasks:
- name: Print inventory groups
debug:
var: groups
- name: Print hosts in inventory groups
debug:
var: groups[group_name]
- name: Print all hosts in inventory
debug:
var: ansible_play_hosts_all
以上代码示例会输出清单组和主机的信息。你可以将上面的代码保存为一个名为check_inventory.yml
的文件,并使用以下命令来运行它:
ansible-playbook check_inventory.yml -i your_inventory_file
其中,your_inventory_file
是你的Ansible清单文件的路径。
注意:在上述示例中,我们使用了localhost
作为目标主机,这意味着运行该代码的主机将被视为Ansible的目标主机。你可以根据你的需求将这个主机更改为其他主机名,或者使用all
来指定所有主机。