Ansible:使用动态清单从子网获取主机名

Ansible: Use Dynamic Inventory to get hostnames from a subnet

我需要 运行 在同一子网上的一些主机上运行 ansible playbook。

但是,我不知道主机名是什么,因为它们总是在变化,但是子网将保持不变。

有没有办法使用动态清单来查找给定子网上的所有主机?

提前致谢!

一个简单的解决方案可能是

nmap -n -sn 172.17.0.0/24 -oG - | awk '/Up$/{print }' > myInventory && ansible -i myInventory all -m ping

要获得更优雅的解决方案,请查看 Dynamic Inventory and Developing Dynamic Inventory Sources

根据 Vagrant 文档。它已经生成了适合 Ansible 的动态清单。

关于 Vagrant 的文档包括 Ansible 的页面:

Vagrant will generate an inventory file encompassing all of the virtual machines it manages, and use it for provisioning machines.

...

the generated inventory file is stored as part of your local Vagrant environment in .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory

使用带有 -i 选项的 Ansible 来指定清单应该可行。您可以通过列出主机来测试它:

ansible -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory --list-hosts