大网数据广告活动
服务器运维问答

Is it possible to define host aliases in ansible, similar to ssh config?

· 52CE 我爱测

In SSH config, you can use the following format to list multiple aliases that all specify the same host: Host DEV-USA-BGW100 dev-usa-bgw100 USA100 usa100 HostName 192.168.1.100 User ansible Host DEV-CAN-BGW101 dev-can-bgw101 CAN1…

问题原文

原帖链接 · Damian T. · CC BY-SA 4.0

In SSH config, you can use the following format to list multiple aliases that all specify the same host:

Host DEV-USA-BGW100 dev-usa-bgw100 USA100 usa100
    HostName 192.168.1.100
    User ansible

Host DEV-CAN-BGW101 dev-can-bgw101 CAN101 can101
    HostName 192.168.2.101
    User ansible

Host DEV-CAN-BGW102 dev-can-bgw102 CAN102 can102
    HostName 192.168.2.102
    User ansible

I would like to be able to do the same thing in ansible, but I'm unsure if it's possible. The reason is I sometimes want to test a single command on a single host without typing the entire proper hostname verbatim.

I've been searching around but haven't been able to find an answer. I did find a page that says that by default, ansible will use OpenSSH on the server, and thus should read the SSH config file. I tested this, but it didn't work.

已采纳解答

原帖链接 · John Mahowald · CC BY-SA 4.0

Define an inventory file or script that returns fully qualified host names. Then use patterns.

ansible '*can*101*' -m ping

Or, you can have groups of groups in inventory deep enough where the group name is an alias for 1 or 2 devices.

Defining the same host by different names in inventory may result in tasks being done twice to the same device.

SSH config is a separate name resolution thing Ansible is not aware of.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-07-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。