当 Ansible 中的 --inventory 选项以尾随逗号赋值时,它如何工作?

How does --inventory option in Ansible work when it is given value with a trailing comma?

ansibleansible-playbook 的手册页将 -i 选项定义为:

-i PATH, --inventory=PATH
       The PATH to the inventory hosts file, which defaults to
       /etc/ansible/hosts.

然而 运行 在本地系统上,示例中使用了以下语法:

ansible -i "localhost," -c local -m ping localhost

这个 "localhost," 到底是什么,末尾有逗号(否则它被视为文件名),它与 PATH 有什么关系?

实际上,当您想对特定主机执行 运行 命令时,不要添加 -i,而是按以下方式 运行 添加:

ansible localhost -m ping 

仅使用 -i 指定动态清单或主机的路径。

根据 Ansible 的创建者 Michael DeHann 的说法,您所指的逗号技巧是 a hack that shouldn't be relied upon。对于没有清单文件的 运行 Ansible 的 hack,对于你要 运行 针对本地主机的情况。这样您实际上不必创建仅列出本地主机的清单文件。

这是(至少现在)已记录的功能。来自 man page:

-i, --inventory, --inventory-file
specify inventory host path or comma separated host list. --inventory-file is deprecated

(强调)

手册中还没有的是"comma separated host list"的意思是即使"list"是单项也需要加一个逗号,用来区分"target a single host called hostname":

$ ansible -i 'hostname,' ...

和"load inventory from a file called hostname":

$ ansible -i 'hostname,' ...

如果有人有时间,也许你可以提交一个 pull request 来更改 the help text 来解释这一点(并在 "comma-separated" 中添加一个连字符,但也许只有我..)