# Some jinja filters don't work in host pattern

**URL:** <https://forum.ansible.com/t/some-jinja-filters-dont-work-in-host-pattern/17141>\
**Category:** Ansible Project\
**Created:** [December 24, 2014, 1:01pm UTC](https://forum.ansible.com/t/some-jinja-filters-dont-work-in-host-pattern/17141 "2014-12-24T13:01:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raghu\_Udiyar](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Raghu\_Udiyar](https://forum.ansible.com/u/Raghu_Udiyar)\
**Post date:** [December 24, 2014, 1:01pm UTC](https://forum.ansible.com/t/some-jinja-filters-dont-work-in-host-pattern/17141/1 "2014-12-24T13:01:40Z")

</div>

Hello,

I’m trying to work on a requirement to have the host pattern change depending on certain host groups or parameters being passed to the playbook :

This is what I need exactly :

1. When an external var cli\_hosts is provided, this playbook runs on the provided host pattern

2. If not, it defaults to a group ‘new\_instances’, I’ve used this group to indicate new instances that are spawned in a parent playbook, which includes this play via include statement.

I could have used cli\_hosts ( by using set\_fact ) in the parent playbook, expecting that this one will pick it up, but that doesn’t work. Passing it as a parameter in the include statement doesn’t work either.

From what I understand, this is because host patterns are not in the set\_fact scope. Also, set\_fact is host specific fact, so it gets set in the pattern of the original play (localhost in my case). I think it would help if there is a way to access the global scope somehow. I’m not sure why passing it as a parameter not work ( via include )

1. If either of the above two are not present, default to the entire cluster.

This is the play I tried :

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 24, 2014, 1:40pm UTC](https://forum.ansible.com/t/some-jinja-filters-dont-work-in-host-pattern/17141/2 "2014-12-24T13:40:52Z")

</div>

I'm very confused as to what you are attempting.

First, 'groups' is not available at the host pattern level, as it is  
host dependent.  
Second, the default filter does work, you can test this easily with:

- hosts: "{{cli\_hosts|default('new\_instances')}}"

having the ternary condition you want (if no new\_instances) is a lot  
harder as the group won't get evaluated until after the expression is.

---

<div class="post-metadata">

**Author:** ![Raghu\_Udiyar](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Raghu\_Udiyar](https://forum.ansible.com/u/Raghu_Udiyar)\
**Post date:** [December 24, 2014, 1:56pm UTC](https://forum.ansible.com/t/some-jinja-filters-dont-work-in-host-pattern/17141/3 "2014-12-24T13:56:20Z")

</div>

> I’m very confused as to what you are attempting.

I’m basically trying to reuse an exiting playbook, by passing a new host pattern to it, from another play. One use case is, I have a “common” play that runs on all hosts. I have another play that spawns new nodes. After spawning new nodes, I include this common play, and need a way to pass the new hosts to the common play. This common play cannot be a role, because there are certain decisions I make based on the pattern, and pass parameters to the role.

> First, ‘groups’ is not available at the host pattern level, as it is  
> host dependent.

I was under the impression that groups was at the “current playbook” scope, as its available for new plays in the same playbook (operating on different hosts pattern).

> Second, the default filter does work, you can test this easily with:
> 
> - hosts: “{{cli\_hosts|default(‘new\_instances’)}}”
> 
> having the ternary condition you want (if no new\_instances) is a lot  
> harder as the group won’t get evaluated until after the expression is.

The reason I have the ternary condition is I’m unable to pass the new host pattern to “cli\_hosts”, if I could do that, than the pattern expression will be simpler. Is there a way I can do that? that is pass the host pattern via include.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 24, 2014, 2:02pm UTC](https://forum.ansible.com/t/some-jinja-filters-dont-work-in-host-pattern/17141/4 "2014-12-24T14:02:08Z")

</div>

> I was under the impression that groups was at the "current playbook" scope,  
> as its available for new plays in the same playbook (operating on different  
> hosts pattern).

groups is available to every play but only once hosts/inventory is  
loaded, which is not the case with the - hosts: directive.

> The reason I have the ternary condition is I'm unable to pass the new host  
> pattern to "cli\_hosts", if I could do that, than the pattern expression will  
> be simpler. Is there a way I can do that? that is pass the host pattern via  
> include.

I don't understand, if you are passing cli\_hosts in the command line  
it should be easy enough to pass a pattern -e  
'cli\_hosts=group1:!group2'.

As for the play needing to target new\_hosts, just leave it at that, if  
there are no new\_hosts ti should be skipped.

---

<div class="post-metadata">

**Author:** ![Raghu\_Udiyar](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Raghu\_Udiyar](https://forum.ansible.com/u/Raghu_Udiyar)\
**Post date:** [December 24, 2014, 4:05pm UTC](https://forum.ansible.com/t/some-jinja-filters-dont-work-in-host-pattern/17141/5 "2014-12-24T16:05:13Z")

</div>

> > I was under the impression that groups was at the “current playbook” scope,  
> > as its available for new plays in the same playbook (operating on different  
> > hosts pattern).
> 
> groups is available to every play but only once hosts/inventory is  
> loaded, which is not the case with the - hosts: directive.

Ok, if I could pass the pattern to the included play, I can use it directly in the hosts pattern. But I’m not able to pass that.

> > The reason I have the ternary condition is I’m unable to pass the new host  
> > pattern to “cli\_hosts”, if I could do that, than the pattern expression will  
> > be simpler. Is there a way I can do that? that is pass the host pattern via  
> > include.
> 
> I don’t understand, if you are passing cli\_hosts in the command line  
> it should be easy enough to pass a pattern -e  
> ‘cli\_hosts=group1:!group2’.
> 
> As for the play needing to target new\_hosts, just leave it at that, if  
> there are no new\_hosts ti should be skipped.

That is exactly what I was using in fact. But now there is a new requirement of having a default cluster if none of them are defined, that is cli\_cluster or new\_hosts. and, that is why I started working with the defaults filter to come up with an expression to solve this.

– Raghu
