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

前端 未结 3 610
醉酒成梦
醉酒成梦 2020-12-11 20:16

Man pages for ansible and ansible-playbook define -i option as:

-i PATH, --inventory=PATH
       The PATH to the inve         


        
相关标签:
3条回答
  • 2020-12-11 20:35

    According to Michael DeHann, who created Ansible, the comma trick you're referring to is a hack that shouldn't be relied upon. It's a hack to run Ansible without an inventory file, for cases where you're going to run against localhost. That way you don't actually have to create an inventory file that just lists localhost.

    0 讨论(0)
  • 2020-12-11 20:42

    This is (now, at least) a documented feature. From the man page:

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

    (emphasis added)

    What's still not in the manual is that "comma separated host list" means that you need to add a comma even if the "list" is a single item, to distinguish between "target a single host called hostname":

    $ ansible -i 'hostname,' ...
    

    and "load inventory from a file called hostname":

    $ ansible -i 'hostname,' ...
    

    If anyone out there has time, maybe you could submit a pull request to change the help text to explain this (and to add a hyphen in "comma-separated", but maybe that's just me..)

    0 讨论(0)
  • 2020-12-11 20:54

    Actually, when you want to run commands against a specific host, don't add -i, instead, run it in the following way:

    ansible localhost -m ping 
    

    Use -i only to specify the path for dynamic inventory or hosts.

    0 讨论(0)
提交回复
热议问题