Override hosts variable of Ansible playbook from the command line


This is a fragment of a playbook that I’m using (server.yml):

My hosts file has different groups of servers, e.g.

Now I want to execute ansible-playbook -i hosts/<env> server.yml and override hosts: web from server.yml to run this playbook for [droplets].

Can I just override as a one time off thing, without editing server.yml directly?



I don’t think Ansible provides this feature, which it should. Here’s something that you can do:

hosts: "{{ variable_host | default('web') }}"

and you can pass variable_host from either command-line or from a vars file, e.g.:

Leave a Reply