The general case:
There is one central ansible inventory on, let's say,
'inventory_host'.
There are two (or more) hosts, let's say 'host1' and 'host2', that
can run ansible-based deployments, using vars from the central
ansible inventory directory (mounted via sshfs from
'inventory_host').
Some playbooks have local actions that use a variable named
'some_variable' for 'localhost', the value of which should be
different for 'host1' and 'host2'.
Since there is a common, central ansible inventory (and only one
'localhost' section in the inventory), there is no way to use one
value for 'some_variable' when deploying local actions with 'host1'
being the local machine, and a different value for that variable
when deploying local actions with 'host2' being the local machine.
This would not be a problem if you could override 'some_variable'
for 'localhost' host in the command line.
My use case:
A good example of such a variable, which is actually the one that I
would like to set it explicitly in the command line, is the variable
'ansible_python_interpreter'.
If 'host1' is an Archlinux-based machine, and 'host2' a Debian-based
machine, local actions of playbooks deployed from 'host1' should use
ansible_python_interpreter=/usr/bin/python2 in contrast with
playbooks deployed from 'host2'. So it would be nice if you could do
something like the following when deploying from 'host1':
$ ansible-playbook -e
'hostvars['localhost'].ansible_python_interpreter=/usr/bin/python2'
myplaybook.yml
I have thought of other solutions to the problem, such as using
something like unionfs to create a combination of the central
inventory with a local host_vars/localhost file, but I would prefer
a less complicated setup. Also, the ability to pass vars for a
specific host in the command line may apply not only to 'localhost'
vars, but to other cases too (e.g in case you need to quickly test
something for a specific host).