specifying both inventory-file & group from that file

I work with a legacy 2.1 Ansible (mid-migration to 2.9).

I have had success specifying my own private hosts (ini) file that contains 2 Groups ( ’ -i ’ ). But I cannot find the 2.1 argument to running a playbook (that has no hosts directive) against both, custom-inventory file and single-group in that inventory-file.

I tried:

  1. -i /path/to/file:<group_label>
  2. -i /path/to/file --extra-vars “hosts=<group_label>”
  3. -i /path/to/file --extra-vars “variable_host=<group_label>”
  4. -i /path/to/file -l <group_label>

…all result “ERROR! the field ‘hosts’ is required but was not set”

So I have two questions:
a) is it possible to specify both custom inventory file and individual group from that file in Ansible 2.1 ?
b) does anyone do this in Ansible 2.9 and if yes, would they share an newb-friendly example ?

For the time-being, I am splitting out my groups into distinct host files. For what its worth, the intent is to keep a hosts directive out of playbooks.

TIA Ya’ll

`hosts` directive is required in playbooks.

I often use

`hosts: "{{ ansible_limit }}"`

So the playbook runs against the hosts passed to the -l cli option. I've also seen folks swap ansible_limit for target, then force playbook operators to specify -e target=mygroup

V/r,
James Cassell

  • James Cassell !!

Thanks for your response-time and accuracy! Triple-points for guiding with 2 examples.

PLAYBOOK: hosts: "{{ target }}"
CLI: ‘ansible-playbook -i ~ME/projects/ansible/hosts -e target=<GROUP_LABEL> .yml -u -K --check --extra-vars “<SET_VAR>=<SET_VAL>”’

…was what I tried first, and what succeeded in a fashion I recognize (variable reference in playbook & as a key-value pair argument from the CLI).

Absolutely wonderful experience being guided, thank you James Cassell!