ansible-playbook hosts defined as "," defaults to play all the inventory defined in -i

Hi everyone,

 We use Jenkins to call ansible-playbook, usually the command would look like the following:

ansible-playbook -i targets deploy_portal.yml -e 'hosts=portal_dev,portal_dev2 version=1.0.35' -vvvv

It would go into the inventory file of hosts (-i targets) find the portal_dev,portal_dev2 tags and deploy to those servers only.

Recently we had a configuration error in Jenkins and the playbook call did the following:

ansible-playbook -i targets deploy_portal.yml -e 'hosts=, version=1.0.35' -vvvv

After reading through the documentation it looks like the hosts=, defaults to ,* which essentially went out to our inventory targets file and played on all servers defined in that file.

Is this normal behavior?  I've tried putting hosts = pleasetryagain in the ansible.cfg so it doesn't default to * but I think the comma in the environment variables overrides it and it continues
to play on all servers.

Any help would be greatly appreciated.

Thanks,
Stacey

A workaround can be to use this hosts statement in the playbook
- hosts: "{% if hosts == ',' %}pleasetryagain{% else %}{{ hosts }}{% endif %}"

First, -e 'hosts=' is not something Ansible recognizes in a special
way, its just a variable named `hosts`. In any case, I don't recommend
using variable names that conflict with directives.

The 'hosts default' configuration was removed many versions ago, so
either you are using an ancient version or that setting is being
ignored.

If you have (I suspect) the following:

hosts: "{{hosts}}"

you might want to use |default or |ternary filters to control what
happens when you pass , as a value, which should not work and errors
out in my tests.

Thanks for the example Kai. I’ll give it a try.

Thanks for the reply Brian. I was just going off of the Ansible configuration documentation and it would make sense if was removed.