# Is there a way to dynamically target subsets of an inventory group based on facts?

**URL:** <https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197>\
**Category:** Ansible Project\
**Tags:** fedora\
**Created:** [September 12, 2014, 2:48pm UTC](https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197 "2014-09-12T14:48:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [September 12, 2014, 2:48pm UTC](https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197/1 "2014-09-12T14:48:57Z")

</div>

Hi all,

I have an inventory file with hosts in different groups within. I would like to run a playbook on a certain group, but only where the host members of that group that have a certain OS (say, " ansible\_distribution=‘Fedora’ ".) I was told to look at the “group\_by” module, and so in my top-level site.yml file I put in the following:

# Set up OS-specific groups

- group\_by: key=os-{{ansible\_distribution}}

So then I tried to do the following:

$ ansible-playbook -i inventory-file -u root -k site.yml --tags=mailserver --limit=os-Fedora

but when I do so, I get:

ERROR: provided hosts list is empty

So am I doing this incorrectly, or for some reason, is the “os-Fedora” group empty? (i.e. the logic is not matching any hosts)

Also, is there a way to print the members of the group\_by variable out so I can see what’s being included?

Thanks,  
Will

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [September 12, 2014, 2:54pm UTC](https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197/2 "2014-09-12T14:54:26Z")

</div>

​The --limit happens early, at initialization​, and at that time the group  
is indeed empty.

AFAIK, there is no workaround for this.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 12, 2014, 3:42pm UTC](https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197/3 "2014-09-12T15:42:26Z")

</div>

You can’t use group\_by stuff at that point because it hasn’t run yet, true, but there is a solution to what you would like to do, using two plays in the same playbook

- hosts: all  
tasks:

- group\_by: key=os-{{ ansible\_distribution }}

- hosts: “{{ distro | default(‘all’) }}”  
tasks:

- action: ping

- …

ansible-playbook foo.yml -e “pattern=os-Fedora”

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 12, 2014, 3:42pm UTC](https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197/4 "2014-09-12T15:42:45Z")

</div>

Sorry in the above, the word “distro” should be “pattern”.

---

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [September 12, 2014, 6:15pm UTC](https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197/5 "2014-09-12T18:15:47Z")

</div>

Thanks, Michael! But can I do the following?

$ cat site.yml

---

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [September 12, 2014, 6:29pm UTC](https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197/6 "2014-09-12T18:29:46Z")

</div>

Hmmm, I guess not…

$ ansible-playbook -i temp-stor-inv -u root -k site.yml --tags=mailserver -e “distro=os-Fedora-20”  
SSH password:  
Traceback (most recent call last):  
File “/usr/bin/ansible-playbook”, line 309, in   
sys.exit(main(sys.argv[1:]))  
File “/usr/bin/ansible-playbook”, line 191, in main  
force\_handlers=options.force\_handlers  
File “/usr/lib/pymodules/python2.7/ansible/playbook/ **init**.py”, line 180, in **init**  
(self.playbook, self.play\_basedirs) = self.\_load\_playbook\_from\_file(playbook, vars)  
File “/usr/lib/pymodules/python2.7/ansible/playbook/ **init**.py”, line 271, in \_load\_playbook\_from\_file  
inc\_vars, inc\_path = self.\_get\_include\_info(play, basedir, play\_vars)  
File “/usr/lib/pymodules/python2.7/ansible/playbook/ **init**.py”, line 208, in \_get\_include\_info  
tokens = shlex.split(play\_ds.get(‘include’, ‘’))  
File “/usr/lib/python2.7/shlex.py”, line 279, in split  
return list(lex)  
File “/usr/lib/python2.7/shlex.py”, line 269, in next  
token = self.get\_token()  
File “/usr/lib/python2.7/shlex.py”, line 96, in get\_token  
raw = self.read\_token()  
File “/usr/lib/python2.7/shlex.py”, line 124, in read\_token  
nextchar = self.instream.read(1)  
AttributeError: ‘list’ object has no attribute ‘read’

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 12, 2014, 7:19pm UTC](https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197/7 "2014-09-12T19:19:12Z")

</div>

I’m unclear what you meant by “can I do X” specifically, what that meant you could or couldn’t do.

A traceback in ansible is a bug though, so please figure out what the minimum way to reproduce that is and file a ticket (provided you are using at least 1.7.1 already and it can be reproduced there).

If you can, checking the devel branch would also be useful.

---

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [September 12, 2014, 8:38pm UTC](https://forum.ansible.com/t/is-there-a-way-to-dynamically-target-subsets-of-an-inventory-group-based-on-facts/16197/8 "2014-09-12T20:38:22Z")

</div>

Sorry Michael, I will try to be more clear.

I am running Ansible 1.7.1 currently.

So my original site.yml is this:

$ cat site.yml.orig
