从 Ansible Inventory 文件获取主机信息
从 Ansible Inventory 文件获取主机信息
假设您在 inventory.ini 文件中定义了以下主机信息:
[managers]
controller1 ansible_connection=ssh ansible_host=192.168.214.101 ansible_ssh_user=root ansible_ssh_pass='thecloud2015.1'
controller2 ansible_connection=ssh ansible_host=192.168.214.102 ansible_ssh_user=root ansible_ssh_pass='thecloud2015.1'
controller3 ansible_connection=ssh ansible_host=192.168.214.103 ansible_ssh_user=root ansible_ssh_pass='thecloud2015.1'
您可以使用以下 Ansible 命令从 inventory.ini 文件中获取所有 controller1 的 ansible_host 内容:
ansible manager -i inventory.ini -m debug -a "var=hostvars['controller1']['ansible_host']"
解释:
-i inventory.ini指定使用 inventory.ini 文件作为主机信息来源。-m debug表示使用 debug 模块,用于打印变量信息。-a "var=hostvars['controller1']['ansible_host']"指定了要打印的变量名,即hostvars['controller1']['ansible_host']。hostvars是一个特殊的变量,它包含了所有主机的变量信息。可以通过主机名来访问。hostvars['controller1']['ansible_host']表示获取 controller1 主机的ansible_host变量的值。
最终输出结果应该是:
manager | SUCCESS => {
"hostvars['controller1']['ansible_host']": "192.168.214.101"
}
注意:
- 请确保您已经配置了 Ansible 环境并安装了所需的软件包。
- 将
manager替换为您的实际主机组名。 - 将
controller1替换为您要获取主机信息的目标主机名。
原文地址: https://www.cveoy.top/t/topic/nlYy 著作权归作者所有。请勿转载和采集!