# specifying both inventory-file & group from that file

**URL:** <https://forum.ansible.com/t/specifying-both-inventory-file-group-from-that-file/31964>\
**Category:** Ansible Project\
**Created:** [January 14, 2020, 6:16am UTC](https://forum.ansible.com/t/specifying-both-inventory-file-group-from-that-file/31964 "2020-01-14T06:16:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sd\_Neil](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sd\_Neil](https://forum.ansible.com/u/Sd_Neil)\
**Post date:** [January 14, 2020, 6:16am UTC](https://forum.ansible.com/t/specifying-both-inventory-file-group-from-that-file/31964/1 "2020-01-14T06:16:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cassell](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cassell](https://forum.ansible.com/u/cassell)\
**Post date:** [January 14, 2020, 6:23am UTC](https://forum.ansible.com/t/specifying-both-inventory-file-group-from-that-file/31964/2 "2020-01-14T06:23:57Z")

</div>

`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

---

<div class="post-metadata">

**Author:** ![Sd\_Neil](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Sd\_Neil](https://forum.ansible.com/u/Sd_Neil)\
**Post date:** [January 14, 2020, 6:40am UTC](https://forum.ansible.com/t/specifying-both-inventory-file-group-from-that-file/31964/3 "2020-01-14T06:40:37Z")

</div>

- 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!
